Basically, following the stripe docs, you create a checkout session
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [[
'price' => $prices->data[0]->id,
'quantity' => 1,
]],
'mode' => 'subscription',
'success_url' => $YOUR_DOMAIN . '/success.html?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
The user gets redirected to stripe, completes the transaction, gets redirected back to the website, and the events
for the webhook
fire.
Now in the webhook listener, you can get the info about the payment and customer, but I have no idea who that customer is on my end.
So, can I attach some custom data to the Session::create
so I can identify witch user made the purchase in the webhook listener on the server ?
>Solution :
You can pass a metadata
object in your session create call to Stripe with all custom data that you’d like to add for eg, customerId
of your database. When the webhook is triggered, stripe will post the metadata
object along with other fields listed here.
So your code should look like
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [[
'price' => $prices->data[0]->id,
'quantity' => 1,
]],
'mode' => 'subscription',
'success_url' => $YOUR_DOMAIN . '/success.html?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
'metadata' => [
'customerId' => 'some-value'
]
]);