| 1 |
import { browser } from "@web/core/browser/browser"; |
| 2 |
import { _t } from "@web/core/l10n/translation"; |
| 3 |
import { AlertDialog } from "@web/core/confirmation_dialog/confirmation_dialog"; |
| 4 |
import { PaymentInterface } from "@point_of_sale/app/utils/payment/payment_interface"; |
| 5 |
import { register_payment_method } from "@point_of_sale/app/services/pos_store"; |
| 6 |
|
| 7 |
export class PaymentMollie extends PaymentInterface { |
| 8 |
setup() { |
| 9 |
super.setup(...arguments); |
| 10 |
this.paymentLineResolvers = {}; |
| 11 |
} |
| 12 |
|
| 13 |
async sendPaymentRequest(uuid) { |
| 14 |
const paymentLine = this.pos.getOrder().getPaymentlineByUuid(uuid); |
| 15 |
if (paymentLine.amount < 0) { |
| 16 |
const originalPaymentId = this._findOriginalPaymentId(paymentLine); |
| 17 |
if (!originalPaymentId) { |
| 18 |
this._showMollieError( |
| 19 |
_t("You can only refund an order that was paid for with Mollie.") |
| 20 |
); |
| 21 |
return false; |
| 22 |
} |
| 23 |
|
| 24 |
return this._createMollieRefund(paymentLine, originalPaymentId); |
| 25 |
} |
| 26 |
|
| 27 |
return this._createMolliePayment(paymentLine); |
| 28 |
} |
| 29 |
|
| 30 |
async sendPaymentCancel(order, uuid) { |
| 31 |
const paymentLine = this.pos.getOrder().getPaymentlineByUuid(uuid); |
| 32 |
try { |
| 33 |
await this.pos.data.call("pos.payment.method", "mollie_cancel_payment", [ |
| 34 |
this.payment_method_id.id, |
| 35 |
paymentLine.transaction_id, |
| 36 |
]); |
| 37 |
return true; |
| 38 |
} catch (error) { |
| 39 |
this._showMollieError(error); |
| 40 |
return false; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
async _createMolliePayment(paymentLine) { |
| 45 |
try { |
| 46 |
const data = await this.pos.data.call("pos.payment.method", "mollie_create_payment", [ |
| 47 |
this.payment_method_id.id, |
| 48 |
paymentLine.amount, |
| 49 |
paymentLine.uuid, |
| 50 |
this.pos.session.id, |
| 51 |
]); |
| 52 |
if (!["open", "pending"].includes(data.status)) { |
| 53 |
this._showMollieError(_t("Failed to initiate payment: %s", data.status)); |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
// In test mode, the terminal is simulated using a popup |
| 58 |
if (data._links.changePaymentState) { |
| 59 |
browser.open(data._links.changePaymentState.href, "_blank"); |
| 60 |
} |
| 61 |
|
| 62 |
paymentLine.transaction_id = data.id; |
| 63 |
await this.pos.data.synchronizeLocalDataInIndexedDB(); |
| 64 |
|
| 65 |
const { promise, resolve } = Promise.withResolvers(); |
| 66 |
this.paymentLineResolvers[paymentLine.uuid] = resolve; |
| 67 |
|
| 68 |
return promise; |
| 69 |
} catch (error) { |
| 70 |
this._showMollieError(error); |
| 71 |
return false; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
async _createMollieRefund(refundPaymentLine, originalPaymentId) { |
| 76 |
try { |
| 77 |
const data = await this.pos.data.call("pos.payment.method", "mollie_create_refund", [ |
| 78 |
this.payment_method_id.id, |
| 79 |
originalPaymentId, |
| 80 |
Math.abs(refundPaymentLine.amount), |
| 81 |
refundPaymentLine.uuid, |
| 82 |
this.pos.session.id, |
| 83 |
]); |
| 84 |
|
| 85 |
if (!["queued", "pending"].includes(data.status)) { |
| 86 |
this._showMollieError(_t("Failed to initiate refund: %s", data.status)); |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
refundPaymentLine.transaction_id = data.id; |
| 91 |
return true; |
| 92 |
} catch (error) { |
| 93 |
this._showMollieError(error); |
| 94 |
return false; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
_findOriginalPaymentId(refundPaymentLine) { |
| 99 |
const currentOrder = refundPaymentLine.pos_order_id; |
| 100 |
const orderToRefund = currentOrder.lines[0]?.refunded_orderline_id?.order_id; |
| 101 |
if (!orderToRefund) { |
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
const amountDue = Math.abs(currentOrder.remainingDue); |
| 106 |
const matchedPaymentLine = orderToRefund.payment_ids.find( |
| 107 |
(line) => |
| 108 |
line.payment_method_id.use_payment_terminal === "mollie" && line.amount <= amountDue |
| 109 |
); |
| 110 |
|
| 111 |
return matchedPaymentLine?.transaction_id ?? null; |
| 112 |
} |
| 113 |
|
| 114 |
handleMollieStatusResponse(paymentLine, notification) { |
| 115 |
const isSuccessful = notification.status === "paid"; |
| 116 |
|
| 117 |
if (isSuccessful) { |
| 118 |
paymentLine.card_no = notification.card_no; |
| 119 |
paymentLine.card_type = notification.card_type; |
| 120 |
paymentLine.card_brand = notification.card_brand; |
| 121 |
} |
| 122 |
|
| 123 |
if (notification.status === "failed") { |
| 124 |
this._showMollieError( |
| 125 |
notification.status_reason?.message ?? |
| 126 |
_t("The payment failed for an unknown reason.") |
| 127 |
); |
| 128 |
} |
| 129 |
if (notification.status === "expired") { |
| 130 |
this._showMollieError(_t("The payment has timed out.")); |
| 131 |
} |
| 132 |
|
| 133 |
const resolver = this.paymentLineResolvers?.[paymentLine.uuid]; |
| 134 |
if (resolver) { |
| 135 |
this.paymentLineResolvers[paymentLine.uuid] = null; |
| 136 |
resolver(isSuccessful); |
| 137 |
} else { |
| 138 |
paymentLine.handlePaymentResponse(isSuccessful); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
_extractErrorMessage(error) { |
| 143 |
if (typeof error === "string") { |
| 144 |
return error; |
| 145 |
} |
| 146 |
if (error.name === "RPC_ERROR") { |
| 147 |
return error.data.message; |
| 148 |
} |
| 149 |
return error.message; |
| 150 |
} |
| 151 |
|
| 152 |
_showMollieError(error) { |
| 153 |
this.env.services.dialog.add(AlertDialog, { |
| 154 |
title: _t("Mollie Error"), |
| 155 |
body: this._extractErrorMessage(error), |
| 156 |
}); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
register_payment_method("mollie", PaymentMollie); |