Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor checkout compleate mutation
fowczarek742e388ed47e
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex 380edff656..03b21753c7 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -344,17 +344,15 @@ class CheckoutShippingAddressUpdate(BaseMutation, I18nMixin):checkout = cls.get_node_or_error(info, checkout_id, errors, 'checkout_id', only_type=Checkout)- if checkout is not None:+ if checkout is not None and shipping_address:shipping_address, errors = cls.validate_address(shipping_address, errors, instance=checkout.shipping_address)+ # FIXME test if below function is calledclean_shipping_method(checkout, checkout.shipping_method, errors,info.context.discounts,get_taxes_for_address(shipping_address))- if errors:- CheckoutShippingAddressUpdate(errors=errors)-- if shipping_address:+ if not errors:with transaction.atomic():shipping_address.save()change_shipping_address_in_cart(checkout, shipping_address)@@ -379,16 +377,13 @@ class CheckoutBillingAddressUpdate(CheckoutShippingAddressUpdate):checkout = cls.get_node_or_error(info, checkout_id, errors, 'checkout_id', only_type=Checkout)- if billing_address:+ if checkout is not None and billing_address:billing_address, errors = cls.validate_address(billing_address, errors, instance=checkout.billing_address)- if errors:- return CheckoutBillingAddressUpdate(errors=errors)-- if checkout is not None and billing_address:- with transaction.atomic():- billing_address.save()- change_billing_address_in_cart(checkout, billing_address)+ if not errors:+ with transaction.atomic():+ billing_address.save()+ change_billing_address_in_cart(checkout, billing_address)return CheckoutShippingAddressUpdate(checkout=checkout, errors=errors)@@ -438,11 +433,10 @@ class CheckoutShippingMethodUpdate(BaseMutation):clean_shipping_method(checkout, shipping_method, errors, info.context.discounts,info.context.taxes, remove=False)- if errors:- return CheckoutShippingMethodUpdate(errors=errors)- checkout.shipping_method = shipping_method- checkout.save(update_fields=['shipping_method'])+ if not errors:+ checkout.shipping_method = shipping_method+ checkout.save(update_fields=['shipping_method'])return CheckoutShippingMethodUpdate(checkout=checkout, errors=errors)@@ -462,26 +456,27 @@ class CheckoutComplete(BaseMutation):errors = []checkout = cls.get_node_or_error(info, checkout_id, errors, 'checkout_id', only_type=Checkout)- if checkout is None:- return CheckoutComplete(errors=errors)- taxes = get_taxes_for_cart(checkout, info.context.taxes)- ready, checkout_error = ready_to_place_order(- checkout, taxes, info.context.discounts)- if not ready:- cls.add_error(field=None, message=checkout_error, errors=errors)+ if checkout is not None:+ taxes = get_taxes_for_cart(checkout, info.context.taxes)+ ready, checkout_error = ready_to_place_order(+ checkout, taxes, info.context.discounts)+ if not ready:+ cls.add_error(+ field=None, message=checkout_error, errors=errors)+ if not errors:+ try:+ order = create_order(+ cart=checkout,+ tracking_code=analytics.get_client_id(info.context),+ discounts=info.context.discounts, taxes=taxes)+ except InsufficientStock:+ order = None+ cls.add_error(+ field=None, message='Insufficient product stock.',+ errors=errors)+ if errors:return CheckoutComplete(errors=errors)- try:- order = create_order(- cart=checkout,- tracking_code=analytics.get_client_id(info.context),- discounts=info.context.discounts, taxes=taxes)- except InsufficientStock:- order = None- cls.add_error(- field=None, message='Insufficient product stock.',- errors=errors)-payment = checkout.payments.filter(is_active=True).first()# FIXME there could be a situation where order was created but payment# failed. we should cancel/delete the order at this moment I think