Skip to content

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

Add void method to PaymentGateway

Jakub Draganek975b985b4d17
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index 3e792b928f..b79dc70dbe 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -146,6 +146,23 @@ class PaymentGateway:
gateway_response=response,
)
+ @raise_payment_error
+ @payment_postprocess
+ def void(self, payment: Payment) -> Transaction:
+ gateway = _get_gateway(payment)
+ token = _get_past_transaction_token(payment, TransactionKind.AUTH)
+ payment_data = create_payment_information(payment=payment, payment_token=token)
+ response, error = _fetch_gateway_response(
+ self.plugin_manager.void_payment, gateway, payment_data
+ )
+ return create_transaction(
+ payment=payment,
+ kind=TransactionKind.VOID,
+ payment_information=payment_data,
+ error_msg=error,
+ gateway_response=response,
+ )
+
def _get_gateway(payment: Payment) -> Gateway:
try:
diff --git a/tests/gateway/test_gateway.py b/tests/gateway/test_gateway.py
index 6933c8297c..c87b46f9d4 100644
--- a/tests/gateway/test_gateway.py
+++ b/tests/gateway/test_gateway.py
@@ -31,6 +31,18 @@ AUTHORIZE_RESPONSE = GatewayResponse(
error=None,
raw_response=RAW_RESPONSE,
)
+VOID_AMOUNT = Decimal("98.40")
+VOID_RESPONSE = GatewayResponse(
+ is_success=True,
+ customer_id="test_customer",
+ action_required=False,
+ kind=TransactionKind.VOID,
+ amount=VOID_AMOUNT,
+ currency="usd",
+ transaction_id="1234",
+ error=None,
+ raw_response=RAW_RESPONSE,
+)
PARTIAL_REFUND_AMOUNT = Decimal(2.0)
PARTIAL_REFUND_RESPONSE = GatewayResponse(
is_success=True,
@@ -190,3 +202,25 @@ def test_full_refund_payment(gateway, payment_txn_captured):
assert transaction.kind == TransactionKind.REFUND
assert transaction.currency == "usd"
assert transaction.gateway_response == RAW_RESPONSE
+
+
+def test_void_payment(gateway, payment_txn_preauth):
+ auth_transaction = payment_txn_preauth.transactions.get()
+ PAYMENT_DATA = create_payment_information(
+ payment=payment_txn_preauth,
+ payment_token=auth_transaction.token,
+ amount=VOID_AMOUNT,
+ )
+ gateway.plugin_manager.void_payment.return_value = VOID_RESPONSE
+
+ transaction = gateway.void(payment=payment_txn_preauth)
+
+ gateway.plugin_manager.void_payment.assert_called_once_with(
+ USED_GATEWAY, PAYMENT_DATA
+ )
+ payment_txn_preauth.refresh_from_db()
+ assert not payment_txn_preauth.is_active
+ assert transaction.amount == VOID_RESPONSE.amount
+ assert transaction.kind == TransactionKind.VOID
+ assert transaction.currency == "usd"
+ assert transaction.gateway_response == RAW_RESPONSE