#aooa-checkout-items

1 messages ยท Page 1 of 1 (latest)

west dockBOT
red fern
#

Is this payment intent related to a Checkout Session?

urban light
#

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

red fern
#

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

west dockBOT
urban light
#

How can I share an example checkout session

urban light
#

there's a red squiggly line because lines doesnt exist on paymentIntent

warm sandal
#

๐Ÿ‘‹ hopping in here since hanzo has to head out

urban light
#

while it does work for subscription

#

i just wanna know i can get the price id when the session.mode is payment

warm sandal
#

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

west dockBOT
#

aooa-checkout-items

urban light
#

payments are for standalone products right

urban light
warm sandal
#

You're already doing that in lines 41-45 - that should give you all the line items for the checkout session

urban light
#

im confused, these are the logs
this doesnt have the price id

warm sandal
#

Instead of doing console.log('ITEM:' + item.price?.product) do console.log('ITEM:' + JSON.stringify(item.price?))