UNPKG

@malga-checkout/core

Version:
138 lines (137 loc) 5.38 kB
import settings from '../../stores/settings'; import credit from '../../stores/credit'; import payment from '../../stores/payment'; import dialog from '../../stores/dialog'; import { MalgaPaymentsBoletoService } from '../malga-payments-boleto/malga-payments-boleto.service'; import { MalgaPaymentsCreditService } from '../malga-payments-credit/malga-payments-credit.service'; import { MalgaPaymentsPixService } from '../malga-payments-pix/malga-payments-pix.service'; import { MalgaPaymentsDripService } from '../malga-payments-drip/malga-payments-drip.service'; import { MalgaPaymentsSessionService } from '../malga-payments-session/malga-payments-session.service'; import { Customers } from '../../services/customers'; import { MalgaPaymentsNuPayService } from '../malga-payments-nupay/malga-payments-nupay.service'; export class MalgaCheckoutService { constructor({ onPaymentSuccess, onPaymentFailed }) { this.handleCreditPaymentData = () => { var _a, _b, _c, _d, _e, _f; const isShowingInstallmentSelector = (_b = (_a = settings.paymentMethods.credit) === null || _a === void 0 ? void 0 : _a.installments) === null || _b === void 0 ? void 0 : _b.show; if (payment.isSelectedSavedCard) { const installments = isShowingInstallmentSelector ? payment.installments : (_d = (_c = settings.paymentMethods.credit) === null || _c === void 0 ? void 0 : _c.installments) === null || _d === void 0 ? void 0 : _d.quantity; return { cardId: payment.cardId, cardCvv: payment.cvv, installments, }; } const installments = isShowingInstallmentSelector ? credit.form.installments : (_f = (_e = settings.paymentMethods.credit) === null || _e === void 0 ? void 0 : _e.installments) === null || _f === void 0 ? void 0 : _f.quantity; return Object.assign(Object.assign({}, credit.form), { installments }); }; this.handlePaymentData = () => { const credit = this.handleCreditPaymentData(); const paymentMethodsData = { nupay: settings.paymentMethods.nupay, drip: settings.paymentMethods.drip, pix: settings.paymentMethods.pix, boleto: settings.paymentMethods.boleto, credit, }; return (paymentMethodsData[payment.selectedPaymentMethod] || paymentMethodsData.credit); }; this.onPaymentSuccess = onPaymentSuccess; this.onPaymentFailed = onPaymentFailed; } handlePaymentMethod() { const paymentMethods = { drip: MalgaPaymentsDripService, pix: MalgaPaymentsPixService, credit: MalgaPaymentsCreditService, boleto: MalgaPaymentsBoletoService, nupay: MalgaPaymentsNuPayService, }; return (paymentMethods[payment.selectedPaymentMethod] || paymentMethods.credit); } async handleSession(sessionId) { if (!sessionId) { settings.hiddenPoweredByMalga = false; return; } const sessionService = new MalgaPaymentsSessionService({ onShowDialog: this.handleShowDialog, }); return sessionService.findSession(sessionId); } async handleCustomerId(customerId) { if (!customerId) return; const customerService = new Customers(); const { data: customer } = await customerService.find(customerId); const hasCustomerAddress = Object.values(customer.address || {}).some((value) => value); const address = hasCustomerAddress ? customer.address : null; settings.transactionConfig = Object.assign(Object.assign({}, settings.transactionConfig), { fraudAnalysis: Object.assign({ customer: { name: customer.name, email: customer.email, phoneNumber: (customer === null || customer === void 0 ? void 0 : customer.phoneNumber) || "", document: { number: customer.document.number, type: customer.document.type, country: customer.document.country, }, address, } }, settings.transactionConfig.fraudAnalysis) }); } handleShowDialog(dialogConfigs) { const initialDialogConfigs = { pix: { open: false, mode: 'pix', amount: 0, paymentCode: '', paymentImageUrl: '', expirationDate: '', expirationTime: 3600, }, drip: { open: false, mode: 'success', amount: 0, }, credit: { open: false, mode: 'success', amount: 0, }, boleto: { open: false, mode: 'boleto', amount: 0, paymentCode: '', paymentImageUrl: '', expirationDate: '', }, nupay: { open: false, mode: 'success', amount: 0, }, }; const currenInitialDialogConfigs = payment.isSelectedSavedCard ? initialDialogConfigs.credit : initialDialogConfigs[payment.selectedPaymentMethod]; dialog.configs = Object.assign(Object.assign({}, currenInitialDialogConfigs), dialogConfigs); } async pay() { const PaymentMethodClass = this.handlePaymentMethod(); const paymentMethodData = this.handlePaymentData(); const paymentMethod = new PaymentMethodClass({ data: paymentMethodData, onPaymentSuccess: this.onPaymentSuccess, onPaymentFailed: this.onPaymentFailed, onShowDialog: this.handleShowDialog, }); await paymentMethod.pay(); } }