details:   https://code.tryton.org/tryton/commit/355e3114e142
branch:    default
user:      Cédric Krier <[email protected]>
date:      Fri Aug 15 13:08:08 2025 +0200
description:
        Add support for credit refunds with Shopify

        Closes #14165
diffstat:

 modules/web_shop_shopify/CHANGELOG  |   1 +
 modules/web_shop_shopify/account.py |  30 ++++++++++++++++++++++--------
 2 files changed, 23 insertions(+), 8 deletions(-)

diffs (79 lines):

diff -r 36819c39122c -r 355e3114e142 modules/web_shop_shopify/CHANGELOG
--- a/modules/web_shop_shopify/CHANGELOG        Fri Aug 15 09:11:10 2025 +0200
+++ b/modules/web_shop_shopify/CHANGELOG        Fri Aug 15 13:08:08 2025 +0200
@@ -1,3 +1,4 @@
+* Add support for credit refunds
 
 Version 7.6.0 - 2025-04-28
 --------------------------
diff -r 36819c39122c -r 355e3114e142 modules/web_shop_shopify/account.py
--- a/modules/web_shop_shopify/account.py       Fri Aug 15 09:11:10 2025 +0200
+++ b/modules/web_shop_shopify/account.py       Fri Aug 15 13:08:08 2025 +0200
@@ -33,14 +33,19 @@
 
     @classmethod
     def _get_from_shopify(cls, sale, transaction):
-        assert transaction.kind in {'authorization', 'sale'}
+        assert (
+            transaction.kind in {'authorization', 'sale'}
+            or (transaction.kind == 'refund' and not transaction.parent_id))
         payment = cls(shopify_identifier=transaction.id)
         payment.company = sale.company
         payment.journal = sale.web_shop.get_payment_journal(
             transaction.currency,
             cls._get_shopify_payment_journal_pattern(
                 sale, transaction))
-        payment.kind = 'receivable'
+        if transaction.kind == 'refund':
+            payment.kind = 'payable'
+        else:
+            payment.kind = 'receivable'
         payment.amount = Decimal(transaction.amount)
         payment.origin = sale
         payment.party = sale.party
@@ -57,7 +62,9 @@
         transactions.sort(key=lambda t: kinds.index(t.kind))
         amounts = defaultdict(Decimal)
         for transaction in transactions:
-            if transaction.kind not in {'authorization', 'sale'}:
+            if (transaction.kind not in {'authorization', 'sale'}
+                    and not (transaction.kind == 'refund'
+                        and not transaction.parent_id)):
                 continue
             payments = cls.search([
                     ('shopify_identifier', '=', transaction.id),
@@ -90,13 +97,20 @@
                 payment = id2payments[transaction.parent_id]
                 voided[payment] += Decimal(transaction.amount)
             elif transaction.kind == 'refund':
-                payment = id2payments[transaction.parent_id]
-                captured[payment] -= Decimal(transaction.amount)
+                if not transaction.parent_id:
+                    payment = id2payments[transaction.id]
+                else:
+                    payment = id2payments[transaction.parent_id]
+                    captured[payment] -= Decimal(transaction.amount)
                 refunded[payment] += Decimal(transaction.amount)
 
         to_save = []
         for payment in id2payments.values():
-            if payment.amount != captured[payment]:
+            if payment.kind == 'payable':
+                amount = refunded[payment]
+            else:
+                amount = captured[payment]
+            if payment.amount != amount:
                 payment.amount = captured[payment]
                 to_save.append(payment)
         cls.proceed(to_save)
@@ -104,8 +118,8 @@
 
         to_succeed, to_fail, to_proceed = set(), set(), set()
         for transaction_id, payment in id2payments.items():
-            if amounts[transaction_id] == (
-                    captured[payment] + voided[payment] + refunded[payment]):
+            amount = captured[payment] + voided[payment] + refunded[payment]
+            if amounts[transaction_id] == amount:
                 if payment.amount:
                     if payment.state != 'succeeded':
                         to_succeed.add(payment)

Reply via email to