| 1 |
import { _t } from "@web/core/l10n/translation"; |
| 2 |
import { PaymentInterface } from "@point_of_sale/app/utils/payment/payment_interface"; |
| 3 |
import { AlertDialog } from "@web/core/confirmation_dialog/confirmation_dialog"; |
| 4 |
import { register_payment_method } from "@point_of_sale/app/services/pos_store"; |
| 5 |
|
| 6 |
export class PaymentMercadoPago extends PaymentInterface { |
| 7 |
async createPaymentIntent() { |
| 8 |
const order = this.pos.getOrder(); |
| 9 |
const line = order.getSelectedPaymentline(); |
| 10 |
// Build informations for creating a payment intend on Mercado Pago. |
| 11 |
// Data in "external_reference" are send back with the webhook notification |
| 12 |
const infos = { |
| 13 |
amount: parseInt(line.amount * 100, 10), |
| 14 |
additional_info: { |
| 15 |
external_reference: `${this.pos.config.current_session_id.id}_${line.payment_method_id.id}_${order.uuid}`, |
| 16 |
print_on_terminal: true, |
| 17 |
}, |
| 18 |
}; |
| 19 |
// mp_payment_intent_create will call the Mercado Pago api |
| 20 |
return await this.env.services.orm.silent.call( |
| 21 |
"pos.payment.method", |
| 22 |
"mp_payment_intent_create", |
| 23 |
[[line.payment_method_id.id], infos] |
| 24 |
); |
| 25 |
} |
| 26 |
async getLastStatusPaymentIntent() { |
| 27 |
const line = this.pos.getOrder().getSelectedPaymentline(); |
| 28 |
// mp_payment_intent_get will call the Mercado Pago api |
| 29 |
return await this.env.services.orm.silent.call( |
| 30 |
"pos.payment.method", |
| 31 |
"mp_payment_intent_get", |
| 32 |
[[line.payment_method_id.id], this.payment_intent.id] |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
async cancelPaymentIntent() { |
| 37 |
const line = this.pos.getOrder().getSelectedPaymentline(); |
| 38 |
// mp_payment_intent_cancel will call the Mercado Pago api |
| 39 |
return await this.env.services.orm.silent.call( |
| 40 |
"pos.payment.method", |
| 41 |
"mp_payment_intent_cancel", |
| 42 |
[[line.payment_method_id.id], this.payment_intent.id] |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
async getPayment(payment_id) { |
| 47 |
const line = this.pos.getOrder().getSelectedPaymentline(); |
| 48 |
// mp_get_payment_status will call the Mercado Pago api |
| 49 |
return await this.env.services.orm.silent.call( |
| 50 |
"pos.payment.method", |
| 51 |
"mp_get_payment_status", |
| 52 |
[[line.payment_method_id.id], payment_id] |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
setup() { |
| 57 |
super.setup(...arguments); |
| 58 |
this.webhook_resolver = null; |
| 59 |
this.payment_intent = {}; |
| 60 |
} |
| 61 |
|
| 62 |
async sendPaymentRequest(cid) { |
| 63 |
await super.sendPaymentRequest(...arguments); |
| 64 |
const line = this.pos.getOrder().getSelectedPaymentline(); |
| 65 |
try { |
| 66 |
// During payment creation, user can't cancel the payment intent |
| 67 |
line.setPaymentStatus("waitingCapture"); |
| 68 |
// Call Mercado Pago to create a payment intent |
| 69 |
const payment_intent = await this.createPaymentIntent(); |
| 70 |
if (!("id" in payment_intent)) { |
| 71 |
this._showMsg(payment_intent.message, "error"); |
| 72 |
return false; |
| 73 |
} |
| 74 |
// Payment intent creation successfull, save it |
| 75 |
this.payment_intent = payment_intent; |
| 76 |
// After payment creation, make the payment intent canceling possible |
| 77 |
line.setPaymentStatus("waitingCard"); |
| 78 |
// Wait for payment intent status change and return status result |
| 79 |
return await new Promise((resolve) => { |
| 80 |
this.webhook_resolver = resolve; |
| 81 |
}); |
| 82 |
} catch (error) { |
| 83 |
this._showMsg(error, "System error"); |
| 84 |
return false; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
async sendPaymentCancel(order, cid) { |
| 89 |
await super.sendPaymentCancel(order, cid); |
| 90 |
if (!("id" in this.payment_intent)) { |
| 91 |
return true; |
| 92 |
} |
| 93 |
const canceling_status = await this.cancelPaymentIntent(); |
| 94 |
if ("error" in canceling_status) { |
| 95 |
const message = |
| 96 |
canceling_status.status === 409 |
| 97 |
? _t("Payment has to be canceled on terminal") |
| 98 |
: _t("Payment not found (canceled/finished on terminal)"); |
| 99 |
this._showMsg(message, "info"); |
| 100 |
return canceling_status.status !== 409; |
| 101 |
} |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
async handleMercadoPagoWebhook() { |
| 106 |
const line = this.pos.getOrder().getSelectedPaymentline(); |
| 107 |
const MAX_RETRY = 5; // Maximum number of retries for the "ON_TERMINAL" BUG |
| 108 |
const RETRY_DELAY = 1000; // Delay between retries in milliseconds for the "ON_TERMINAL" BUG |
| 109 |
|
| 110 |
const showMessageAndResolve = (messageKey, status, resolverValue) => { |
| 111 |
if (!resolverValue) { |
| 112 |
this._showMsg(messageKey, status); |
| 113 |
} |
| 114 |
line.setPaymentStatus("done"); |
| 115 |
this.webhook_resolver?.(resolverValue); |
| 116 |
return resolverValue; |
| 117 |
}; |
| 118 |
|
| 119 |
const handleFinishedPayment = async (paymentIntent) => { |
| 120 |
if (paymentIntent.state === "CANCELED") { |
| 121 |
return showMessageAndResolve(_t("Payment has been canceled"), "info", false); |
| 122 |
} |
| 123 |
if (["FINISHED", "PROCESSED"].includes(paymentIntent.state)) { |
| 124 |
const payment = await this.getPayment(paymentIntent.payment.id); |
| 125 |
if (payment.status === "approved") { |
| 126 |
return showMessageAndResolve(_t("Payment has been processed"), "info", true); |
| 127 |
} |
| 128 |
return showMessageAndResolve(_t("Payment has been rejected"), "info", false); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
// No payment intent id means either that the user reload the page or |
| 133 |
// it is an old webhook -> trash |
| 134 |
if ("id" in this.payment_intent) { |
| 135 |
// Call Mercado Pago to get the payment intent status |
| 136 |
let last_status_payment_intent = await this.getLastStatusPaymentIntent(); |
| 137 |
// Bad payment intent id, then it's an old webhook not related with the |
| 138 |
// current payment intent -> trash |
| 139 |
if (this.payment_intent.id == last_status_payment_intent.id) { |
| 140 |
if ( |
| 141 |
["FINISHED", "PROCESSED", "CANCELED"].includes(last_status_payment_intent.state) |
| 142 |
) { |
| 143 |
return await handleFinishedPayment(last_status_payment_intent); |
| 144 |
} |
| 145 |
// BUG Sometimes the Mercado Pago webhook return ON_TERMINAL |
| 146 |
// instead of CANCELED/FINISHED when we requested a payment status |
| 147 |
// that was actually canceled/finished by the user on the terminal. |
| 148 |
// Then the strategy here is to ask Mercado Pago MAX_RETRY times the |
| 149 |
// payment intent status, hoping going out of this status |
| 150 |
if ( |
| 151 |
["OPEN", "ON_TERMINAL", "PROCESSING"].includes(last_status_payment_intent.state) |
| 152 |
) { |
| 153 |
return await new Promise((resolve) => { |
| 154 |
let retry_cnt = 0; |
| 155 |
const s = setInterval(async () => { |
| 156 |
last_status_payment_intent = await this.getLastStatusPaymentIntent(); |
| 157 |
if ( |
| 158 |
["FINISHED", "PROCESSED", "CANCELED"].includes( |
| 159 |
last_status_payment_intent.state |
| 160 |
) |
| 161 |
) { |
| 162 |
clearInterval(s); |
| 163 |
resolve(await handleFinishedPayment(last_status_payment_intent)); |
| 164 |
} |
| 165 |
retry_cnt += 1; |
| 166 |
if (retry_cnt >= MAX_RETRY) { |
| 167 |
clearInterval(s); |
| 168 |
resolve( |
| 169 |
showMessageAndResolve( |
| 170 |
_t("Payment status could not be confirmed"), |
| 171 |
"error", |
| 172 |
false |
| 173 |
) |
| 174 |
); |
| 175 |
} |
| 176 |
}, RETRY_DELAY); |
| 177 |
}); |
| 178 |
} |
| 179 |
// If the state does not match any of the expected values |
| 180 |
return showMessageAndResolve(_t("Unknown payment status"), "error", false); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// private methods |
| 186 |
_showMsg(msg, title) { |
| 187 |
this.env.services.dialog.add(AlertDialog, { |
| 188 |
title: "Mercado Pago " + title, |
| 189 |
body: msg, |
| 190 |
}); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
register_payment_method("mercado_pago", PaymentMercadoPago); |