UNPKG

@malga-checkout/core

Version:
63 lines (62 loc) 2 kB
import { v4 as uuid } from 'uuid'; import { Boleto } from '../../providers/boleto'; import { Charges } from '../../services/charges'; import settings from '../../stores/settings'; import { t } from '@malga-checkout/i18n'; export class MalgaPaymentsBoletoService { constructor({ onPaymentSuccess, onPaymentFailed, onShowDialog, data, }) { this.charge = new Charges({ provider: new Boleto({ boleto: data }) }); this.data = data; this.onPaymentSuccess = onPaymentSuccess; this.onPaymentFailed = onPaymentFailed; this.onShowDialog = onShowDialog; } handlePaymentSuccess(boletoData) { const paymentMethod = boletoData.paymentMethod; if (settings.dialogConfig.show) { this.onShowDialog({ mode: 'boleto', amount: boletoData.amount, open: true, paymentCode: paymentMethod.barcodeData, paymentImageUrl: paymentMethod.barcodeImageUrl, expirationDate: paymentMethod.expiresDate, }); } this.onPaymentSuccess(boletoData); } handlePaymentFailed(error) { if (settings.automaticallyGeneratedIdempotencyKey) { settings.idempotencyKey = uuid(); } if (settings.dialogConfig.show) { this.onShowDialog({ open: true, mode: 'error', errorMessage: t('dialogs.boleto.errorMessage', settings.locale), }); } this.onPaymentFailed(error); } async pay() { try { const checkoutResponse = await this.charge.create(); if (checkoutResponse.hasError) { this.handlePaymentFailed({ type: checkoutResponse.data.status, message: 'Your transaction cannot be completed', errorStack: checkoutResponse.data, }); return; } this.handlePaymentSuccess(checkoutResponse.data); } catch (error) { this.handlePaymentFailed({ type: error.response.status, message: 'Your transaction cannot be completed', errorStack: error.response.data, }); } } }