Home > Back-end >  Adding shipping rate to checkout session results in "invalid array" exception
Adding shipping rate to checkout session results in "invalid array" exception

Time:05-07

I am creating a checkout session where I want to add shipping rate which I've created in 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 an error that the array is invalid.

If I comment shipping_options it works...

What is wrong here?

CodePudding user response:

Right now your code is just passing a single hash for shipping_options instead of an array, so instead of this:

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

you need to move the brackets to look like this:

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