Can you pass a shipping address to Stripe Checkout

Viewed 1601

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';
    \Stripe\Stripe::setApiKey('[API_KEY_REDACTED]');
    
    $YOUR_DOMAIN = '[SITE_URL_REDACTED]';
    
    // Calculate price
    $price = 50;
    
    $checkout_session = \Stripe\Checkout\Session::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'],
],
1 Answers
Related