Debian Codegraph @

addons/payment_mercado_pago/static/src/interactions/payment_form.js

raw
1 /* global MercadoPago */
2
3 import { _t } from '@web/core/l10n/translation';
4 import { loadJS } from '@web/core/assets';
5 import { rpc, RPCError } from '@web/core/network/rpc';
6 import { patch } from '@web/core/utils/patch';
7
8 import { PaymentForm } from '@payment/interactions/payment_form';
9
10 patch(PaymentForm.prototype, {
11
12 setup() {
13 super.setup();
14 this.mercadoPagoBricksBuilder = undefined;
15 this.lastMercadoPagoPMId = undefined; // The last instantiated payment method ID.
16 this.lastMercadoPagoBrick = undefined; // The brick of the last instantiated payment method.
17 },
18
19 // === DOM MANIPULATION === //
20
21 /**
22 * Prepare the inline form of Mercado Pago for direct payment.
23 *
24 * @override method from payment.payment_form
25 * @private
26 * @param {number} providerId - The id of the selected payment option's provider.
27 * @param {string} providerCode - The code of the selected payment option's provider.
28 * @param {number} paymentOptionId - The id of the selected payment option
29 * @param {string} paymentMethodCode - The code of the selected payment method, if any.
30 * @param {string} flow - The online payment flow of the selected payment option
31 * @return {void}
32 */
33 async _prepareInlineForm(providerId, providerCode, paymentOptionId, paymentMethodCode, flow) {
34 if (providerCode !== 'mercado_pago' || paymentMethodCode !== 'card') {
35 await super._prepareInlineForm(...arguments);
36 return;
37 }
38
39 // Check if instantiation of the component is needed.
40 if (flow === 'token') {
41 return; // No component for tokens.
42 } else if (paymentOptionId === this.lastMercadoPagoPMId) {
43 this._setPaymentFlow('direct'); // Overwrite the flow even if no re-instantiation.
44 return; // Don't re-instantiate if already done for this payment method.
45 }
46
47 // Extract and deserialize the inline form values.
48 const radio = document.querySelector('input[name="o_payment_radio"]:checked');
49 const inlineFormValues = JSON.parse(radio.dataset['mercadoPagoInlineFormValues']);
50
51 // Process card payments with the redirect flow if the account is not onboarded with OAuth.
52 const publicKey = inlineFormValues['public_key'];
53 if (!publicKey) { // The account was not onboarded with OAuth and can't support direct flow.
54 return; // Keep the flow to redirect.
55 }
56
57 // Overwrite the flow of the selected payment method.
58 this._setPaymentFlow('direct');
59
60 // Create the bricksBuilder object if not already done for another payment method.
61 if (!this.mercadoPagoBricksBuilder) {
62 await this.waitFor(loadJS('https://sdk.mercadopago.com/js/v2'));
63 const mercadoPago = new MercadoPago(publicKey, { locale: 'en-US' });
64 this.mercadoPagoBricksBuilder = mercadoPago.bricks();
65 }
66
67 // Unmount the previous brick, if any, as only one brick can be mounted at a time.
68 this.lastMercadoPagoBrick?.unmount();
69
70 // Instantiate and mount the card brick.
71 const settings = {
72 initialization: {
73 amount: this.paymentContext['amount'],
74 payer: {
75 email: inlineFormValues['email'],
76 },
77 },
78 customization: {
79 visual: {
80 hideFormTitle: true,
81 hidePaymentButton: true,
82 },
83 paymentMethods: {
84 maxInstallments: 12,
85 },
86 },
87 callbacks: { // Callbacks are required even if empty.
88 onReady: () => {},
89 onError: () => {},
90 },
91 };
92 this.lastMercadoPagoBrick = await this.waitFor(this.mercadoPagoBricksBuilder.create(
93 'cardPayment',
94 `o_mercado_pago_brick_container_${providerId}_${paymentOptionId}`,
95 settings,
96 ));
97 this.lastMercadoPagoPMId = paymentOptionId;
98 },
99
100 /**
101 * Validate the form inputs before initiating the payment flow.
102 *
103 * @override method from @payment/js/payment_form
104 * @private
105 * @param {string} providerCode - The code of the selected payment option's provider.
106 * @param {number} paymentOptionId - The id of the selected payment option.
107 * @param {string} paymentMethodCode - The code of the selected payment method, if any.
108 * @param {string} flow - The payment flow of the selected payment option.
109 * @return {void}
110 */
111 async _initiatePaymentFlow(providerCode, paymentOptionId, paymentMethodCode, flow) {
112 if (providerCode !== 'mercado_pago' || flow !== 'direct') {
113 // Tokens and redirect payment methods are handled by the generic flow.
114 await super._initiatePaymentFlow(...arguments);
115 return;
116 }
117
118 // Trigger form validation.
119 this.mercadoPagoFormData = await this.waitFor(this.lastMercadoPagoBrick.getFormData());
120 if (!this.mercadoPagoFormData){
121 this._displayErrorDialog(_t("Incorrect payment details"));
122 this._enableButton(); // The submit button is disabled at this point, enable it.
123 return;
124 }
125 await super._initiatePaymentFlow(...arguments);
126 },
127
128 /**
129 * Process Mercado Pago's implementation of the direct payment flow.
130 *
131 * @override method from payment.payment_form
132 * @private
133 * @param {string} providerCode - The code of the selected payment option's provider.
134 * @param {number} paymentOptionId - The id of the selected payment option.
135 * @param {string} paymentMethodCode - The code of the selected payment method, if any.
136 * @param {object} processingValues - The processing values of the transaction.
137 * @return {void}
138 */
139 async _processDirectFlow(providerCode, paymentOptionId, paymentMethodCode, processingValues) {
140 if (providerCode !== 'mercado_pago') {
141 super._processDirectFlow(...arguments);
142 return;
143 }
144
145 try {
146 await this.waitFor(rpc('/payment/mercado_pago/payments', {
147 'reference': processingValues.reference,
148 'transaction_amount': processingValues.amount,
149 'token': this.mercadoPagoFormData.token,
150 'installments': this.mercadoPagoFormData.installments,
151 'payment_method_brand': this.mercadoPagoFormData.payment_method_id,
152 'issuer_id': this.mercadoPagoFormData.issuer_id,
153 }));
154 window.location = '/payment/status';
155 } catch(error) {
156 if (error instanceof RPCError) {
157 this._displayErrorDialog(_t("Payment processing failed"), error.data.message);
158 this._enableButton();
159 } else {
160 return Promise.reject(error);
161 }
162 }
163 },
164
165 });