Skip to content

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

Add capture to PaymentGateway

Jakub Draganek69ca7c95c41f
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index a60d4b37d7..88af0ad41d 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -1,3 +1,4 @@
+from decimal import Decimal
import logging
from django.core.exceptions import ImproperlyConfigured
@@ -8,6 +9,8 @@ from ..extensions.manager import get_extensions_manager
from . import GatewayError, TransactionKind, PaymentError
from .models import Payment, Transaction
from .utils import (
+ clean_authorize,
+ clean_capture,
create_payment_information,
create_transaction,
validate_gateway_response,
@@ -74,10 +77,10 @@ class PaymentGateway:
@raise_payment_error
@payment_postprocess
- @require_active_payment
def authorize(
self, payment: Payment, token: str, store_source: bool = False
) -> Transaction:
+ clean_authorize(payment)
gateway = _get_gateway(payment)
payment_data = create_payment_information(
payment=payment, payment_token=token, store_source=store_source
@@ -93,12 +96,39 @@ class PaymentGateway:
gateway_response=response,
)
+ @raise_payment_error
+ @payment_postprocess
+ def capture(
+ self, payment: Payment, amount: Decimal = None, store_source: bool = False
+ ) -> Transaction:
+ if amount is None:
+ amount = payment.get_charge_amount()
+ clean_capture(payment, amount)
+ gateway = _get_gateway(payment)
+ token = _get_authorization_token(payment)
+ payment_data = create_payment_information(
+ payment=payment,
+ payment_token=token,
+ amount=amount,
+ store_source=store_source,
+ )
+ response, error = _fetch_gateway_response(
+ self.plugin_manager.capture_payment, gateway, payment_data
+ )
+ return create_transaction(
+ payment=payment,
+ kind=TransactionKind.CAPTURE,
+ payment_information=payment_data,
+ error_msg=error,
+ gateway_response=response,
+ )
+
def _get_gateway(payment: Payment) -> Gateway:
try:
gateway = Gateway(payment.gateway)
except AttributeError:
- raise ImproperlyConfigured("Payment gateway %s is not configured." % gateway)
+ raise ImproperlyConfigured(f"Payment gateway {gateway} is not configured.")
return gateway
@@ -117,3 +147,12 @@ def _fetch_gateway_response(fn, *args, **kwargs):
error = ERROR_MSG
response = None
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
diff --git a/tests/gateway/test_gateway.py b/tests/gateway/test_gateway.py
index 90aea28c3e..a30a129f47 100644
--- a/tests/gateway/test_gateway.py
+++ b/tests/gateway/test_gateway.py
@@ -31,6 +31,17 @@ AUTHORIZE_RESPONSE = GatewayResponse(
error=None,
raw_response=RAW_RESPONSE,
)
+AUTHORIZE_RESPONSE = GatewayResponse(
+ is_success=True,
+ customer_id="test_customer",
+ action_required=False,
+ kind=TransactionKind.AUTH,
+ amount=Decimal(10.0),
+ currency="usd",
+ transaction_id="1234",
+ error=None,
+ raw_response=RAW_RESPONSE,
+)
TOKEN = "token"
USED_GATEWAY = Gateway.DUMMY
@@ -89,13 +100,13 @@ def test_store_source_when_processing_payment(gateway, payment_txn_preauth):
assert transaction.customer_id == PROCESS_PAYMENT_RESPONSE.customer_id
-def test_authorize_payment(gateway, payment_txn_preauth):
+def test_authorize_payment(gateway, payment_dummy):
PAYMENT_DATA = create_payment_information(
- payment=payment_txn_preauth, payment_token=TOKEN
+ payment=payment_dummy, payment_token=TOKEN
)
- gateway.plugin_manager.authorize_payment.return_value = PROCESS_PAYMENT_RESPONSE
+ gateway.plugin_manager.authorize_payment.return_value = AUTHORIZE_RESPONSE
- transaction = gateway.authorize(payment=payment_txn_preauth, token=TOKEN)
+ transaction = gateway.authorize(payment=payment_dummy, token=TOKEN)
gateway.plugin_manager.authorize_payment.assert_called_once_with(
USED_GATEWAY, PAYMENT_DATA
@@ -104,3 +115,21 @@ def test_authorize_payment(gateway, payment_txn_preauth):
assert transaction.kind == TransactionKind.AUTH
assert transaction.currency == "usd"
assert transaction.gateway_response == RAW_RESPONSE
+
+
+def test_capture_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
+ )
+ gateway.plugin_manager.capture_payment.return_value = PROCESS_PAYMENT_RESPONSE
+
+ transaction = gateway.capture(payment=payment_txn_preauth)
+
+ gateway.plugin_manager.capture_payment.assert_called_once_with(
+ USED_GATEWAY, PAYMENT_DATA
+ )
+ assert transaction.amount == PROCESS_PAYMENT_RESPONSE.amount
+ assert transaction.kind == TransactionKind.CAPTURE
+ assert transaction.currency == "usd"
+ assert transaction.gateway_response == RAW_RESPONSE