Implement a facade for a payment provider
Implement a facade for a payment provider in JavaScript: expose charge and refund and keep the provider's request format inside. Node.js 24.
A payment SDK has many options and its own vocabulary. A facade exposes the two operations you need, so the provider can change without touching checkout code.
class FakeProvider {
createCharge({ amount_minor, currency_code, source }) {
return { id: 'ch_1', paid: true, amount_minor, currency_code, source };
}
createRefund({ charge_id }) {
return { id: 're_1', charge_id, status: 'done' };
}
}
function createPayments(provider) {
return {
charge(cents, currency, token) {
const result = provider.createCharge({ amount_minor: cents, currency_code: currency.toUpperCase(), source: token });
return { id: result.id, ok: result.paid };
},
refund(chargeId) {
return { ok: provider.createRefund({ charge_id: chargeId }).status === 'done' };
},
};
}
const payments = createPayments(new FakeProvider());
const charge = payments.charge(2500, 'usd', 'tok_visa');
console.log(charge);
console.log(payments.refund(charge.id));{ id: 'ch_1', ok: true }
{ ok: true }Checkout code sees charge and refund with your own names and a simple result. The field names, the casing of the currency and the nesting stay in createPayments.
Pass the provider in so tests use the fake. Do not make the facade a copy of the SDK: add an operation when a caller needs one, and keep provider-specific errors from leaking by translating them into your own.