Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Raise ValidationError when refund cannot be performed (#7260)
Iga Karbowiak6fd4aa555213
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3e328367e1..6206d0e63a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -69,6 +69,7 @@ All notable, unreleased changes to this project will be documented in this file.- Use GraphQL IDs instead of database IDs in export - #7240 by @IKarbowiak- Fix draft order tax mismatch - #7226 by @IKarbowiak- Introduce `calculate_order_line_total` plugin method+- Raise ValidationError when refund cannot be performed - #7260 by @IKarbowiak### Breaking- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysockidiff --git a/saleor/graphql/order/tests/test_fulfillment_refund_products.py b/saleor/graphql/order/tests/test_fulfillment_refund_products.pyindex 5924f6eb19..73a6593843 100644--- a/saleor/graphql/order/tests/test_fulfillment_refund_products.py+++ b/saleor/graphql/order/tests/test_fulfillment_refund_products.py@@ -7,7 +7,7 @@ from prices import Money, TaxedMoneyfrom ....core.prices import quantize_pricefrom ....order.error_codes import OrderErrorCodefrom ....order.models import FulfillmentStatus-from ....payment import ChargeStatus+from ....payment import ChargeStatus, PaymentErrorfrom ....warehouse.models import Stockfrom ...tests.utils import get_graphql_content@@ -85,6 +85,40 @@ def test_fulfillment_refund_products_amount_and_shipping_costs()+@patch("saleor.order.actions.gateway.refund")+def test_fulfillment_refund_products_refund_raising_payment_error(+ mocked_refund,+ staff_api_client,+ permission_manage_orders,+ fulfilled_order,+ payment_dummy,+):+ # given+ mocked_refund.side_effect = PaymentError("Error")++ payment_dummy.captured_amount = payment_dummy.total+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED+ payment_dummy.save()+ fulfilled_order.payments.add(payment_dummy)+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)+ amount_to_refund = Decimal("11.00")+ variables = {+ "order": order_id,+ "input": {"amountToRefund": amount_to_refund, "includeShippingCosts": True},+ }+ staff_api_client.user.user_permissions.add(permission_manage_orders)++ # when+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)++ # then+ content = get_graphql_content(response)+ data = content["data"]["orderFulfillmentRefundProducts"]+ errors = data["errors"]+ assert len(errors) == 1+ assert errors[0]["code"] == OrderErrorCode.CANNOT_REFUND.name++@patch("saleor.order.actions.gateway.refund")def test_fulfillment_refund_products_order_lines(mocked_refund,diff --git a/saleor/graphql/order/tests/test_fulfillment_return_products.py b/saleor/graphql/order/tests/test_fulfillment_return_products.pyindex b5f93c46fa..1ec10d71c0 100644--- a/saleor/graphql/order/tests/test_fulfillment_return_products.py+++ b/saleor/graphql/order/tests/test_fulfillment_return_products.py@@ -8,7 +8,7 @@ from ....core.prices import quantize_pricefrom ....order import OrderStatusfrom ....order.error_codes import OrderErrorCodefrom ....order.models import FulfillmentStatus, Order-from ....payment import ChargeStatus+from ....payment import ChargeStatus, PaymentErrorfrom ....warehouse.models import Stockfrom ...tests.utils import get_graphql_content@@ -112,6 +112,40 @@ def test_fulfillment_return_products_amount_and_shipping_costs()+@patch("saleor.order.actions.gateway.refund")+def test_fulfillment_return_products_refund_raising_payment_error(+ mocked_refund,+ staff_api_client,+ permission_manage_orders,+ fulfilled_order,+ payment_dummy,+):+ mocked_refund.side_effect = PaymentError("Error")++ payment_dummy.captured_amount = payment_dummy.total+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED+ payment_dummy.save()+ fulfilled_order.payments.add(payment_dummy)+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)+ amount_to_refund = Decimal("11.00")+ variables = {+ "order": order_id,+ "input": {+ "refund": True,+ "amountToRefund": amount_to_refund,+ "includeShippingCosts": True,+ },+ }+ staff_api_client.user.user_permissions.add(permission_manage_orders)+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)++ content = get_graphql_content(response)+ data = content["data"]["orderFulfillmentReturnProducts"]+ errors = data["errors"]+ assert len(errors) == 1+ assert errors[0]["code"] == OrderErrorCode.CANNOT_REFUND.name++@patch("saleor.order.actions.gateway.refund")def test_fulfillment_return_products_order_lines(mocked_refund,diff --git a/saleor/order/actions.py b/saleor/order/actions.pyindex 1a6ec0f8a9..3c9d04e588 100644--- a/saleor/order/actions.py+++ b/saleor/order/actions.py@@ -5,6 +5,7 @@ from decimal import Decimalfrom typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Tuplefrom django.contrib.sites.models import Site+from django.core.exceptions import ValidationErrorfrom django.db import transactionfrom ..account.models import User@@ -34,6 +35,7 @@ from . import (events,utils,)+from .error_codes import OrderErrorCodefrom .events import (draft_order_created_from_replace_event,fulfillment_refunded_event,@@ -1128,7 +1130,13 @@ def _process_refund(amount += order.shipping_price_gross_amountif amount:amount = min(payment.captured_amount, amount)- gateway.refund(payment, manager, amount)+ try:+ gateway.refund(payment, manager, amount)+ except PaymentError:+ raise ValidationError(+ "The refund operation is not available yet.",+ code=OrderErrorCode.CANNOT_REFUND.value,+ )order_refunded(order, requester, amount, payment, manager=manager)fulfillment_refunded_event(