Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add gateway validation in CheckoutPaymentCreate
IKarbowiak84f0ed2bcba0
diff --git a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.pyindex 1e509e6d63..7f4b725968 100644--- a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py+++ b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py@@ -494,7 +494,7 @@ def test_checkout_payment_charge(checkout=checkout_with_billing_address,lines=list(checkout_with_billing_address),).gross.amount,- "gateway": "Dummy",+ "gateway": "mirumee.payments.dummy","token": "charged",},}diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.pyindex 19d1d71a22..2330d99a0d 100644--- a/saleor/graphql/payment/mutations.py+++ b/saleor/graphql/payment/mutations.py@@ -10,6 +10,7 @@ from ...graphql.checkout.utils import clean_billing_address, clean_checkout_shipfrom ...payment import PaymentError, gateway, modelsfrom ...payment.error_codes import PaymentErrorCodefrom ...payment.utils import create_payment+from ...plugins.manager import get_plugins_managerfrom ..account.i18n import I18nMixinfrom ..account.types import AddressInputfrom ..checkout.types import Checkout@@ -105,6 +106,21 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):})+ @classmethod+ def validate_gateway(cls, gateway_id, currency):+ available_gateways = get_plugins_manager().list_payment_gateways(+ currency=currency+ )+ if not any([gateway["id"] == gateway_id for gateway in available_gateways]):+ raise ValidationError(+ {+ "gateway": ValidationError(+ "This gateway does not support checkout currency.",+ code=PaymentErrorCode.NOT_SUPPORTED_GATEWAY.value,+ )+ }+ )+@classmethoddef perform_mutation(cls, _root, info, checkout_id, **data):checkout_id = from_global_id_strict_type(@@ -115,6 +131,9 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):).get(pk=checkout_id)data = data["input"]+ gateway = data["gateway"]++ cls.validate_gateway(gateway, checkout.currency)checkout_total = cls.calculate_total(info, checkout)amount = data.get("amount", checkout_total.gross.amount)@@ -126,7 +145,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):extra_data = {"customer_user_agent": info.context.META.get("HTTP_USER_AGENT")}payment = create_payment(- gateway=data["gateway"],+ gateway=gateway,payment_token=data["token"],total=amount,currency=settings.DEFAULT_CURRENCY,diff --git a/saleor/graphql/payment/tests/test_payment.py b/saleor/graphql/payment/tests/test_payment.pyindex 7f3881612b..8904f203c1 100644--- a/saleor/graphql/payment/tests/test_payment.py+++ b/saleor/graphql/payment/tests/test_payment.py@@ -102,7 +102,7 @@ def test_checkout_add_payment_without_shipping_method_and_not_shipping_required(variables = {"checkoutId": checkout_id,"input": {- "gateway": "Dummy",+ "gateway": "mirumee.payments.dummy","token": "sample-token","amount": total.gross.amount,},@@ -138,7 +138,7 @@ def test_checkout_add_payment_without_shipping_method_with_shipping_required(variables = {"checkoutId": checkout_id,"input": {- "gateway": "Dummy",+ "gateway": "mirumee.payments.dummy","token": "sample-token","amount": total.gross.amount,},@@ -165,7 +165,7 @@ def test_checkout_add_payment_with_shipping_method_and_shipping_required(variables = {"checkoutId": checkout_id,"input": {- "gateway": "Dummy",+ "gateway": "mirumee.payments.dummy","token": "sample-token","amount": total.gross.amount,},@@ -201,7 +201,7 @@ def test_checkout_add_payment(variables = {"checkoutId": checkout_id,"input": {- "gateway": "Dummy",+ "gateway": "mirumee.payments.dummy","token": "sample-token","amount": total.gross.amount,},@@ -237,7 +237,7 @@ def test_checkout_add_payment_default_amount(variables = {"checkoutId": checkout_id,- "input": {"gateway": "DUMMY", "token": "sample-token"},+ "input": {"gateway": "mirumee.payments.dummy", "token": "sample-token"},}response = user_api_client.post_graphql(CREATE_QUERY, variables)content = get_graphql_content(response)@@ -265,7 +265,7 @@ def test_checkout_add_payment_bad_amount(variables = {"checkoutId": checkout_id,"input": {- "gateway": "DUMMY",+ "gateway": "mirumee.payments.dummy","token": "sample-token","amount": str(calculations.checkout_total(@@ -284,6 +284,32 @@ def test_checkout_add_payment_bad_amount()+def test_checkout_add_payment_not_supported_gateways(+ user_api_client, checkout_without_shipping_required, address+):+ checkout = checkout_without_shipping_required+ checkout.billing_address = address+ checkout.currency = "EUR"+ checkout.save(update_fields=["billing_address", "currency"])+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)++ variables = {+ "checkoutId": checkout_id,+ "input": {+ "gateway": "mirumee.payments.dummy",+ "token": "sample-token",+ "amount": "10.0",+ },+ }+ response = user_api_client.post_graphql(CREATE_QUERY, variables)+ content = get_graphql_content(response)+ data = content["data"]["checkoutPaymentCreate"]+ assert (+ data["paymentErrors"][0]["code"] == PaymentErrorCode.NOT_SUPPORTED_GATEWAY.name+ )+ assert data["paymentErrors"][0]["field"] == "gateway"++def test_use_checkout_billing_address_as_payment_billing(user_api_client, checkout_without_shipping_required, address):@@ -293,7 +319,7 @@ def test_use_checkout_billing_address_as_payment_billing(variables = {"checkoutId": checkout_id,"input": {- "gateway": "Dummy",+ "gateway": "mirumee.payments.dummy","token": "sample-token","amount": total.gross.amount,},diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 29d7c56c70..ec8eaecb12 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -3105,13 +3105,14 @@ enum PaymentErrorCode {GRAPHQL_ERRORINVALIDNOT_FOUND+ REQUIRED+ UNIQUEPARTIAL_PAYMENT_NOT_ALLOWEDSHIPPING_ADDRESS_NOT_SETINVALID_SHIPPING_METHODSHIPPING_METHOD_NOT_SETPAYMENT_ERROR- REQUIRED- UNIQUE+ NOT_SUPPORTED_GATEWAY}type PaymentGateway {diff --git a/saleor/payment/error_codes.py b/saleor/payment/error_codes.pyindex 42e3ea5f9a..c6cb7456aa 100644--- a/saleor/payment/error_codes.py+++ b/saleor/payment/error_codes.py@@ -6,10 +6,11 @@ class PaymentErrorCode(Enum):GRAPHQL_ERROR = "graphql_error"INVALID = "invalid"NOT_FOUND = "not_found"+ REQUIRED = "required"+ UNIQUE = "unique"PARTIAL_PAYMENT_NOT_ALLOWED = "partial_payment_not_allowed"SHIPPING_ADDRESS_NOT_SET = "shipping_address_not_set"INVALID_SHIPPING_METHOD = "invalid_shipping_method"SHIPPING_METHOD_NOT_SET = "shipping_method_not_set"PAYMENT_ERROR = "payment_error"- REQUIRED = "required"- UNIQUE = "unique"+ NOT_SUPPORTED_GATEWAY = "not_supported_gateway"diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex cd52566f68..2e1b3da42a 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -220,7 +220,7 @@ def site_settings(db, settings) -> SiteSettings:@pytest.fixturedef checkout(db):- checkout = Checkout.objects.create()+ checkout = Checkout.objects.create(currency="USD")checkout.set_country("US", commit=True)return checkout@@ -409,6 +409,7 @@ def user_checkout(customer_user):billing_address=customer_user.default_billing_address,shipping_address=customer_user.default_shipping_address,note="Test notes",+ currency="USD",)return checkout