Skip to content

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

Add authorize method to Gateway

Jakub Draganek7d440316b88d
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index 0d7ce70739..a60d4b37d7 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -38,30 +38,60 @@ def payment_postprocess(fn):
return wrapped
+def require_active_payment(fn):
+ def wrapped(self, payment: Payment, *args, **kwargs):
+ if not payment.is_active:
+ raise PaymentError("This payment is no longer active.")
+ return fn(self, payment, *args, **kwargs)
+
+ return wrapped
+
+
class PaymentGateway:
def __init__(self):
self.plugin_manager: PaymentInterface = get_extensions_manager()
@raise_payment_error
@payment_postprocess
+ @require_active_payment
def process_payment(
self, payment: Payment, token: str, store_source: bool = False
) -> Transaction:
+ gateway = _get_gateway(payment)
payment_data = create_payment_information(
payment=payment, payment_token=token, store_source=store_source
)
- gateway = _get_gateway(payment)
response, error = _fetch_gateway_response(
self.plugin_manager.process_payment, gateway, payment_data
)
- txn = create_transaction(
+ return create_transaction(
payment=payment,
kind=TransactionKind.CAPTURE,
payment_information=payment_data,
error_msg=error,
gateway_response=response,
)
- return txn
+
+ @raise_payment_error
+ @payment_postprocess
+ @require_active_payment
+ def authorize(
+ self, payment: Payment, token: str, store_source: bool = False
+ ) -> Transaction:
+ gateway = _get_gateway(payment)
+ payment_data = create_payment_information(
+ payment=payment, payment_token=token, store_source=store_source
+ )
+ response, error = _fetch_gateway_response(
+ self.plugin_manager.authorize_payment, gateway, payment_data
+ )
+ return create_transaction(
+ payment=payment,
+ kind=TransactionKind.AUTH,
+ payment_information=payment_data,
+ error_msg=error,
+ gateway_response=response,
+ )
def _get_gateway(payment: Payment) -> Gateway:
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 7bdb87d603..0ad310ec99 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -1175,7 +1175,7 @@ def test_checkout_complete_does_not_delete_checkout_after_unsuccessful_payment(
address,
shipping_method,
):
- expected_error ="Oops! Something went wrong."
+ expected_error = "Oops! Something went wrong."
mock_get_manager.process_payment.side_effect = error_side_effect
expected_voucher_usage_count = voucher.used
checkout = checkout_with_voucher
diff --git a/tests/gateway/test_gateway.py b/tests/gateway/test_gateway.py
index 2daa482635..90aea28c3e 100644
--- a/tests/gateway/test_gateway.py
+++ b/tests/gateway/test_gateway.py
@@ -10,6 +10,17 @@ from saleor.payment.utils import create_payment_information
RAW_RESPONSE = {"test": "abcdefgheijklmn"}
PROCESS_PAYMENT_RESPONSE = GatewayResponse(
+ is_success=True,
+ customer_id="test_customer",
+ action_required=False,
+ kind=TransactionKind.CAPTURE,
+ amount=Decimal(10.0),
+ currency="usd",
+ transaction_id="1234",
+ error=None,
+ raw_response=RAW_RESPONSE,
+)
+AUTHORIZE_RESPONSE = GatewayResponse(
is_success=True,
customer_id="test_customer",
action_required=False,
@@ -57,7 +68,7 @@ def test_process_payment(gateway, payment_txn_preauth):
USED_GATEWAY, PAYMENT_DATA
)
assert transaction.amount == PROCESS_PAYMENT_RESPONSE.amount
- assert transaction.kind == TransactionKind.AUTH
+ assert transaction.kind == TransactionKind.CAPTURE
assert transaction.currency == "usd"
assert transaction.gateway_response == RAW_RESPONSE
@@ -76,3 +87,20 @@ def test_store_source_when_processing_payment(gateway, payment_txn_preauth):
USED_GATEWAY, PAYMENT_DATA
)
assert transaction.customer_id == PROCESS_PAYMENT_RESPONSE.customer_id
+
+
+def test_authorize_payment(gateway, payment_txn_preauth):
+ PAYMENT_DATA = create_payment_information(
+ payment=payment_txn_preauth, payment_token=TOKEN
+ )
+ gateway.plugin_manager.authorize_payment.return_value = PROCESS_PAYMENT_RESPONSE
+
+ transaction = gateway.authorize(payment=payment_txn_preauth, token=TOKEN)
+
+ gateway.plugin_manager.authorize_payment.assert_called_once_with(
+ USED_GATEWAY, PAYMENT_DATA
+ )
+ assert transaction.amount == AUTHORIZE_RESPONSE.amount
+ assert transaction.kind == TransactionKind.AUTH
+ assert transaction.currency == "usd"
+ assert transaction.gateway_response == RAW_RESPONSE