UNPKG

@malga-checkout/core

Version:
58 lines (57 loc) 1.91 kB
import { v4 as uuid } from 'uuid'; import settings from '../../stores/settings'; import payment from '../../stores/payment'; import { Charges } from '../../services/charges'; import { t } from '@malga-checkout/i18n'; import { NuPay } from '../../providers/nupay'; export class MalgaPaymentsNuPayService { constructor({ onPaymentSuccess, onPaymentFailed, onShowDialog, data, }) { this.charge = new Charges({ provider: new NuPay({ nupay: data }) }); this.data = data; this.onPaymentSuccess = onPaymentSuccess; this.onPaymentFailed = onPaymentFailed; this.onShowDialog = onShowDialog; } handlePaymentSuccess(data) { var _a, _b; const paymentUrl = (_b = (_a = data.transactionRequests[0]) === null || _a === void 0 ? void 0 : _a.nupay) === null || _b === void 0 ? void 0 : _b.paymentUrl; if (!!paymentUrl) { payment.paymentUrl = 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.nupay.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, }); } } }