From 80dcf62086fc6b48bfbb7ac617f1c76f9c901eea Mon Sep 17 00:00:00 2001 From: "Anh Thao Pham (pta)" Date: Wed, 25 Oct 2023 09:10:02 +0000 Subject: [PATCH] [FIX] account_payment: fix failed payment transaction from invoice Steps to reproduce: - Configure a payment provider (e.g. Stripe) - Go to the portal account of an user (e.g. Marc Demo) - Click on "Manage payment methods" - Save a payment method to create a payment token - /!\ Go back to payment provider configuration and make a typo in the credentials (This will be needed later to trigger a payment transaction error) - Go to Accounting and create an invoice for Marc Demo - Confirm the invoice - Register payment: * Payment Method: Stripe * Saved payment token: [payment token of Marc Demo] - Create payment [1st issue] The payment transaction fails (as the credentials are incorrect), but no information about it appears on the invoice. [2nd issue] It is not possible to register another payment (with the payment provider) because there is an unique constraint on the reference field of "payment.transaction" model and each attempt of registering a payment uses the same reference. Solution: [1st issue] Link invoice to payment transaction. Messages related to payment transaction will be logged on the invoice automatically. [2nd issue] Generate reference with "_compute_reference" method instead of always using the same one. opw-3499200 closes odoo/odoo#140533 X-original-commit: 8c98efe036972c43a95c28509e91c356ddc84084 Signed-off-by: Antoine Vandevenne (anv) Signed-off-by: Anh Thao Pham (pta) --- addons/account_payment/models/account_payment.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/addons/account_payment/models/account_payment.py b/addons/account_payment/models/account_payment.py index 7df7492dad0..cc84b61f5ff 100644 --- a/addons/account_payment/models/account_payment.py +++ b/addons/account_payment/models/account_payment.py @@ -197,13 +197,18 @@ class AccountPayment(models.Model): return { 'provider_id': self.payment_token_id.provider_id.id, 'payment_method_id': self.payment_token_id.payment_method_id.id, - 'reference': self.ref, + 'reference': self.env['payment.transaction']._compute_reference( + self.payment_token_id.provider_id.code, prefix=self.ref + ), 'amount': self.amount, 'currency_id': self.currency_id.id, 'partner_id': self.partner_id.id, 'token_id': self.payment_token_id.id, 'operation': 'offline', 'payment_id': self.id, + **({'invoice_ids': [Command.set(self._context.get('active_ids', []))]} + if self._context.get('active_model') == 'account.move' + else {}), **extra_create_values, }