Adding shipping costs to checkout session causes "invalid array" exception
P粉403549616
P粉403549616 2024-03-28 11:04:59
0
1
413

I'm creating a checkout session where I want to add shipping costs that I created in the Stripe dashboard.

This is my code:

$charge = $stripeClient->checkout->sessions->create([
        'payment_method_types' => ['card', 'sepa_debit', 'giropay', 'sofort', 'alipay'],
        'success_url' => 'https://example.com/success',
        'cancel_url' => 'https://example.com/cancel',
        'shipping_address_collection' => [
          'allowed_countries' => ['DE'],
        ],

        'shipping_options' => [
          'shipping_rate' => [env('SHIPPING_KEY')],
        ],
        
        'line_items' => [$lineItems],
        'automatic_tax' => [
          'enabled' => true,
        ],
        'mode' => 'payment',
        'allow_promotion_codes' => true,
      ]);

But it gives error that array is invalid.

If I comment shipping_options it will work...

What's wrong here?

P粉403549616
P粉403549616

reply all(1)
P粉523625080

Now, your code is just passing a hash for shipping_options, not an array, so don't do this:

'shipping_options' => [
          'shipping_rate' => [env('SHIPPING_KEY')],
        ],

You need to move the brackets so that they look like this:

'shipping_options' => [
          ['shipping_rate' => env('SHIPPING_KEY'),],
        ],
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template