Skip to content

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

Update process_payment for dummy and pre-authorized tokens

IKarbowiakc8dd3f20f6b5
diff --git a/saleor/payment/gateways/dummy/__init__.py b/saleor/payment/gateways/dummy/__init__.py
index e10ff8a121..f66679abf4 100644
--- a/saleor/payment/gateways/dummy/__init__.py
+++ b/saleor/payment/gateways/dummy/__init__.py
@@ -117,7 +117,9 @@ def process_payment(
"""Process the payment."""
token = payment_information.token
- if token in PREAUTHORIZED_TOKENS and not config.auto_capture:
- return authorize(payment_information, config)
+ if token in PREAUTHORIZED_TOKENS:
+ authorize_response = authorize(payment_information, config)
+ if not config.auto_capture:
+ return authorize_response
return capture(payment_information, config)
diff --git a/saleor/payment/gateways/dummy/tests/test_dummy.py b/saleor/payment/gateways/dummy/tests/test_dummy.py
index 263b320205..889c8eb5ca 100644
--- a/saleor/payment/gateways/dummy/tests/test_dummy.py
+++ b/saleor/payment/gateways/dummy/tests/test_dummy.py
@@ -252,11 +252,11 @@ def test_process_payment_failed(token, error, payment_dummy):
e._excinfo[1].message == error
[email protected]("token", PREAUTHORIZED_TOKENS)
def test_process_payment_pre_authorized(
- token, payment_dummy, dummy_gateway_config, monkeypatch
+ payment_dummy, dummy_gateway_config, monkeypatch
):
# given
+ token = PREAUTHORIZED_TOKENS[1]
dummy_gateway_config.auto_capture = False
monkeypatch.setattr(
"saleor.payment.gateways.dummy.plugin.DummyGatewayPlugin._get_gateway_config",
@@ -275,6 +275,47 @@ def test_process_payment_pre_authorized(
assert payment_dummy.is_active
+def test_process_payment_pre_authorized_and_capture(
+ payment_dummy, dummy_gateway_config, monkeypatch
+):
+ # given
+ token = PREAUTHORIZED_TOKENS[1]
+ dummy_gateway_config.auto_capture = True
+ monkeypatch.setattr(
+ "saleor.payment.gateways.dummy.plugin.DummyGatewayPlugin._get_gateway_config",
+ lambda _: dummy_gateway_config,
+ )
+
+ # when
+ txn = gateway.process_payment(payment=payment_dummy, token=token)
+
+ # then
+ assert txn.is_success
+ assert txn.payment == payment_dummy
+ assert txn.kind == TransactionKind.CAPTURE
+ assert not txn.error
+ payment_dummy.refresh_from_db()
+ assert payment_dummy.is_active
+
+
+def test_process_payment_pre_authorized_and_capture_error(
+ payment_dummy, dummy_gateway_config, monkeypatch
+):
+ # given
+ token = PREAUTHORIZED_TOKENS[0]
+ dummy_gateway_config.auto_capture = True
+ monkeypatch.setattr(
+ "saleor.payment.gateways.dummy.plugin.DummyGatewayPlugin._get_gateway_config",
+ lambda _: dummy_gateway_config,
+ )
+
+ # when
+ with pytest.raises(PaymentError) as e:
+ gateway.process_payment(payment=payment_dummy, token=token)
+
+ e._excinfo[1].message == TOKEN_VALIDATION_MAPPING[token]
+
+
@pytest.mark.parametrize("token, error", list(TOKEN_VALIDATION_MAPPING.items()))
def test_process_payment_method_error_in_response(
token, error, dummy_gateway_config, dummy_payment_data