| 1 |
import { PaymentScreen } from "@point_of_sale/app/screens/payment_screen/payment_screen"; |
| 2 |
import { patch } from "@web/core/utils/patch"; |
| 3 |
import { onMounted } from "@odoo/owl"; |
| 4 |
import { _t } from "@web/core/l10n/translation"; |
| 5 |
|
| 6 |
patch(PaymentScreen.prototype, { |
| 7 |
setup() { |
| 8 |
super.setup(...arguments); |
| 9 |
onMounted(async () => { |
| 10 |
const pendingPaymentLine = this.currentOrder.payment_ids.find( |
| 11 |
(paymentLine) => |
| 12 |
paymentLine.payment_method_id.use_payment_terminal === "razorpay" && |
| 13 |
!paymentLine.isDone() && |
| 14 |
paymentLine.getPaymentStatus() !== "pending" |
| 15 |
); |
| 16 |
if (pendingPaymentLine) { |
| 17 |
const payment_status = |
| 18 |
await pendingPaymentLine.payment_method_id.payment_terminal._waitForPaymentConfirmation(); |
| 19 |
if (payment_status?.status === "AUTHORIZED") { |
| 20 |
pendingPaymentLine.setPaymentStatus("done"); |
| 21 |
} else { |
| 22 |
pendingPaymentLine.setPaymentStatus("force_done"); |
| 23 |
} |
| 24 |
} |
| 25 |
}); |
| 26 |
}, |
| 27 |
async addNewPaymentLine(paymentMethod) { |
| 28 |
if (paymentMethod.use_payment_terminal === "razorpay" && this.isRefundOrder) { |
| 29 |
const refundedOrder = this.currentOrder.lines[0]?.refunded_orderline_id?.order_id; |
| 30 |
if (!refundedOrder) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
const transactionsIds = this.currentOrder.payment_ids.map( |
| 34 |
(pi) => pi.uiState.transaction_id |
| 35 |
); |
| 36 |
const razorpayPaymentline = refundedOrder.payment_ids.find( |
| 37 |
(pi) => |
| 38 |
pi.payment_method_id.use_payment_terminal === "razorpay" && |
| 39 |
!transactionsIds.find((x) => x === pi.transaction_id) |
| 40 |
); |
| 41 |
const currentDue = this.currentOrder.remainingDue; |
| 42 |
if (!razorpayPaymentline || currentDue === 0) { |
| 43 |
this.pos.notification.add( |
| 44 |
_t( |
| 45 |
"Adding a new Razorpay payment line is not allowed under the current conditions." |
| 46 |
), |
| 47 |
{ type: "warning", sticky: false } |
| 48 |
); |
| 49 |
return false; |
| 50 |
} |
| 51 |
const res = await super.addNewPaymentLine(paymentMethod); |
| 52 |
const newPaymentLine = this.paymentLines.at(-1); |
| 53 |
const amountToSet = Math.min( |
| 54 |
Math.abs(newPaymentLine.amount), |
| 55 |
razorpayPaymentline.amount |
| 56 |
); |
| 57 |
if (res) { |
| 58 |
newPaymentLine.setAmount(-amountToSet); |
| 59 |
newPaymentLine.updateRefundPaymentLine(razorpayPaymentline); |
| 60 |
} |
| 61 |
return res; |
| 62 |
} else { |
| 63 |
return await super.addNewPaymentLine(paymentMethod); |
| 64 |
} |
| 65 |
}, |
| 66 |
}); |