Skip to content

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

Basic flow for process payment in PaymentGateway

Jakub Draganek73d0488aab0d
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index 4140175a1f..838065f90c 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -1,7 +1,7 @@
from ..core.payments import PaymentInterface, Gateway
from ..extensions.manager import get_extensions_manager
from .models import Payment, Transaction
-from .utils import create_payment_information
+from .utils import create_payment_information, create_transaction
class 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.py
index 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 ANY
import pytest
-from sympy.physics.units import amount
from saleor.core.payments import Gateway, PaymentInterface
+from saleor.payment import TransactionKind
from saleor.payment.gateway import PaymentGateway
-from saleor.payment.interface import GatewayResponse, PaymentData
+from saleor.payment.interface import GatewayResponse
from 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.fixture
def 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.STRIPE
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.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