Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor after review
Maciej Korycinskic56ac0520e1a
diff --git a/saleor/graphql/checkout/utils.py b/saleor/checkout/checkout_cleaner.pysimilarity index 83%rename from saleor/graphql/checkout/utils.pyrename to saleor/checkout/checkout_cleaner.pyindex c774daa2fe..e370dd8cc9 100644--- a/saleor/graphql/checkout/utils.py+++ b/saleor/checkout/checkout_cleaner.py@@ -1,12 +1,13 @@-from typing import Iterable, Type, Union+from typing import Iterable, Optional, Type, Unionfrom django.core.exceptions import ValidationError-from ...checkout.error_codes import CheckoutErrorCode-from ...checkout.models import Checkout, CheckoutLine-from ...checkout.utils import is_fully_paid, is_valid_shipping_method-from ...discount import DiscountInfo-from ...payment.error_codes import PaymentErrorCode+from ..discount import DiscountInfo+from ..payment import gateway, models as payment_models+from ..payment.error_codes import PaymentErrorCode+from .error_codes import CheckoutErrorCode+from .models import Checkout, CheckoutLine+from .utils import is_fully_paid, is_valid_shipping_methoddef clean_checkout_shipping(@@ -65,9 +66,11 @@ def clean_checkout_payment(lines: Iterable[CheckoutLine],discounts: Iterable[DiscountInfo],error_code: Type[CheckoutErrorCode],+ last_payment: Optional[payment_models.Payment],):clean_billing_address(checkout, error_code)if not is_fully_paid(checkout, lines, discounts):+ gateway.payment_refund_or_void(last_payment)raise ValidationError("Provided payment methods can not cover the checkout's total amount",code=error_code.CHECKOUT_NOT_FULLY_PAID.value,diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.pyindex db7ae0bf4f..fde8370ba9 100644--- a/saleor/checkout/complete_checkout.py+++ b/saleor/checkout/complete_checkout.py@@ -10,31 +10,27 @@ from ..core.taxes import TaxErrorfrom ..core.utils.url import validate_storefront_urlfrom ..discount import DiscountInfofrom ..discount.models import NotApplicable-from ..graphql.checkout.utils import ( # TODO move from graphql- clean_checkout_payment,- clean_checkout_shipping,-)from ..order import models as order_modelsfrom ..order.models import Orderfrom ..payment import PaymentError, gatewayfrom ..payment.models import Payment, Transactionfrom ..payment.utils import store_customer_idfrom . import models-from .calculations import calculate_checkout_total_with_gift_cards+from .checkout_cleaner import clean_checkout_payment, clean_checkout_shippingfrom .error_codes import CheckoutErrorCodefrom .utils import abort_order_data, create_order, get_order, prepare_order_data@transaction.atomic-def prepare_checkout(checkout: models.Checkout, discounts, tracking_code, redirect_url):+def prepare_checkout(+ checkout: models.Checkout, discounts, tracking_code, redirect_url, payment+):lines = list(checkout)clean_checkout_shipping(checkout, lines, discounts, CheckoutErrorCode)- clean_checkout_payment(checkout, lines, discounts, CheckoutErrorCode)-- payment = checkout.get_last_active_payment()-- validate_payment_amount(discounts, payment, checkout)+ clean_checkout_payment(+ checkout, lines, discounts, CheckoutErrorCode, last_payment=payment+ )if redirect_url:try:@@ -70,16 +66,6 @@ def convert_checkout_to_order(return order-def validate_payment_amount(discounts, payment, checkout):- checkout_total = calculate_checkout_total_with_gift_cards(checkout, discounts)- if payment.total != checkout_total.gross.amount:- gateway.payment_refund_or_void(payment)- raise ValidationError(- "Payment does not cover all checkout value.",- code=CheckoutErrorCode.CHECKOUT_NOT_FULLY_PAID.value,- )--def get_order_data(checkout: models.Checkout, discounts: List[DiscountInfo]) -> dict:try:with transaction.atomic():@@ -140,14 +126,16 @@ def complete_checkout(order = get_order(checkout.token)if order:return order, action_required, action_data++ payment = checkout.get_last_active_payment()prepare_checkout(checkout=checkout,discounts=discounts,tracking_code=tracking_code,redirect_url=redirect_url,+ payment=payment,)- payment = checkout.get_last_active_payment()try:order_data = get_order_data(checkout, discounts)except ValidationError as error:diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.pyindex fa049c3967..0ef1f57fad 100644--- a/saleor/graphql/checkout/tests/test_checkout.py+++ b/saleor/graphql/checkout/tests/test_checkout.py@@ -11,6 +11,10 @@ from prices import Money, TaxedMoneyfrom ....account.models import Userfrom ....checkout import calculations+from ....checkout.checkout_cleaner import (+ clean_checkout_payment,+ clean_checkout_shipping,+)from ....checkout.error_codes import CheckoutErrorCodefrom ....checkout.models import Checkoutfrom ....checkout.utils import add_variant_to_checkout@@ -25,7 +29,6 @@ from ..mutations import (clean_shipping_method,update_checkout_shipping_method_if_invalid,)-from ..utils import clean_checkout_payment, clean_checkout_shippingdef test_clean_shipping_method_after_shipping_address_changes_stay_the_same(@@ -2116,7 +2119,9 @@ def test_clean_checkout(checkout_with_item, payment_dummy, address, shipping_met# Shouldn't raise any errorslines = list(checkout)clean_checkout_shipping(checkout, lines, None, CheckoutErrorCode)- clean_checkout_payment(checkout, lines, None, CheckoutErrorCode)+ clean_checkout_payment(+ checkout, lines, None, CheckoutErrorCode, last_payment=payment+ )def test_clean_checkout_no_shipping_method(checkout_with_item, address):@@ -2166,9 +2171,12 @@ def test_clean_checkout_no_billing_address(checkout.shipping_address = addresscheckout.shipping_method = shipping_methodcheckout.save()+ payment = checkout.get_last_active_payment()with pytest.raises(ValidationError) as e:- clean_checkout_payment(checkout, list(checkout), None, CheckoutErrorCode)+ clean_checkout_payment(+ checkout, list(checkout), None, CheckoutErrorCode, last_payment=payment+ )msg = "Billing address is not set"assert e.value.error_dict["billing_address"][0].message == msg@@ -2179,9 +2187,12 @@ def test_clean_checkout_no_payment(checkout_with_item, shipping_method, address)checkout.shipping_method = shipping_methodcheckout.billing_address = addresscheckout.save()+ payment = checkout.get_last_active_payment()with pytest.raises(ValidationError) as e:- clean_checkout_payment(checkout, list(checkout), None, CheckoutErrorCode)+ clean_checkout_payment(+ checkout, list(checkout), None, CheckoutErrorCode, last_payment=payment+ )msg = "Provided payment methods can not cover the checkout's total amount"assert e.value.error_list[0].message == msgdiff --git a/saleor/graphql/checkout/tests/test_checkout_complete.py b/saleor/graphql/checkout/tests/test_checkout_complete.pyindex 5120d6f9d7..973b557011 100644--- a/saleor/graphql/checkout/tests/test_checkout_complete.py+++ b/saleor/graphql/checkout/tests/test_checkout_complete.py@@ -741,7 +741,7 @@ def test_checkout_complete_payment_payment_total_different_than_checkout(payment = payment_dummypayment.is_active = Truepayment.order = None- payment.total = total.gross.amount + Decimal(10)+ payment.total = total.gross.amount - Decimal(10)payment.currency = total.gross.currencypayment.checkout = checkoutpayment.save()@@ -750,7 +750,6 @@ def test_checkout_complete_payment_payment_total_different_than_checkout(orders_count = Order.objects.count()checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)variables = {"checkoutId": checkout_id, "redirectUrl": "https://www.example.com"}- response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)# whenresponse = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.pyindex 9f5bc2451d..c7680f519b 100644--- a/saleor/graphql/payment/mutations.py+++ b/saleor/graphql/payment/mutations.py@@ -3,11 +3,11 @@ from django.conf import settingsfrom django.core.exceptions import ValidationErrorfrom ...checkout.calculations import calculate_checkout_total_with_gift_cards+from ...checkout.checkout_cleaner import clean_billing_address, clean_checkout_shippingfrom ...checkout.utils import cancel_active_paymentsfrom ...core.permissions import OrderPermissionsfrom ...core.utils import get_client_ipfrom ...core.utils.url import validate_storefront_url-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, is_currency_supporteddiff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.pyindex 0191596823..9a3b29553a 100644--- a/saleor/payment/gateway.py+++ b/saleor/payment/gateway.py@@ -271,7 +271,9 @@ def _validate_refund_amount(payment: Payment, amount: Decimal):raise PaymentError("Cannot refund more than captured.")-def payment_refund_or_void(payment):+def payment_refund_or_void(payment: Optional[Payment]):+ if payment is None:+ returnif payment.can_refund():refund(payment)elif payment.can_void():