Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add confirm method to PaymentGateway
Jakub Draganekc5ec3ac36fec
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.pyindex b79dc70dbe..da6fb459bc 100644--- a/saleor/payment/gateway.py+++ b/saleor/payment/gateway.py@@ -148,6 +148,7 @@ class PaymentGateway:@raise_payment_error@payment_postprocess+ @require_active_paymentdef void(self, payment: Payment) -> Transaction:gateway = _get_gateway(payment)token = _get_past_transaction_token(payment, TransactionKind.AUTH)@@ -163,6 +164,24 @@ class PaymentGateway:gateway_response=response,)+ @raise_payment_error+ @payment_postprocess+ @require_active_payment+ def confirm(self, payment: Payment) -> Transaction:+ gateway = _get_gateway(payment)+ token = _get_past_transaction_token(payment, TransactionKind.AUTH)+ payment_data = create_payment_information(payment=payment, payment_token=token)+ response, error = _fetch_gateway_response(+ self.plugin_manager.confirm_payment, gateway, payment_data+ )+ return create_transaction(+ payment=payment,+ kind=TransactionKind.CONFIRM,+ payment_information=payment_data,+ error_msg=error,+ gateway_response=response,+ )+def _get_gateway(payment: Payment) -> Gateway:try:diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.pyindex ca21e30cef..8e4721d82d 100644--- a/tests/api/test_checkout.py+++ b/tests/api/test_checkout.py@@ -40,6 +40,7 @@ def other_shipping_method(shipping_zone):@pytest.fixture(autouse=True)def setup_dummy_gateway(settings):settings.PLUGINS = ["saleor.payment.gateways.dummy.plugin.DummyGatewayPlugin"]+ return settingsdef test_clean_shipping_method_after_shipping_address_changes_stay_the_same(diff --git a/tests/gateway/test_gateway.py b/tests/gateway/test_gateway.pyindex c87b46f9d4..5db34b1319 100644--- a/tests/gateway/test_gateway.py+++ b/tests/gateway/test_gateway.py@@ -67,6 +67,18 @@ FULL_REFUND_RESPONSE = GatewayResponse(error=None,raw_response=RAW_RESPONSE,)+CONFIRM_AMOUNT = Decimal("98.40")+CONFIRM_RESPONSE = GatewayResponse(+ is_success=True,+ customer_id="test_customer",+ action_required=False,+ kind=TransactionKind.CONFIRM,+ amount=CONFIRM_AMOUNT,+ currency="usd",+ transaction_id="1234",+ error=None,+ raw_response=RAW_RESPONSE,+)TOKEN = "token"USED_GATEWAY = Gateway.DUMMY@@ -224,3 +236,23 @@ def test_void_payment(gateway, payment_txn_preauth):assert transaction.kind == TransactionKind.VOIDassert transaction.currency == "usd"assert transaction.gateway_response == RAW_RESPONSE+++def test_confirm_payment(gateway, payment_txn_preauth):+ auth_transaction = payment_txn_preauth.transactions.get()+ PAYMENT_DATA = create_payment_information(+ payment=payment_txn_preauth,+ payment_token=auth_transaction.token,+ amount=CONFIRM_AMOUNT,+ )+ gateway.plugin_manager.confirm_payment.return_value = CONFIRM_RESPONSE++ transaction = gateway.confirm(payment=payment_txn_preauth)++ gateway.plugin_manager.confirm_payment.assert_called_once_with(+ USED_GATEWAY, PAYMENT_DATA+ )+ assert transaction.amount == CONFIRM_RESPONSE.amount+ assert transaction.kind == TransactionKind.CONFIRM+ assert transaction.currency == "usd"+ assert transaction.gateway_response == RAW_RESPONSE