From 90fbb99ed2a744b5f649eb897c80c3eea7755a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 21 Jun 2024 14:32:15 +0200 Subject: [PATCH] Do not update payment if amount or status from Braintree has not changed If payment create clearing moves, this avoid to cancel and recreate account moves unnecessary. --- modules/account_payment_braintree/account.py | 24 ++++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/modules/account_payment_braintree/account.py b/modules/account_payment_braintree/account.py index 01f2b5263a..20f2a3bcf0 100644 --- a/modules/account_payment_braintree/account.py +++ b/modules/account_payment_braintree/account.py @@ -502,8 +502,7 @@ class Payment(CheckoutMixin, BraintreeCustomerMethodMixin, metaclass=PoolMeta): def braintree_update(self, transaction): "Update payment with braintree transaction" - if self.state == 'succeeded': - self.__class__.proceed([self]) + assert transaction.id == self.braintree_transaction_id gateway = transaction.gateway amount = transaction.amount for id_ in transaction.refund_ids: @@ -513,15 +512,20 @@ class Payment(CheckoutMixin, BraintreeCustomerMethodMixin, metaclass=PoolMeta): for dispute in transaction.disputes: if dispute.status in DISPUTE_FINAL_STATUSES: amount -= dispute.amount_disputed - dispute.amount_won - self.amount = amount - self.save() - if transaction.status in SUCCEEDED_STATUSES: - if amount: - self.__class__.succeed([self]) - else: + if (self.state not in {'succeeded', 'failed'} + or self.amount != amount + or (not amount and self.state != 'failed')): + if self.state == 'succeeded': + self.__class__.proceed([self]) + self.amount = amount + self.save() + if transaction.status in SUCCEEDED_STATUSES: + if amount: + self.__class__.succeed([self]) + else: + self.__class__.fail([self]) + elif transaction.status in FAILED_STATUSES: self.__class__.fail([self]) - elif transaction.status in FAILED_STATUSES: - self.__class__.fail([self]) class PaymentBraintreeRefund(Workflow, ModelSQL, ModelView):