4

Using stripe's API and the Integration Builder, before you go to the checkout page, you can get the id of the Ceckout Sessions in the create-checkout-session.php file. (https://stripe.com/docs/api/checkout/sessions/object?lang=php)

My plan is to use that session ID later on to get the customer's ID and the subscription's ID.

But does these sessions expires? Meaning after some time, i won't be able retrieve that session? (https://stripe.com/docs/api/checkout/sessions/retrieve?lang=php).

Ps: I don't think it matters, but i'm using PHP as my backend language.

2 Answers 2

6

To be clear,

  • You can retrieve the full details of a Checkout Session any time using the API : https://stripe.com/docs/api/checkout/sessions/retrieve. There is no "expiry" for this.
  • You can only access the full data of an Event e.g. checkout.session.completed if it was created within the last 13 months from the Dashboard. Events older than 30 days will only provide a summary view. Events within the last 30 days will be fully visible via the API and Dashboard. (source)
  • A Checkout Session expires 24 hours after they’re created (i.e. the customer cannot access/pay via the Checkout url), but you can shorten the expiration time by setting expires_at (source). Note that you can still access the details of the Checkout Session as mentioned in the first point.

Ideally, you should rely on webhooks :

The checkout.session.completed event will contain the customer and subscription.

1
  • That's exactly it. I was worried I wouldn't be able to retrieve the details of a Checkout Session after some time. But if those details are accessible at anytime then thats ok. I'll just check in my DB if the user's ID is empty, and if it is, i'll fetch it retrieving it from the check out session ID. But later i'll look into webhooks!
    – Soulss
    Commented Oct 17, 2021 at 5:38
1

Checkout sessions aren't stored forever. They live for short period of time and after that they expire. However, if you need the customer and subscription id, you can follow the following steps (same i use in one of my project).

  1. Create Checkout Session and once user redirect back to the website after successful checkout, retrieve the session using the session Id
  2. Inside the retrieve session response, there is a customer (customerId) and subscriptionId if the payment mode is subscription.
  3. enter image description here
  4. Get the customer (customerId) and subscriptionId from the session retrieve api response and store both into database for later use.
  5. Further to get the customer information, you can use the customer api and pass the customerId to get the detailed information about that customer.
  6. enter image description here
  7. Similarly, you can use the subscription api to retrieve the particular subscription.
  8. enter image description here
3
  • I saw this. But assuming the user doesn't make it to the success page (like if right after his subscription payment is completed he closes the tab, or something prevents him to getting to the success page) would this still works? That's my only concern about this method...
    – Soulss
    Commented Oct 17, 2021 at 4:03
  • You should not rely on the success redirect to perform important actions. There is no guarantee that the user will not close the browser before that.
    – alex
    Commented Oct 17, 2021 at 4:52
  • I've never messed with webhooks but i guess i'll have to, apparently is the only reliable way to do this
    – Soulss
    Commented Oct 17, 2021 at 4:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.