Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix order.totalRefunded for fully-refunded payments (#17632)

Maciej Korycinski6da9682b3d50
diff --git a/saleor/graphql/order/tests/queries/test_order_amounts.py b/saleor/graphql/order/tests/queries/test_order_amounts.py
index 1d702ab667..80a28a7a33 100644
--- a/saleor/graphql/order/tests/queries/test_order_amounts.py
+++ b/saleor/graphql/order/tests/queries/test_order_amounts.py
@@ -2,6 +2,8 @@ from decimal import Decimal
import pytest
+from saleor.core.prices import quantize_price
+
from ....core.utils import to_global_id_or_none
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -236,7 +238,7 @@ def test_order_total_refunded_query_with_payment_by_staff_user(
assert total_refunded["amount"] == first_refund_amount + second_refund_amount
-def test_order_total_refunded_query_with_payment_by_app(
+def test_order_total_refunded_query_with_partially_refunded_payment_by_app(
app_api_client, permission_manage_orders, order_with_lines, payment_txn_refunded
):
# given
@@ -264,6 +266,29 @@ def test_order_total_refunded_query_with_payment_by_app(
assert total_refunded["amount"] == first_refund_amount + second_refund_amount
+def test_order_total_refunded_query_with_fully_refunded_payment_by_app(
+ app_api_client, permission_manage_orders, order_with_lines, payment_txn_refunded
+):
+ # given
+ payment = payment_txn_refunded
+ assert not payment.is_active
+ refund_transaction = payment.transactions.first()
+ assert refund_transaction.amount == payment.total
+
+ app_api_client.app.permissions.set([permission_manage_orders])
+
+ # when
+ response = app_api_client.post_graphql(ORDERS_QUERY_WITH_AMOUNT_FIELDS)
+ content = get_graphql_content(response)
+
+ # then
+ order_data = content["data"]["orders"]["edges"][0]["node"]
+ total_refunded = order_data["totalRefunded"]
+ assert quantize_price(
+ Decimal(total_refunded["amount"]), payment.currency
+ ) == quantize_price(payment.total, payment.currency)
+
+
def test_order_total_refund_pending_query_with_transactions_by_staff_user(
staff_api_client,
permission_group_manage_orders,
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 6b91140a7f..e6be80a475 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -2805,7 +2805,13 @@ class Order(SyncWebhookControlContextModelObjectType[ModelObjectType[models.Orde
def _resolve_total_refund(data):
payments, transactions = data
last_payment = get_last_payment(payments)
- if last_payment and last_payment.is_active:
+ payment_is_active = last_payment and last_payment.is_active
+ payment_is_fully_refunded = (
+ last_payment
+ and last_payment.charge_status == ChargeStatus.FULLY_REFUNDED
+ )
+
+ if payment_is_active or payment_is_fully_refunded:
return (
TransactionByPaymentIdLoader(info.context)
.load(last_payment.id)