Illegal? No. Why would it be? There's nothing criminal about what is, essentially, a cash advance. Nor is it criminal to refund a returned purchase in a particular way (unless the refund is in the form of cocaine I suppose).
Is it permitted by the contract(s) the merchant has with Visa, MC, etc? Who knows. Even if it isn't allowed per their contract, that's not the same as "illegal".