Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move clean functions to utils
Damian W4dbb2e26b3b8
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex 0b5f8a6b59..73af2d7013 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -16,7 +16,6 @@ from ...checkout.utils import (add_variant_to_checkout,change_billing_address_in_checkout,change_shipping_address_in_checkout,- clean_checkout,create_order,get_user_checkout,get_valid_shipping_methods_for_checkout,@@ -30,6 +29,7 @@ from ...core.permissions import OrderPermissionsfrom ...core.taxes import TaxErrorfrom ...core.utils.url import validate_storefront_urlfrom ...discount import models as voucher_model+from ...graphql.checkout.utils import clean_checkout_payment, clean_checkout_shippingfrom ...payment import PaymentError, gateway, models as payment_modelsfrom ...payment.interface import AddressDatafrom ...payment.utils import store_customer_id@@ -734,7 +734,10 @@ class CheckoutComplete(BaseMutation):discounts = info.context.discountsuser = info.context.user- clean_checkout(checkout, list(checkout), discounts)++ clean_args = checkout, list(checkout), discounts, CheckoutErrorCode+ clean_checkout_shipping(*clean_args)+ clean_checkout_payment(*clean_args)payment = checkout.get_last_active_payment()diff --git a/saleor/graphql/checkout/utils.py b/saleor/graphql/checkout/utils.pynew file mode 100644index 0000000000..9b26a92577--- /dev/null+++ b/saleor/graphql/checkout/utils.py@@ -0,0 +1,70 @@+from enum import Enum+from typing import Iterable++from django.core.exceptions import ValidationError++from ...checkout.models import Checkout, CheckoutLine+from ...checkout.utils import is_fully_paid, is_valid_shipping_method+from ...discount import DiscountInfo+++def clean_checkout_shipping(+ checkout: Checkout,+ lines: Iterable[CheckoutLine],+ discounts: Iterable[DiscountInfo],+ error_code: Enum,+):+ if checkout.is_shipping_required():+ if not checkout.shipping_method:+ raise ValidationError(+ {+ "shipping_method": ValidationError(+ "Shipping method is not set",+ code=error_code.SHIPPING_METHOD_NOT_SET.value, # type: ignore+ )+ }+ )+ if not checkout.shipping_address:+ raise ValidationError(+ {+ "shipping_address": ValidationError(+ "Shipping address is not set",+ code=error_code.SHIPPING_ADDRESS_NOT_SET.value, # type: ignore+ )+ }+ )+ if not is_valid_shipping_method(checkout, lines, discounts):+ raise ValidationError(+ {+ "shipping_method": ValidationError(+ "Shipping method is not valid for your shipping address",+ code=error_code.INVALID_SHIPPING_METHOD.value, # type: ignore+ )+ }+ )+++def clean_billing_address(checkout: Checkout, error_code: Enum):+ if not checkout.billing_address:+ raise ValidationError(+ {+ "billing_address": ValidationError(+ "Billing address is not set",+ code=error_code.BILLING_ADDRESS_NOT_SET.value, # type: ignore+ )+ }+ )+++def clean_checkout_payment(+ checkout: Checkout,+ lines: Iterable[CheckoutLine],+ discounts: Iterable[DiscountInfo],+ error_code: Enum,+):+ clean_billing_address(checkout, error_code)+ if not is_fully_paid(checkout, lines, discounts):+ raise ValidationError(+ "Provided payment methods can not cover the checkout's total amount",+ code=error_code.CHECKOUT_NOT_FULLY_PAID.value, # type: ignore+ )diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.pyindex 6e217876ed..086a26188c 100644--- a/saleor/graphql/payment/mutations.py+++ b/saleor/graphql/payment/mutations.py@@ -5,6 +5,7 @@ from django.core.exceptions import ValidationErrorfrom ...core.permissions import OrderPermissionsfrom ...core.taxes import zero_taxed_moneyfrom ...core.utils import get_client_ip+from ...graphql.checkout.utils import clean_billing_address, clean_checkout_shippingfrom ...payment import PaymentError, gateway, modelsfrom ...payment.error_codes import PaymentErrorCodefrom ...payment.utils import create_payment@@ -87,18 +88,6 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):})- @classmethod- def clean_billing_address(cls, billing_address):- if billing_address is None:- raise ValidationError(- {- "billing_address": ValidationError(- "No billing address associated with this checkout.",- code=PaymentErrorCode.BILLING_ADDRESS_NOT_SET,- )- }- )-@classmethoddef clean_payment_amount(cls, info, checkout_total, amount):if amount != checkout_total.gross.amount:@@ -125,10 +114,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)+ clean_checkout_shipping(+ checkout, list(checkout), info.context.discounts, PaymentErrorCode+ )+ clean_billing_address(checkout, PaymentErrorCode)cls.clean_payment_amount(info, checkout_total, amount)extra_data = {"customer_user_agent": info.context.META.get("HTTP_USER_AGENT")}diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 68d92753e4..aadb591fbc 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -3094,6 +3094,8 @@ enum PaymentErrorCode {INVALIDNOT_FOUNDPARTIAL_PAYMENT_NOT_ALLOWED+ SHIPPING_ADDRESS_NOT_SET+ INVALID_SHIPPING_METHODSHIPPING_METHOD_NOT_SETPAYMENT_ERRORREQUIREDdiff --git a/saleor/payment/error_codes.py b/saleor/payment/error_codes.pyindex 84f3bbfae6..42e3ea5f9a 100644--- a/saleor/payment/error_codes.py+++ b/saleor/payment/error_codes.py@@ -7,6 +7,8 @@ class PaymentErrorCode(Enum):INVALID = "invalid"NOT_FOUND = "not_found"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"diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.pyindex 5de9374410..b4198cfe46 100644--- a/tests/api/test_checkout.py+++ b/tests/api/test_checkout.py@@ -6,13 +6,14 @@ from unittest.mock import ANY, patchimport grapheneimport pytestfrom django.core.exceptions import ValidationError+from graphql.checkout.utils import clean_checkout_payment, clean_checkout_shippingfrom prices import Money, TaxedMoneyfrom saleor.account.models import Userfrom saleor.checkout import calculationsfrom saleor.checkout.error_codes import CheckoutErrorCodefrom saleor.checkout.models import Checkout-from saleor.checkout.utils import clean_checkout, is_fully_paid+from saleor.checkout.utils import is_fully_paidfrom saleor.core.payments import PaymentInterfacefrom saleor.core.taxes import zero_moneyfrom saleor.graphql.checkout.mutations import (@@ -2213,7 +2214,9 @@ def test_clean_checkout(checkout_with_item, payment_dummy, address, shipping_metpayment.checkout = checkoutpayment.save()# Shouldn't raise any errors- clean_checkout(checkout, list(checkout), None)+ clean_args = checkout, list(checkout), None+ clean_checkout_shipping(*clean_args)+ clean_checkout_payment(*clean_args)def test_clean_checkout_no_shipping_method(checkout_with_item, address):@@ -2222,7 +2225,7 @@ def test_clean_checkout_no_shipping_method(checkout_with_item, address):checkout.save()with pytest.raises(ValidationError) as e:- clean_checkout(checkout, list(checkout), None)+ clean_checkout_shipping(checkout, list(checkout), None)msg = "Shipping method is not set"assert e.value.error_list[0].message == msg@@ -2234,7 +2237,7 @@ def test_clean_checkout_no_shipping_address(checkout_with_item, shipping_method)checkout.save()with pytest.raises(ValidationError) as e:- clean_checkout(checkout, list(checkout), None)+ clean_checkout_shipping(checkout, list(checkout), None)msg = "Shipping address is not set"assert e.value.error_list[0].message == msg@@ -2249,7 +2252,7 @@ def test_clean_checkout_invalid_shipping_method(checkout.save()with pytest.raises(ValidationError) as e:- clean_checkout(checkout, list(checkout), None)+ clean_checkout_shipping(checkout, list(checkout), None)msg = "Shipping method is not valid for your shipping address"assert e.value.error_list[0].message == msg@@ -2264,7 +2267,7 @@ def test_clean_checkout_no_billing_address(checkout.save()with pytest.raises(ValidationError) as e:- clean_checkout(checkout, list(checkout), None)+ clean_checkout_payment(checkout, list(checkout), None)msg = "Billing address is not set"assert e.value.error_list[0].message == msg@@ -2277,7 +2280,7 @@ def test_clean_checkout_no_payment(checkout_with_item, shipping_method, address)checkout.save()with pytest.raises(ValidationError) as e:- clean_checkout(checkout, list(checkout), None)+ clean_checkout_payment(checkout, list(checkout), None)msg = "Provided payment methods can not cover the checkout's total amount"assert e.value.error_list[0].message == msgdiff --git a/tests/api/test_payment.py b/tests/api/test_payment.pyindex ad0466cd32..7f4c3e0b30 100644--- a/tests/api/test_payment.py+++ b/tests/api/test_payment.py@@ -156,6 +156,7 @@ def test_checkout_add_payment_with_shipping_method_and_shipping_required():checkout = checkout_with_shipping_requiredcheckout.billing_address = address+ checkout.shipping_address = addresscheckout.shipping_method = other_shipping_methodcheckout.save()