UNPKG

@malga-checkout/core

Version:
54 lines (53 loc) 1.71 kB
import { v4 as uuid } from 'uuid'; import settings from '../../stores/settings'; import payment from '../../stores/payment'; import { Drip } from '../../providers/drip'; import { Charges } from '../../services/charges'; import { t } from '@malga-checkout/i18n'; export class MalgaPaymentsDripService { constructor({ onPaymentSuccess, onPaymentFailed, onShowDialog, data, }) { this.charge = new Charges({ provider: new Drip({ drip: data }) }); this.data = data; this.onPaymentSuccess = onPaymentSuccess; this.onPaymentFailed = onPaymentFailed; this.onShowDialog = onShowDialog; } handlePaymentSuccess(data) { payment.paymentUrl = data.paymentMethod.paymentUrl; this.onPaymentSuccess(data); } handlePaymentFailed(error) { if (settings.automaticallyGeneratedIdempotencyKey) { settings.idempotencyKey = uuid(); } if (settings.dialogConfig.show) { this.onShowDialog({ open: true, mode: 'error', errorMessage: t('dialogs.drip.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, }); } } }