Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Test for full refund
Jakub Draganek9f202b5dc031
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.pyindex f696fde6fe..3e792b928f 100644--- a/saleor/payment/gateway.py+++ b/saleor/payment/gateway.py@@ -1,20 +1,19 @@-from decimal import Decimalimport logging+from decimal import Decimalfrom django.core.exceptions import ImproperlyConfiguredfrom ..core.payments import Gateway, PaymentInterfacefrom ..extensions.manager import get_extensions_manager--from . import GatewayError, TransactionKind, PaymentError+from . import GatewayError, PaymentError, TransactionKindfrom .models import Payment, Transactionfrom .utils import (+ _gateway_postprocess,clean_authorize,clean_capture,create_payment_information,create_transaction,validate_gateway_response,- _gateway_postprocess,)logger = logging.getLogger(__name__)@@ -128,6 +127,7 @@ class PaymentGateway:def refund(self, payment: Payment, amount: Decimal = None) -> Transaction:if amount is None:amount = payment.captured_amount+ _validate_refund_amound(payment, amount)if not payment.can_refund():raise PaymentError("This payment cannot be refunded.")gateway = _get_gateway(payment)@@ -177,3 +177,10 @@ def _get_past_transaction_token(payment: Payment, kind: TransactionKind):if txn is None:raise PaymentError("Cannot find successful {kind.value} transaction")return txn.token+++def _validate_refund_amound(payment: Payment, amount: Decimal):+ if amount <= 0:+ raise PaymentError("Amount should be a positive number.")+ if amount > payment.captured_amount:+ raise PaymentError("Cannot refund more than captured")diff --git a/tests/gateway/test_gateway.py b/tests/gateway/test_gateway.pyindex d5685a9aee..6933c8297c 100644--- a/tests/gateway/test_gateway.py+++ b/tests/gateway/test_gateway.py@@ -3,7 +3,7 @@ from decimal import Decimalimport pytestfrom saleor.core.payments import Gateway, PaymentInterface-from saleor.payment import TransactionKind, ChargeStatus+from saleor.payment import ChargeStatus, TransactionKindfrom saleor.payment.gateway import PaymentGatewayfrom saleor.payment.interface import GatewayResponsefrom saleor.payment.utils import create_payment_information@@ -31,13 +31,25 @@ AUTHORIZE_RESPONSE = GatewayResponse(error=None,raw_response=RAW_RESPONSE,)-REFUND_AMOUNT = Decimal(2.0)-REFUND_RESPONSE = GatewayResponse(+PARTIAL_REFUND_AMOUNT = Decimal(2.0)+PARTIAL_REFUND_RESPONSE = GatewayResponse(is_success=True,customer_id="test_customer",action_required=False,kind=TransactionKind.REFUND,- amount=REFUND_AMOUNT,+ amount=PARTIAL_REFUND_AMOUNT,+ currency="usd",+ transaction_id="1234",+ error=None,+ raw_response=RAW_RESPONSE,+)+FULL_REFUND_AMOUNT = Decimal("98.40")+FULL_REFUND_RESPONSE = GatewayResponse(+ is_success=True,+ customer_id="test_customer",+ action_required=False,+ kind=TransactionKind.REFUND,+ amount=FULL_REFUND_AMOUNT,currency="usd",transaction_id="1234",error=None,@@ -136,22 +148,45 @@ def test_capture_payment(gateway, payment_txn_preauth):assert transaction.gateway_response == RAW_RESPONSE-def test_refund_payment(gateway, payment_txn_captured):+def test_partial_refund_payment(gateway, payment_txn_captured):capture_transaction = payment_txn_captured.transactions.get()PAYMENT_DATA = create_payment_information(payment=payment_txn_captured,- amount=REFUND_AMOUNT,+ amount=PARTIAL_REFUND_AMOUNT,payment_token=capture_transaction.token,)- gateway.plugin_manager.refund_payment.return_value = REFUND_RESPONSE- transaction = gateway.refund(payment=payment_txn_captured, amount=REFUND_AMOUNT)+ gateway.plugin_manager.refund_payment.return_value = PARTIAL_REFUND_RESPONSE+ transaction = gateway.refund(+ payment=payment_txn_captured, amount=PARTIAL_REFUND_AMOUNT+ )gateway.plugin_manager.refund_payment.assert_called_once_with(USED_GATEWAY, PAYMENT_DATA)payment_txn_captured.refresh_from_db()assert payment_txn_captured.charge_status == ChargeStatus.PARTIALLY_REFUNDED- assert transaction.amount == REFUND_AMOUNT+ assert transaction.amount == PARTIAL_REFUND_AMOUNT+ assert transaction.kind == TransactionKind.REFUND+ assert transaction.currency == "usd"+ assert transaction.gateway_response == RAW_RESPONSE+++def test_full_refund_payment(gateway, payment_txn_captured):+ capture_transaction = payment_txn_captured.transactions.get()+ PAYMENT_DATA = create_payment_information(+ payment=payment_txn_captured,+ amount=FULL_REFUND_AMOUNT,+ payment_token=capture_transaction.token,+ )+ gateway.plugin_manager.refund_payment.return_value = FULL_REFUND_RESPONSE+ transaction = gateway.refund(payment=payment_txn_captured)+ gateway.plugin_manager.refund_payment.assert_called_once_with(+ USED_GATEWAY, PAYMENT_DATA+ )++ payment_txn_captured.refresh_from_db()+ assert payment_txn_captured.charge_status == ChargeStatus.FULLY_REFUNDED+ assert transaction.amount == FULL_REFUND_AMOUNTassert transaction.kind == TransactionKind.REFUNDassert transaction.currency == "usd"assert transaction.gateway_response == RAW_RESPONSE