Skip to content

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

Reuse checkout billing address for payment

Marcin Gębalac1056cc12991
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index e554c3b8fe..36212089f7 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -1,3 +1,5 @@
+from textwrap import dedent
+
import graphene
from django.conf import settings
from graphql_jwt.decorators import permission_required
@@ -31,7 +33,9 @@ class PaymentInput(graphene.InputObjectType):
'Total amount of the transaction, including '
'all taxes and discounts.'))
billing_address = AddressInput(
- description='Billing address.', required=True)
+ description=dedent(
+ '''Billing address. If empty, the billing address associated with
+ the checkout instance will be used.'''))
class CheckoutPaymentCreate(BaseMutation, I18nMixin):
@@ -52,10 +56,17 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
errors = []
checkout = cls.get_node_or_error(
info, checkout_id, errors, 'checkout_id', only_type=Checkout)
- billing_address, errors = cls.validate_address(
- input['billing_address'], errors, 'billing_address')
+ if checkout is None:
+ return CheckoutPaymentCreate(errors=errors)
- if checkout is None or billing_address is None:
+ billing_address = checkout.billing_address
+ if 'billing_address' in input:
+ billing_address, errors = cls.validate_address(
+ input['billing_address'], errors, 'billing_address')
+ if billing_address is None:
+ cls.add_error(
+ errors, 'billingAddress',
+ 'No billing address associated with this checkout.')
return CheckoutPaymentCreate(errors=errors)
amount = input['amount']
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 58eb42042c..defadd0236 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1250,19 +1250,19 @@ type Payment implements Node {
created: DateTime!
modified: DateTime!
chargeStatus: PaymentChargeStatusEnum!
- billingEmail: String!
- customerIpAddress: String
- extraData: String!
token: String!
total: Money
capturedAmount: Money
checkout: Checkout
order: Order
+ billingEmail: String!
ccFirstDigits: String!
ccLastDigits: String!
ccBrand: String!
ccExpMonth: Int
ccExpYear: Int
+ customerIpAddress: String
+ extraData: String!
transactions: [Transaction]
actions: [OrderAction]!
billingAddress: Address
@@ -1297,7 +1297,7 @@ input PaymentInput {
gateway: GatewaysEnum!
token: String!
amount: Decimal!
- billingAddress: AddressInput!
+ billingAddress: AddressInput
}
type PaymentRefund {
diff --git a/tests/api/test_payment.py b/tests/api/test_payment.py
index 5e6bb2bf5e..b85d80c9d1 100644
--- a/tests/api/test_payment.py
+++ b/tests/api/test_payment.py
@@ -89,7 +89,6 @@ CREATE_QUERY = """
def test_checkout_add_payment(
user_api_client, cart_with_item, graphql_address_data):
cart = cart_with_item
- assert cart.user is None
checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
variables = {
'checkoutId': checkout_id,
@@ -114,6 +113,38 @@ def test_checkout_add_payment(
assert payment.charge_status == ChargeStatus.NOT_CHARGED
+def test_use_checkout_billing_address_as_payment_billing(
+ user_api_client, cart_with_item, address):
+ cart = cart_with_item
+ checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
+ variables = {
+ 'checkoutId': checkout_id,
+ 'input': {
+ 'gateway': 'DUMMY',
+ 'token': 'sample-token',
+ 'amount': str(cart.get_total().gross.amount)}}
+ response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ content = get_graphql_content(response)
+ data = content['data']['checkoutPaymentCreate']
+
+ # check if proper error is returned if address is missing
+ assert data['errors'][0]['field'] == 'billingAddress'
+
+ # assign the address and try again
+ address.street_address_1 = 'spanish-inqusition'
+ address.save()
+ cart.billing_address = address
+ cart.save()
+ response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ content = get_graphql_content(response)
+ data = content['data']['checkoutPaymentCreate']
+
+ cart.refresh_from_db()
+ assert cart.payments.count() == 1
+ payment = cart.payments.first()
+ assert payment.billing_address_1 == address.street_address_1
+
+
CAPTURE_QUERY = """
mutation PaymentCapture($paymentId: ID!, $amount: Decimal!) {
paymentCapture(paymentId: $paymentId, amount: $amount) {