#aooa-checkout-items
1 messages ยท Page 1 of 1 (latest)
Is this payment intent related to a Checkout Session?
yeah
if (event.type === 'checkout.session.completed') {
// console.log("MODE: "+session.mode)
const session1 = await stripe.checkout.sessions.retrieve(session.id, {
expand: ['line_items.data.price.product']
})
if (session.mode == 'subscription') {
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string
)
await db.user.update({
where: {
externalId: session.metadata.userId
},
data: {
stripeSubscriptionId: subscription.id,
stripeCustomerId: subscription.customer as string,
stripePriceId: subscription.items.data[0].price.id,
stripeCurrentPeriodEnd: new Date(
subscription.current_period_end * 1000
)
}
})
const currentPlan = storeSubscriptionPlans.find(
plan => plan.stripePriceId === subscription.items.data[0].price.id
)
let temp = (await kv.get(
`user:creditCount:${session.metadata.userId}`
)) as number
let temp2 = temp + (currentPlan?.creditsGiven ?? 0)
await kv.set(`user:creditCount:${session.metadata.userId}`, temp2)
await db.creditTransaction.create({
data: {
type: 'PURCHASE', // or TransactionType.SPEND
amount: currentPlan?.creditsGiven || 0, // replace with the actual amount
userId: session.metadata.userId, // replace with the actual user ID
chatId: '', // replace with the actual chat ID, if applicable
plan: currentPlan?.name // replace with the actual plan type, if applicable
}
})
}
if (session.mode == 'payment') {
const paymentIntent = await stripe.paymentIntents.retrieve(
session.payment_intent as string
)
const currentPlan = storeSubscriptionPlans.find(
plan => plan.stripePriceId === paymentIntent.lines.data[0].price.id
)
}
}
}
can you please clarify something for me
i have two types of products
a subscription plan and a standalone product - buying a pack of 50 credits - a token used on the site
now, is invoice.payment_succeeded ever fired for buying the credit pack
or only when a subscription is automatically successfully renewed at the end of the month
sorry you pasted a wall of code with not much context ๐
let's take a step back
do you have an example checkout session for your use-case?
This way we can be on the same page about the details
How can I share an example checkout session
what i pasted is a section of the code
it has what happens when checkout.session.completed event is fired
my doubt is, just like when its a subscription type product, we can access the price id as so - subscription.items.data[0].price.id
how do we do it when its a payment type product
I mean the session.mode
there's a red squiggly line because lines doesnt exist on paymentIntent
๐ hopping in here since hanzo has to head out
PaymentIntents don't include price data - if you want this information, you should instead retrieve the line items of the Checkout Session https://stripe.com/docs/api/checkout/sessions/line_items
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
while it does work for subscription
i just wanna know i can get the price id when the session.mode is payment
It's because Subscriptions do store line items - Payment Intents are a different API and don't have line items at all
The only way to do this for Payment Intents is by retrieving the Chcekout Session's line items
aooa-checkout-items
payments are for standalone products right
could you please give me what i should do to get the price id in the payment case here
You're already doing that in lines 41-45 - that should give you all the line items for the checkout session
im confused, these are the logs
this doesnt have the price id
Instead of doing console.log('ITEM:' + item.price?.product) do console.log('ITEM:' + JSON.stringify(item.price?))