Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can I add a custom Id to Session::create so I can retrieve it in a webhook?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Read more

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading