Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Change gateway validation in CheckoutPaymentCreate mutation (#9530)
l3str4ngecf1d49bdc1f0
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.pyindex 23d9063a10..56ecda5dcc 100644--- a/saleor/graphql/payment/mutations.py+++ b/saleor/graphql/payment/mutations.py@@ -153,22 +153,32 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):)@classmethod- def validate_gateway(cls, manager, gateway_id, currency):+ def validate_gateway(cls, manager, gateway_id, checkout):"""Validate if given gateway can be used for this checkout.- Check if provided gateway_id is on the list of available payment gateways.- Gateway will be rejected if gateway_id is invalid or a gateway doesn't support- checkout's currency.+ Check if provided gateway is active and CONFIGURATION_PER_CHANNEL is True.+ If CONFIGURATION_PER_CHANNEL is False then check if gateway has+ defined currency."""- if not is_currency_supported(currency, gateway_id, manager):- raise ValidationError(- {- "gateway": ValidationError(- f"The gateway {gateway_id} is not available for this checkout.",- code=PaymentErrorCode.NOT_SUPPORTED_GATEWAY.value,- )- }- )+ payment_gateway = manager.get_plugin(gateway_id, checkout.channel.slug)++ if not payment_gateway or not payment_gateway.active:+ cls.raise_not_supported_gateway_error(gateway_id)++ if not payment_gateway.CONFIGURATION_PER_CHANNEL:+ if not is_currency_supported(checkout.currency, gateway_id, manager):+ cls.raise_not_supported_gateway_error(gateway_id)++ @classmethod+ def raise_not_supported_gateway_error(cls, gateway_id: str):+ raise ValidationError(+ {+ "gateway": ValidationError(+ f"The gateway {gateway_id} is not available for this checkout.",+ code=PaymentErrorCode.NOT_SUPPORTED_GATEWAY.value,+ )+ }+ )@classmethoddef validate_token(cls, manager, gateway: str, input_data: dict, channel_slug: str):@@ -241,7 +251,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):gateway = data["gateway"]manager = info.context.plugins- cls.validate_gateway(manager, gateway, checkout.currency)+ cls.validate_gateway(manager, gateway, checkout)cls.validate_return_url(data)lines, unavailable_variant_pks = fetch_checkout_lines(checkout)diff --git a/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py b/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.pyindex ba187fede2..d343d58fe8 100644--- a/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py+++ b/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py@@ -1,4 +1,5 @@from decimal import Decimal+from unittest.mock import patchimport grapheneimport pytest@@ -280,7 +281,11 @@ def test_checkout_add_payment_no_checkout_email(assert data["errors"][0]["code"] == PaymentErrorCode.CHECKOUT_EMAIL_NOT_SET.name-def test_checkout_add_payment_not_supported_gateways(+@patch(+ "saleor.payment.gateways.dummy.plugin.DummyGatewayPlugin.CONFIGURATION_PER_CHANNEL",+ False,+)+def test_checkout_add_payment_not_supported_currency(user_api_client, checkout_without_shipping_required, address):checkout = checkout_without_shipping_required@@ -299,6 +304,43 @@ def test_checkout_add_payment_not_supported_gateways(assert data["errors"][0]["field"] == "gateway"+def test_checkout_add_payment_not_existing_gateway(+ user_api_client, checkout_without_shipping_required, address+):+ checkout = checkout_without_shipping_required+ checkout.billing_address = address+ checkout.save(update_fields=["billing_address", "currency"])++ variables = {+ "token": checkout.token,+ "input": {"gateway": "not.existing", "token": "sample-token", "amount": "10.0"},+ }+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)+ content = get_graphql_content(response)+ data = content["data"]["checkoutPaymentCreate"]+ assert data["errors"][0]["code"] == PaymentErrorCode.NOT_SUPPORTED_GATEWAY.name+ assert data["errors"][0]["field"] == "gateway"+++@patch("saleor.payment.gateways.dummy.plugin.DummyGatewayPlugin.DEFAULT_ACTIVE", False)+def test_checkout_add_payment_gateway_inactive(+ user_api_client, checkout_without_shipping_required, address+):+ checkout = checkout_without_shipping_required+ checkout.billing_address = address+ checkout.save(update_fields=["billing_address", "currency"])++ variables = {+ "token": checkout.token,+ "input": {"gateway": DUMMY_GATEWAY, "token": "sample-token", "amount": "10.0"},+ }+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)+ content = get_graphql_content(response)+ data = content["data"]["checkoutPaymentCreate"]+ assert data["errors"][0]["code"] == PaymentErrorCode.NOT_SUPPORTED_GATEWAY.name+ assert data["errors"][0]["field"] == "gateway"++def test_use_checkout_billing_address_as_payment_billing(user_api_client, checkout_without_shipping_required, address):