Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add CheckoutBillingAddressUpdate mutation

Pacu2bb2e276aa1e2
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index d77c39dcdb..77e494bb64 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -58,7 +58,7 @@ from .checkout.mutations import (
CheckoutCreate, CheckoutLinesAdd, CheckoutLinesUpdate, CheckoutLineDelete,
CheckoutCustomerAttach, CheckoutCustomerDetach,
CheckoutShippingAddressUpdate, CheckoutEmailUpdate, CheckoutComplete,
- CheckoutShippingMethodUpdate)
+ CheckoutShippingMethodUpdate, CheckoutBillingAddressUpdate)
from .checkout.resolvers import (
resolve_checkouts, resolve_checkout_lines, resolve_checkout)
@@ -295,6 +295,7 @@ class Mutations(ProductMutations):
checkout_line_delete = CheckoutLineDelete.Field()
checkout_customer_attach = CheckoutCustomerAttach.Field()
checkout_customer_detach = CheckoutCustomerDetach.Field()
+ checkout_billing_address_update = CheckoutBillingAddressUpdate.Field()
checkout_shipping_address_update = CheckoutShippingAddressUpdate.Field()
checkout_shipping_method_update = CheckoutShippingMethodUpdate.Field()
checkout_email_update = CheckoutEmailUpdate.Field()
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 1da8cb7e91..36548914eb 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -4,8 +4,9 @@ from django.db import transaction
from ...account.models import Address
from ...checkout import models
from ...checkout.utils import (
- add_variant_to_cart, change_shipping_address_in_cart, create_order,
- get_taxes_for_cart, ready_to_place_order)
+ add_variant_to_cart, change_billing_address_in_cart,
+ change_shipping_address_in_cart, create_order, get_taxes_for_cart,
+ ready_to_place_order)
from ...core import analytics
from ...core.exceptions import InsufficientStock
from ...core.utils.taxes import get_taxes_for_address
@@ -308,6 +309,36 @@ class CheckoutShippingAddressUpdate(BaseMutation):
return CheckoutShippingAddressUpdate(checkout=checkout, errors=errors)
+class CheckoutBillingAddressUpdate(CheckoutShippingAddressUpdate):
+ checkout = graphene.Field(Checkout, description='An updated checkout')
+
+ class Arguments:
+ checkout_id = graphene.ID(description='ID of the Checkout.')
+ billing_address = AddressInput(
+ description=(
+ 'The billing address of the checkout.'))
+
+ class Meta:
+ description = 'Update billing address in the existing Checkout.'
+
+ @classmethod
+ def mutate(
+ cls, root, info, checkout_id, billing_address):
+ errors = []
+ checkout = cls.get_node_or_error(
+ info, checkout_id, errors, 'checkout_id', only_type=Checkout)
+ if billing_address:
+ billing_address = cls.clean_address(billing_address, errors)
+ if errors:
+ return CheckoutBillingAddressUpdate(errors=errors)
+
+ if checkout and billing_address:
+ with transaction.atomic():
+ billing_address.save()
+ change_billing_address_in_cart(checkout, billing_address)
+ return CheckoutShippingAddressUpdate(checkout=checkout, errors=errors)
+
+
class CheckoutEmailUpdate(BaseMutation):
checkout = graphene.Field(Checkout, description='An updated checkout')
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 044ee7fa48..d9000c48fb 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -301,6 +301,52 @@ def test_checkout_shipping_address_update(user_api_client, cart_with_item):
assert cart.shipping_address.city == shipping_address['city']
+def test_checkout_billing_address_update(user_api_client, cart_with_item):
+ cart = cart_with_item
+ assert cart.shipping_address is None
+ checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
+
+ query = """
+ mutation checkoutBillingAddressUpdate($checkoutId: ID!, $billingAddress: AddressInput!) {
+ checkoutBillingAddressUpdate(checkoutId: $checkoutId, billingAddress: $billingAddress) {
+ checkout {
+ token,
+ id
+ },
+ errors {
+ field,
+ message
+ }
+ }
+ }
+ """
+ billing_address = {
+ 'firstName': "John",
+ 'lastName': 'Doe',
+ 'streetAddress1': 'Wall st.',
+ 'streetAddress2': '',
+ 'postalCode': '902010',
+ 'country': 'US',
+ 'city': 'New York'}
+
+ variables = {'checkoutId': checkout_id, 'billingAddress': billing_address}
+
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content['data']['checkoutBillingAddressUpdate']
+ assert not data['errors']
+ cart.refresh_from_db()
+ assert cart.billing_address is not None
+ assert cart.billing_address.first_name == billing_address['firstName']
+ assert cart.billing_address.last_name == billing_address['lastName']
+ assert cart.billing_address.street_address_1 == billing_address[
+ 'streetAddress1']
+ assert cart.billing_address.street_address_2 == billing_address[
+ 'streetAddress2']
+ assert cart.billing_address.postal_code == billing_address['postalCode']
+ assert cart.billing_address.country == billing_address['country']
+ assert cart.billing_address.city == billing_address['city']
+
def test_checkout_email_update(user_api_client, cart_with_item):
cart = cart_with_item
assert not cart.email
@@ -367,8 +413,7 @@ def test_checkout_complete(
}
}
"""
- variables = {
- 'checkoutId': checkout_id, }
+ variables = {'checkoutId': checkout_id}
assert not Order.objects.exists()
response = user_api_client.post_graphql(query, variables)
content = get_graphql_content(response)