stripe / stripe-php

PHP library for the Stripe API.
https://stripe.com
MIT License
3.75k stars 850 forks source link

Payment link retrieved from checkout completed event doesn't have line_items of original payment link #1769

Closed php4fan closed 1 month ago

php4fan commented 1 month ago

Describe the bug

I'm not sure whether this is a bug in the stripe-php library or in the actual API.

To Reproduce

I create a payment link like this:

$stripe = new StripeClient("__TOKEN__");
$price = $stripe->prices->create([
    'currency' => 'eur',
    'unit_amount' => 400,
    'product_data' => [
        'name' => "Whatever"
    ],
]);
$link = $stripe->paymentLinks->create([
    'line_items' => [
        [
            'price' => $price->id,
            'quantity' => 1
        ],
    ]
]);

Then I have a webook for the event checkout.session.complete. When I receive such webhook I handle it like this:

        $input = $request->getContent(); /// this is php://input
        $event = \Stripe\Webhook::constructEvent(
            $input,
            $_SERVER['HTTP_STRIPE_SIGNATURE'],
            '__WH_SECRET__'
        );  

        $data = $event->data->toArray();

        $stripe = new StripeClient($this->getPaymentToken());

        $plink = $stripe->paymentLinks->retrieve($data['object']['payment_link']);

        $this->getLogger()->debug($plink->line_items->toJSON());
        // .......

Expected behavior

This should log the line_items in the payment link object, which should correspond to the ones that were set when I created the payment link.

NOTE: this bug template has an "expected behavior" section but lacks a "observed behavior" section.

OBSERVED BEHAVIOR

I get the error:

Call to a member function toJSON() on null

which means the line_items property of the payment link is null.

Code snippets

No response

OS

all

PHP version

7.4

Library version

latest

API version

2024-04-10

Additional context

No response

php4fan commented 1 month ago

I also tried changing the webhook-handling code to this:

$input = $request->getContent(); /// this is php://input
$event = \Stripe\Webhook::constructEvent(
    $input,
    $_SERVER['HTTP_STRIPE_SIGNATURE'],
    '__WH_SECRET__'
);  

$data = $event->data->toArray();

$stripe = new StripeClient($this->getPaymentToken());

$session = $stripe->checkout->sessions->retrieve($data['object']['id']);
$items = $session->line_items;

$this->getLogger()->debug("Line items: " . $items->toJSON());

and I get the same error.

remi-stripe commented 1 month ago

@php4fan The line_items property is what we call includable which means it's not return by default in the API and isn't part of the Event payload. You have to call the Retrieve PaymentLink API and use our Expand feature (docs, video) and explicitly ask for the line_items property to be returned.

This is more an integration support question so I'd recommend reaching out to our support team via https://support.stripe.com/contact or talking to developers on our Discord server if you have follow up questions.