Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Basic flow for process payment in PaymentGateway
Jakub Draganek73d0488aab0d
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.pyindex 4140175a1f..838065f90c 100644--- a/saleor/payment/gateway.py+++ b/saleor/payment/gateway.py@@ -1,7 +1,7 @@from ..core.payments import PaymentInterface, Gatewayfrom ..extensions.manager import get_extensions_managerfrom .models import Payment, Transaction-from .utils import create_payment_information+from .utils import create_payment_information, create_transactionclass PaymentGateway:@@ -12,4 +12,10 @@ class PaymentGateway: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)+ response = self.plugin_manager.process_payment(gateway, payment_data, token)+ return create_transaction(+ payment,+ kind=response.kind,+ payment_information=payment_data,+ gateway_response=response,+ )diff --git a/tests/gateway/test_gateway.py b/tests/gateway/test_gateway.pyindex 33c2edb0f1..43b6b993ca 100644--- a/tests/gateway/test_gateway.py+++ b/tests/gateway/test_gateway.py@@ -1,29 +1,29 @@from decimal import Decimal-from unittest.mock import ANYimport pytest-from sympy.physics.units import amountfrom saleor.core.payments import Gateway, PaymentInterface+from saleor.payment import TransactionKindfrom saleor.payment.gateway import PaymentGateway-from saleor.payment.interface import GatewayResponse, PaymentData+from saleor.payment.interface import GatewayResponsefrom saleor.payment.utils import create_payment_information+RAW_RESPONSE = {"test": "abcdefgheijklmn"}PROCESS_PAYMENT_RESPONSE = GatewayResponse(is_success=True,action_required=False,- kind="process_payment",+ kind=TransactionKind.AUTH,amount=Decimal(10.0),currency="usd",transaction_id="1234",error=None,+ raw_response=RAW_RESPONSE,)@pytest.fixturedef fake_manager(mocker):- mgr = mocker.Mock(spec=PaymentInterface)- return mgr+ return mocker.Mock(spec=PaymentInterface)@pytest.fixture(autouse=True, scope="function")@@ -39,16 +39,21 @@ def mock_get_manager(mocker, fake_manager):def test_process_payment(payment_txn_preauth):TOKEN = "token"+ USED_GATEWAY = Gateway.STRIPEgateway = PaymentGateway()PAYMENT_DATA = create_payment_information(payment=payment_txn_preauth, payment_token=TOKEN)- gateway.plugin_manager.process_payment.result_value = PROCESS_PAYMENT_RESPONSE+ gateway.plugin_manager.process_payment.return_value = PROCESS_PAYMENT_RESPONSE- gateway.process_payment(- payment=payment_txn_preauth, gateway=Gateway.STRIPE, token=TOKEN+ transaction = gateway.process_payment(+ payment=payment_txn_preauth, gateway=USED_GATEWAY, token=TOKEN)gateway.plugin_manager.process_payment.assert_called_once_with(- Gateway.STRIPE, PAYMENT_DATA, TOKEN+ USED_GATEWAY, PAYMENT_DATA, TOKEN)+ assert transaction.amount == PROCESS_PAYMENT_RESPONSE.amount+ assert transaction.kind == TransactionKind.AUTH+ assert transaction.currency == "usd"+ assert transaction.gateway_response == RAW_RESPONSE