diff --git a/pos_payment_method_cashdro_rounding/__init__.py b/pos_payment_method_cashdro_rounding/__init__.py new file mode 100644 index 0000000..9f3f569 --- /dev/null +++ b/pos_payment_method_cashdro_rounding/__init__.py @@ -0,0 +1,2 @@ +# Copyright Criptomart +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). diff --git a/pos_payment_method_cashdro_rounding/__manifest__.py b/pos_payment_method_cashdro_rounding/__manifest__.py new file mode 100644 index 0000000..ac4f583 --- /dev/null +++ b/pos_payment_method_cashdro_rounding/__manifest__.py @@ -0,0 +1,18 @@ +{ + "name": "POS CashDro Rounding", + "version": "17.0.1.0.0", + "summary": "Apply cash/currency rounding to CashDro payment request amount.", + "category": "Point of Sale", + "license": "AGPL-3", + "author": "Criptomart", + "depends": [ + "point_of_sale", + "pos_payment_method_cashdro", + ], + "assets": { + "point_of_sale._assets_pos": [ + "pos_payment_method_cashdro_rounding/static/src/js/cashdro_rounding.esm.js", + ], + }, + "installable": True, +} diff --git a/pos_payment_method_cashdro_rounding/static/src/js/cashdro_rounding.esm.js b/pos_payment_method_cashdro_rounding/static/src/js/cashdro_rounding.esm.js new file mode 100644 index 0000000..89d7dd3 --- /dev/null +++ b/pos_payment_method_cashdro_rounding/static/src/js/cashdro_rounding.esm.js @@ -0,0 +1,68 @@ +/** @odoo-module */ +/* Copyright Criptomart + License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + + Override cashdro_send_payment_request to apply POS currency/cash rounding + before sending amount to CashDro, keeping OCA integration flow. +*/ +import {PaymentCashdro} from "@pos_payment_method_cashdro/js/payment_cashdro.esm"; +import {patch} from "@web/core/utils/patch"; +import {_t} from "@web/core/l10n/translation"; +import {ErrorPopup} from "@point_of_sale/app/errors/popups/error_popup"; + +patch(PaymentCashdro.prototype, { + async cashdro_send_payment_request(order) { + // The payment is done in three concatenated steps: + // 1. The POS send a payment request, to which the Cashdro respondes + // with an operation id. + // 2. Then the POS has to acknowledge that such operation id has + // been received. + // 3. Once acknowledged the POS has to send a payment request to the + // cashdro. Once the Cashdro responses with a "F" state (for + // finished) we'll get the response and fill the tendered money + // for the payment line. + const payment_line = order.selected_paymentline; + try { + // Cashdro treats decimals as positions in an integer we also have + // to deal with floating point computing to avoid decimals at the + // end or the drawer will reject our request. + // Add the rounding applied to the due amount to get the correct + // amount to send to the cashdro when rounding is configured. + const amount = Math.round( + (order.get_due(payment_line) + order.get_rounding_applied()) * 100 + ); + console.log("CashDro amount with rounding: " + amount); + const res = await this._cashdro_request( + this._cashdro_payment_url({amount: amount}) + ); + // It comes handy to log the response from the drawer, as + // we can diagnose the right symptoms for each issue + console.log(res); + const operation_id = res.data || ""; + this.pos.get_order().cashdro_operation = operation_id; + // Acknowledge the operation + var ack_url = this._cashdro_ack_url(operation_id); + const res_ack = await this._cashdro_request(ack_url); + // Validate the operation + console.log(res_ack); + var ask_url = this._cashdro_ask_url(operation_id); + const operation_data = await this._cashdro_request_payment(ask_url); + // This might be too verbose, but it helps a lot to diagnose issues and + // their reasons. + console.log(operation_data); + var data = JSON.parse(operation_data.data); + payment_line.cashdro_operation_data = data; + var tendered = data.operation.totalin / 100; + payment_line.set_amount(tendered); + } catch (error) { + // We want to be able to retry after any error. + payment_line.set_payment_status("retry"); + this.env.services.popup.add(ErrorPopup, { + title: _t("Error"), + body: _t("An error occurred while connecting to the cashdro."), + }); + return false; + } + return true; + }, +});