Skip to content

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

Add refund method to PaymentGateway

Jakub Draganek102ed431cf6e
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index 88af0ad41d..f696fde6fe 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -105,7 +105,7 @@ class PaymentGateway:
amount = payment.get_charge_amount()
clean_capture(payment, amount)
gateway = _get_gateway(payment)
- token = _get_authorization_token(payment)
+ token = _get_past_transaction_token(payment, TransactionKind.AUTH)
payment_data = create_payment_information(
payment=payment,
payment_token=token,
@@ -123,6 +123,29 @@ class PaymentGateway:
gateway_response=response,
)
+ @raise_payment_error
+ @payment_postprocess
+ def refund(self, payment: Payment, amount: Decimal = None) -> Transaction:
+ if amount is None:
+ amount = payment.captured_amount
+ if not payment.can_refund():
+ raise PaymentError("This payment cannot be refunded.")
+ gateway = _get_gateway(payment)
+ token = _get_past_transaction_token(payment, TransactionKind.CAPTURE)
+ payment_data = create_payment_information(
+ payment=payment, payment_token=token, amount=amount
+ )
+ response, error = _fetch_gateway_response(
+ self.plugin_manager.refund_payment, gateway, payment_data
+ )
+ return create_transaction(
+ payment=payment,
+ kind=TransactionKind.REFUND,
+ payment_information=payment_data,
+ error_msg=error,
+ gateway_response=response,
+ )
+
def _get_gateway(payment: Payment) -> Gateway:
try:
@@ -149,10 +172,8 @@ def _fetch_gateway_response(fn, *args, **kwargs):
return response, error
-def _get_authorization_token(payment: Payment):
- auth_transaction = payment.transactions.filter(
- kind=TransactionKind.AUTH, is_success=True
- ).first()
- if auth_transaction is None:
- raise PaymentError("Cannot capture unauthorized transaction")
- return auth_transaction.token
+def _get_past_transaction_token(payment: Payment, kind: TransactionKind):
+ txn = payment.transactions.filter(kind=kind, is_success=True).first()
+ if txn is None:
+ raise PaymentError("Cannot find successful {kind.value} transaction")
+ return txn.token
diff --git a/tests/gateway/test_gateway.py b/tests/gateway/test_gateway.py
index a30a129f47..d5685a9aee 100644
--- a/tests/gateway/test_gateway.py
+++ b/tests/gateway/test_gateway.py
@@ -3,7 +3,7 @@ from decimal import Decimal
import pytest
from saleor.core.payments import Gateway, PaymentInterface
-from saleor.payment import TransactionKind
+from saleor.payment import TransactionKind, ChargeStatus
from saleor.payment.gateway import PaymentGateway
from saleor.payment.interface import GatewayResponse
from saleor.payment.utils import create_payment_information
@@ -31,12 +31,13 @@ AUTHORIZE_RESPONSE = GatewayResponse(
error=None,
raw_response=RAW_RESPONSE,
)
-AUTHORIZE_RESPONSE = GatewayResponse(
+REFUND_AMOUNT = Decimal(2.0)
+REFUND_RESPONSE = GatewayResponse(
is_success=True,
customer_id="test_customer",
action_required=False,
- kind=TransactionKind.AUTH,
- amount=Decimal(10.0),
+ kind=TransactionKind.REFUND,
+ amount=REFUND_AMOUNT,
currency="usd",
transaction_id="1234",
error=None,
@@ -133,3 +134,24 @@ def test_capture_payment(gateway, payment_txn_preauth):
assert transaction.kind == TransactionKind.CAPTURE
assert transaction.currency == "usd"
assert transaction.gateway_response == RAW_RESPONSE
+
+
+def test_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,
+ 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.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.kind == TransactionKind.REFUND
+ assert transaction.currency == "usd"
+ assert transaction.gateway_response == RAW_RESPONSE