Retrieve Billing Address from Stripe checkout session?

Viewed 1963

I am creating a checkout session in Node like so. https://stripe.com/docs/api/checkout/sessions/create

I've set the billing_address_collection parameter to required and am now wondering if I can retrieve the billing address collected from the checkout.completed webhook that is triggered.

I'm not finding a good way to retrieve the billing address from a checkout session.

Is there a way to do this? Or should I just gather this information before sending the user to a checkout session?

2 Answers

Billing address can be collected via Checkout. It won't live on the CheckoutSession object though but instead on the PaymentMethod object.

When you get the checkout.session.completed event, it has a payment_intent field with the PaymentIntent object ID. The PaymentIntent object in turn has the PaymentMethod that you need. So you need to go from CheckoutSession -> PaymentIntent -> PaymentMethod to get the billing details.

How to do this: In your webhook event handler, retrieve [0] the PaymentIntent and expand the PaymentMethod (by passing expand: ['payment_method']).

The PaymentMethod has the billing details under the billing_details hash [2]

[0] https://stripe.com/docs/api/payment_intents/retrieve

[1]https://stripe.com/docs/api/expanding_objects

[2] https://stripe.com/docs/api/payment_methods/object#payment_method_object-billing_details

To update the customer with the billing address during a checkout session, use

{
    "billing_address_collection": "required",
    "customer_update": {
        "address": "auto",
        "name": "auto"
    }
}

When creating the checkout session.

Then stripe will update the customer object for you, and you don't need to do it yourself in a webhook.

Related