Skip to content

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

Create PaymentGateway

Jakub Draganek0d8cc173c738
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
new file mode 100644
index 0000000000..4140175a1f
--- /dev/null
+++ b/saleor/payment/gateway.py
@@ -0,0 +1,15 @@
+from ..core.payments import PaymentInterface, Gateway
+from ..extensions.manager import get_extensions_manager
+from .models import Payment, Transaction
+from .utils import create_payment_information
+
+
+class PaymentGateway:
+ def __init__(self):
+ self.plugin_manager: PaymentInterface = get_extensions_manager()
+
+ def process_payment(
+ self, gateway: Gateway, payment: Payment, token: str
+ ) -> Transaction:
+ payment_data = create_payment_information(payment=payment, payment_token=token)
+ self.plugin_manager.process_payment(gateway, payment_data, token)
diff --git a/tests/gateway/test_gateway.py b/tests/gateway/test_gateway.py
new file mode 100644
index 0000000000..33c2edb0f1
--- /dev/null
+++ b/tests/gateway/test_gateway.py
@@ -0,0 +1,54 @@
+from decimal import Decimal
+from unittest.mock import ANY
+
+import pytest
+from sympy.physics.units import amount
+
+from saleor.core.payments import Gateway, PaymentInterface
+from saleor.payment.gateway import PaymentGateway
+from saleor.payment.interface import GatewayResponse, PaymentData
+from saleor.payment.utils import create_payment_information
+
+PROCESS_PAYMENT_RESPONSE = GatewayResponse(
+ is_success=True,
+ action_required=False,
+ kind="process_payment",
+ amount=Decimal(10.0),
+ currency="usd",
+ transaction_id="1234",
+ error=None,
+)
+
+
+def fake_manager(mocker):
+ mgr = mocker.Mock(spec=PaymentInterface)
+ return mgr
+
+
[email protected](autouse=True, scope="function")
+def mock_get_manager(mocker, fake_manager):
+ mgr = mocker.patch(
+ "saleor.payment.gateway.get_extensions_manager",
+ auto_spec=True,
+ return_value=fake_manager,
+ )
+ yield mgr
+ mgr.assert_called_once()
+
+
+def test_process_payment(payment_txn_preauth):
+ TOKEN = "token"
+ gateway = PaymentGateway()
+ PAYMENT_DATA = create_payment_information(
+ payment=payment_txn_preauth, payment_token=TOKEN
+ )
+ gateway.plugin_manager.process_payment.result_value = PROCESS_PAYMENT_RESPONSE
+
+ gateway.process_payment(
+ payment=payment_txn_preauth, gateway=Gateway.STRIPE, token=TOKEN
+ )
+
+ gateway.plugin_manager.process_payment.assert_called_once_with(
+ Gateway.STRIPE, PAYMENT_DATA, TOKEN
+ )