Home > Software design >  How to describe line items in Stripe checkout?
How to describe line items in Stripe checkout?

Time:06-13

This is the way how I create checkout function:

foreach($request->arrayOfObjects as $req) {
    array_push($lineItems, [
        'price' => $req['priceID'],
        'quantity' => (int) $req['quantity']
    ]);
}
    
if (Auth::check()) {
    $charge = $stripeClient->checkout->sessions->create([
        'payment_method_types' => [
            'card',
            'sepa_debit',
            'giropay',
            'sofort',
            'alipay'
        ],
        'success_url' => env('APP_URL').'/success',
        'cancel_url' => env('APP_URL').'/cancel',
        'shipping_address_collection' => [
            'allowed_countries' => ['DE'],
        ],
        'shipping_options' => [
            [
               'shipping_rate' => $request->lieferung ? env('SHIPPING_RATE_LIEFERUNG') : env('SHIPPING_RATE_ABHOLUNG')
            ],
        ],
        'line_items' => [$lineItems],
        'mode' => 'payment',
        'metadata' => [
            'lieferung' => $request->lieferung,
            'isCustomer' => true
        ],
        'allow_promotion_codes' => true,
        'customer' => Auth::user()->stripe_id
    ]);
}

It works fine though... However, I'd like to know is there way to describe measurements of an article?

For example I have ordered 10 packages of Parquet Flooring Classic and each package has 3m2 of parquet.

I'd like to see it in the bill, like:

Article 1001 - Parquet Flooring Classic (10x Packages - 3m2 each)

I found that metadata could be provided but only for the global checkout level, not per item.

Is there any way to do this?

CodePudding user response:

You can provide metadata per item using Stripe Checkout. Here's an example:

$charge = $stripeClient->checkout->sessions->create([
    ...
    'line_items' => [
        // item 1
        [
            'price_data' => [
                'product_data' => [
                    'name' => 'Item A',
                ],
                // 10.99 USD
                'currency' => 'usd',
                'unit_amount' => 1099,
            ],
            'quantity' => 3,
        ],
        // item 2
        [
            'price_data' => [
                'product_data' => [
                    'name' => 'Item B',
                ],
                // 5.49 USD
                'currency' => 'usd',
                'unit_amount' => 549,
            ],
            'quantity' => 2, 
        ],
    ],
    ...
]);

At checkout, it will give you a neat list of

3 x Item A ....... 32.97 $ USD
2 x Item B ....... 10.98 $ USD

Check all the information on line_items in The stripe API documentation (click on the 'show child attributes' button for all the possible keys and what they're for.)

  • Related