'With Stripe API, How To Get session_id of Checkout Session That Created a payment_intent Object, From payment_intent.succeeded Webhook
I am working on developing and testing a Stripe integration, written in PHP, and it's working beautifully. I can create sessions, redirect to the checkout form, and when the payments are complete it sends an event to my webhhook script, which successfully processes the information about the payment going through.
When I create a session, I store the data about the form filled out on my site, in a database, and when the payment goes through, I store information in a different table, which is great.
The problem I'm having is that I don't know how to link up the information about the successful payment, with the session that generated it.
Linking up these data is essential to me, because I want to track which sessions actually result in successful payments, so that I can analyze the flow of the user interface and track conversion rates and analyze factors that lead to abandonment of the checkout session.
In some cases, it is easy to link these things up. For example, if there's only one session generated and one successful payment, associated with a given email in a given time-frame, I can just link them up. The problem is that I want to be able to deal with the (likely common) scenario where a person creates multiple sessions and abandons them. I cannot link the payment to the most recent session associated with the email, in this scenario, because it's possible that a single customer would create two sessions, but complete the payment on the first, earlier-created session.
I can't figure out how to access the session_id from the payment_intent object that is returned to my webhook. Some thoughts I have had about how to possibly approach this include:
- Listening for some other event in my webhook script, that occurs, that would possibly allow me to link the two records.
- Passing metadata to the session, such as a uniquely-generated ID, and then accessing that same metadata from the payment_intent object. However I cannot figure out from reading the Stripe documentation how metadata works and even if the metadata is passed from the session to the payment_intent object (the documentation does not explicitly state this or explain it, and the fact that the session_id is not passed makes me wonder if the metadata would be passed at all). I would prefer not to do this solution because it would require the additional step of generating a unique ID before generating the session, which would require more work on my end and also make my code more complex and involve more database calls and more potential steps that can go wrong (currently I am generating the session and then storing the information in response to the successful creation of the session), but I could tolerate it if there are really no better options.
I would like to follow "best practices" here, but it's not clear to me how Stripe intends people to link up or access the data, or if this is perhaps an oversight on their end.
If you give me example code I would prefer seeing it in PHP if possible but you don't need to show me any code at all; just giving me an abstract or general idea of how to accomplish this would be sufficient and I could come up with the coding details on my own.
Solution 1:[1]
The payment_intent.succeeded event gives you the PaymentIntent ID. You can use the CheckoutSessions "list" endpoint [0] to get the CheckoutSession that used that PaymentIntent ID by passing the payment_method parameter.
[0] https://stripe.com/docs/api/checkout/sessions/list#list_checkout_sessions-payment_intent
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hmunoz |
