Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Prevent creation payments without shipping method
Damian Wf4f005f7107e
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.pyindex 0e5d522b32..6e217876ed 100644--- a/saleor/graphql/payment/mutations.py+++ b/saleor/graphql/payment/mutations.py@@ -75,6 +75,18 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):)return max(checkout_total, zero_taxed_money(checkout_total.currency))+ @classmethod+ def clean_shipping_method(cls, checkout):+ if not checkout.shipping_method:+ raise ValidationError(+ {+ "shipping_method": ValidationError(+ "Shipping method not set for this checkout.",+ code=PaymentErrorCode.SHIPPING_METHOD_NOT_SET,+ )+ }+ )+@classmethoddef clean_billing_address(cls, billing_address):if billing_address is None:@@ -114,9 +126,10 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):checkout_total = cls.calculate_total(info, checkout)amount = data.get("amount", checkout_total.gross.amount)+ if checkout.is_shipping_required():+ cls.clean_shipping_method(checkout)cls.clean_billing_address(checkout.billing_address)cls.clean_payment_amount(info, checkout_total, amount)-extra_data = {"customer_user_agent": info.context.META.get("HTTP_USER_AGENT")}payment = create_payment(diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 19e9aec62c..68d92753e4 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -3094,6 +3094,7 @@ enum PaymentErrorCode {INVALIDNOT_FOUNDPARTIAL_PAYMENT_NOT_ALLOWED+ SHIPPING_METHOD_NOT_SETPAYMENT_ERRORREQUIREDUNIQUEdiff --git a/saleor/payment/error_codes.py b/saleor/payment/error_codes.pyindex a03361b310..84f3bbfae6 100644--- a/saleor/payment/error_codes.py+++ b/saleor/payment/error_codes.py@@ -7,6 +7,7 @@ class PaymentErrorCode(Enum):INVALID = "invalid"NOT_FOUND = "not_found"PARTIAL_PAYMENT_NOT_ALLOWED = "partial_payment_not_allowed"+ SHIPPING_METHOD_NOT_SET = "shipping_method_not_set"PAYMENT_ERROR = "payment_error"REQUIRED = "required"UNIQUE = "unique"diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.pyindex 1201bba3e2..5de9374410 100644--- a/tests/api/test_checkout.py+++ b/tests/api/test_checkout.py@@ -23,25 +23,12 @@ from saleor.order.models import Orderfrom saleor.payment import TransactionKindfrom saleor.payment.interface import GatewayResponsefrom saleor.plugins.manager import PluginsManager-from saleor.shipping import ShippingMethodType-from saleor.shipping.models import ShippingMethodfrom saleor.warehouse.models import Stockfrom ..utils import get_available_quantity_for_stockfrom .utils import assert_no_permission, get_graphql_content-def other_shipping_method(shipping_zone):- return ShippingMethod.objects.create(- name="DPD",- minimum_order_price=Money(0, "USD"),- type=ShippingMethodType.PRICE_BASED,- price=Money(9, "USD"),- shipping_zone=shipping_zone,- )--@pytest.fixture(autouse=True)def setup_dummy_gateway(settings):settings.PLUGINS = ["saleor.payment.gateways.dummy.plugin.DummyGatewayPlugin"]diff --git a/tests/api/test_payment.py b/tests/api/test_payment.pyindex 93f49354d4..b513f2e840 100644--- a/tests/api/test_payment.py+++ b/tests/api/test_payment.py@@ -90,8 +90,96 @@ CREATE_QUERY = """"""-def test_checkout_add_payment(user_api_client, checkout_with_item, address):- checkout = checkout_with_item+def test_checkout_add_payment_without_shipping_method_and_not_shipping_required(+ user_api_client, checkout_without_shipping_required, address+):+ checkout = checkout_without_shipping_required+ checkout.billing_address = address+ checkout.save()++ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)+ total = calculations.checkout_total(checkout=checkout, lines=list(checkout))+ variables = {+ "checkoutId": checkout_id,+ "input": {+ "gateway": "Dummy",+ "token": "sample-token",+ "amount": total.gross.amount,+ },+ }+ response = user_api_client.post_graphql(CREATE_QUERY, variables)+ content = get_graphql_content(response)+ data = content["data"]["checkoutPaymentCreate"]+ assert not data["paymentErrors"]+++def test_checkout_add_payment_without_shipping_method_with_shipping_required(+ user_api_client, checkout_with_shipping_required, address+):+ checkout = checkout_with_shipping_required++ checkout.billing_address = address+ checkout.save()++ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)+ total = calculations.checkout_total(checkout=checkout, lines=list(checkout))+ variables = {+ "checkoutId": checkout_id,+ "input": {+ "gateway": "Dummy",+ "token": "sample-token",+ "amount": total.gross.amount,+ },+ }+ response = user_api_client.post_graphql(CREATE_QUERY, variables)+ content = get_graphql_content(response)+ data = content["data"]["checkoutPaymentCreate"]++ assert data["paymentErrors"][0]["code"] == "SHIPPING_METHOD_NOT_SET"+ assert data["paymentErrors"][0]["field"] == "shippingMethod"+++def test_checkout_add_payment_with_shipping_method_and_shipping_required(+ user_api_client, checkout_with_shipping_required, other_shipping_method, address+):+ checkout = checkout_with_shipping_required+ checkout.billing_address = address+ checkout.shipping_method = other_shipping_method+ checkout.save()++ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)+ total = calculations.checkout_total(checkout=checkout, lines=list(checkout))+ variables = {+ "checkoutId": checkout_id,+ "input": {+ "gateway": "Dummy",+ "token": "sample-token",+ "amount": total.gross.amount,+ },+ }+ response = user_api_client.post_graphql(CREATE_QUERY, variables)+ content = get_graphql_content(response)+ data = content["data"]["checkoutPaymentCreate"]++ assert not data["paymentErrors"]+ transactions = data["payment"]["transactions"]+ assert not transactions+ payment = Payment.objects.get()+ assert payment.checkout == checkout+ assert payment.is_active+ assert payment.token == "sample-token"+ assert payment.total == total.gross.amount+ assert payment.currency == total.gross.currency+ assert payment.charge_status == ChargeStatus.NOT_CHARGED+ assert payment.billing_address_1 == checkout.billing_address.street_address_1+ assert payment.billing_first_name == checkout.billing_address.first_name+ assert payment.billing_last_name == checkout.billing_address.last_name+++def test_checkout_add_payment(+ user_api_client, checkout_without_shipping_required, address+):+ checkout = checkout_without_shipping_requiredcheckout.billing_address = addresscheckout.save()@@ -125,10 +213,9 @@ def test_checkout_add_payment(user_api_client, checkout_with_item, address):def test_checkout_add_payment_default_amount(- user_api_client, checkout_with_item, address+ user_api_client, checkout_without_shipping_required, address):- checkout = checkout_with_item- checkout = checkout_with_item+ checkout = checkout_without_shipping_requiredcheckout.billing_address = addresscheckout.save()@@ -154,8 +241,10 @@ def test_checkout_add_payment_default_amount(assert payment.charge_status == ChargeStatus.NOT_CHARGED-def test_checkout_add_payment_bad_amount(user_api_client, checkout_with_item, address):- checkout = checkout_with_item+def test_checkout_add_payment_bad_amount(+ user_api_client, checkout_without_shipping_required, address+):+ checkout = checkout_without_shipping_requiredcheckout.billing_address = addresscheckout.save()checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)@@ -183,9 +272,9 @@ def test_checkout_add_payment_bad_amount(user_api_client, checkout_with_item, addef test_use_checkout_billing_address_as_payment_billing(- user_api_client, checkout_with_item, address+ user_api_client, checkout_without_shipping_required, address):- checkout = checkout_with_item+ checkout = checkout_without_shipping_requiredcheckout_id = graphene.Node.to_global_id("Checkout", checkout.pk)total = calculations.checkout_total(checkout=checkout, lines=list(checkout))variables = {diff --git a/tests/conftest.py b/tests/conftest.pyindex 62547c5861..37dbd47e90 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -216,6 +216,35 @@ def checkout_with_item(checkout, product):return checkout+def checkout_with_shipping_required(checkout_with_item, product):+ checkout = checkout_with_item+ variant = product.variants.get()+ add_variant_to_checkout(checkout, variant, 3)+ checkout.save()+ return checkout+++def other_shipping_method(shipping_zone):+ return ShippingMethod.objects.create(+ name="DPD",+ minimum_order_price=Money(0, "USD"),+ type=ShippingMethodType.PRICE_BASED,+ price=Money(9, "USD"),+ shipping_zone=shipping_zone,+ )+++def checkout_without_shipping_required(checkout, product_without_shipping):+ checkout = checkout+ variant = product_without_shipping.variants.get()+ add_variant_to_checkout(checkout, variant, 1)+ checkout.save()+ return checkout++@pytest.fixturedef checkout_with_single_item(checkout, product):variant = product.variants.get()