Home  >  Q&A  >  body text

Adding shipping costs to checkout session causes "invalid array" exception

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粉403549616P粉403549616228 days ago402

reply all(1)I'll reply

  • P粉523625080

    P粉5236250802024-03-29 09:37:54

    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'),],
            ],

    reply
    0
  • Cancelreply