class CryptoPaymentGateway {
constructor(api) {
this.api = api;
this.merchants = new Map();
}
// Registrar merchant
async registerMerchant(merchantId, config) {
const wallets = await Promise.all(
config.currencies.map(currency =>
this.api.createWallet(currency, `Merchant ${merchantId} - ${currency}`)
)
);
this.merchants.set(merchantId, {
...config,
wallets: wallets.reduce((acc, wallet) => {
acc[wallet.currency] = wallet;
return acc;
}, {})
});
}
// Processar pagamento
async processPayment(merchantId, amount, currency, customerInfo) {
const merchant = this.merchants.get(merchantId);
if (!merchant) {
throw new Error('Merchant não encontrado');
}
const wallet = merchant.wallets[currency];
if (!wallet) {
throw new Error('Moeda não suportada');
}
// Criar pagamento
const payment = await this.api.createPayment(
wallet.id,
amount,
'BRL'
);
// Notificar merchant
await this.notifyMerchant(merchantId, payment, customerInfo);
return {
paymentId: payment.id,
pixKey: payment.pix_key,
qrCode: payment.qr_code,
expiresAt: payment.expires_at
};
}
// Notificar merchant
async notifyMerchant(merchantId, payment, customerInfo) {
const merchant = this.merchants.get(merchantId);
// Enviar webhook para o merchant
await fetch(merchant.webhookUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
type: 'payment.created',
payment,
customer: customerInfo
})
});
}
}