I have a web application. Users need to buy different packs of credits to use this web application. For instance, one pack of 5000 credits costs 4.99 USD; one pack of 10000 credits costs 7.99 USD. I use Stripe to manage the purchase. I can use the following code to create a checkout session where users can adjust the quantity to buy priceId
.
const session = await stripe.checkout.sessions.create({
customer: customer.id,
mode: "payment",
line_items: [
{
price: priceId,
quantity: 1,
adjustable_quantity: {
enabled: true,
minimum: 1,
maximum: 10,
},
},
],
success_url: successUrl,
expand: ['line_items'],
});
Then, given a user, I would like to write a function to get his purchase history. I'm thinking of using stripe.charges.list
. But in a charge, we know the amount the user paid, we don't know what price/item he bought. Does anyone know how to solve that?
You could save all the information in your database at the time a payment happens by handling a webhook, and then you can query what you need. https://stripe.com/docs/payments/checkout/fulfill-orders
You can list CheckoutSessions by a Stripe customer ID (https://stripe.com/docs/api/checkout/sessions/list#list_checkout_sessions-customer) and expand the data.line_items
field (https://stripe.com/docs/expand#lists) to see what the customer bought in that Session.