Skip to content
Advertisement

Can you pass a shipping address to Stripe Checkout

I am using Stripe Checkout API to direct a website user to make payment.

Is there a way to pass a shipping address to the hosted checkout page so it’s gathered from the referrer rather then Stripe themselves?

function createSession()
{
    require 'vendor/autoload.php';
    StripeStripe::setApiKey('[API_KEY_REDACTED]');
    
    $YOUR_DOMAIN = '[SITE_URL_REDACTED]';
    
    // Calculate price
    $price = 50;
    
    $checkout_session = StripeCheckoutSession::create([
        'billing_address_collection' => 'required',
        'payment_method_types' => ['card'],
        'line_items' => [[
            'price_data' => [
                'currency' => 'gbp',
                'unit_amount' => $price,
                'product_data' => [
                    'name' => 'Product'
                ],
            ],
            'quantity' => 1,
        ]],
        'mode' => 'payment',
        'success_url' => $YOUR_DOMAIN . '/success',
        'cancel_url' => $YOUR_DOMAIN . '/cancel',
    ]);
    
    return json_encode(['id' => $checkout_session->id]);
}

You can add the following line to make Stripe ask for a shipping address, but I want to pass this from the referrer instead.

'shipping_address_collection' => [
    'allowed_countries' => ['US', 'CA'],
],

Advertisement

Answer

To do this you would create a Customer and provide their shipping address, then provide that existing Customer when creating the Checkout session:

$checkout_session = StripeCheckoutSession::create([
    'customer' => 'cus_123',
    ...
]);

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement