#aooa1009

1 messages · Page 1 of 1 (latest)

chrome cairnBOT
quiet wedge
#

Are you referring specifically to the case of a checkout session that included both?

shrewd sail
#

No

quiet wedge
#

Ok, can you explain the scenario here a bit more then?

shrewd sail
#

Say there's a monthly subscription called Pro
And another product called Credits - where they enter how many credits they want to buy and then are redirected to buy it
The buying happens successfully
But now, through the webhook, I want to update an attr of the User in my db
But I dont know what event to be looking for in this case or from what I should be retrieving this one time buy of say X credits

quiet wedge
#

Can you share an example event ID like what you'd be looking at?

#

For example, you might wish to look at the session line items, or the subscription's latest_invoice items

shrewd sail
#

Okay

#

I'm trying to find it

#

one moment

#

yes, that's my question

shrewd sail
#

because its a one time payment right

#

also, how can I access how many units of Credits have been bought

#

to add here

quiet wedge
#

Can you share an example event? I need to see what you're doing

shrewd sail
#

where can I find these events sir im not following

quiet wedge
#

It looks like you're creating subscriptions via checkout based on the code you're showing me

shrewd sail
#

yes, this is for the subscription plan product i made eariler

#

now im trying to add credits

quiet wedge
#

Well you're listening for checkout.session.completed events, eg evt_123

shrewd sail
#

Checking

#

internet is v slow

#

this is in my webhook stripe:listen terminal

#

I just successfully bought 60$ of Credits

#

is this what you were asking to see

#

`import { db } from "@/lib/db";
import { stripe } from "@/lib/stripe";
import { headers } from "next/headers";
import type Stripe from "stripe";

export async function POST(request: Request) {
const body = await request.text();
const signature = headers().get("Stripe-Signature") ?? "";

let event: Stripe.Event;

try {
event = stripe.webhooks.constructEvent(
body,
signature,
process.env.STRIPE_WEBHOOK_SECRET || "",
);
} catch (err) {
return new Response(
Webhook Error: ${err instanceof Error ? err.message : "Unknown Error"},
{ status: 400 },
);
}

const session = event.data.object as Stripe.Checkout.Session;

if (!session?.metadata?.userId) {
return new Response(null, {
status: 200,
});
}

if (event.type === "checkout.session.completed") {
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,
    ),
  },
});

}

if (event.type === "invoice.payment_succeeded") {
// Retrieve the subscription details from Stripe.
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string,
);

// Update the price id and set the new period end.
await db.user.update({
  where: {
    stripeSubscriptionId: subscription.id,
  },
  data: {
    stripePriceId: subscription.items.data[0].price.id,
    stripeCurrentPeriodEnd: new Date(
      subscription.current_period_end * 1000,
    ),
  },
});

}

return new Response(null, { status: 200 });
}`

#

If the above is my app/api/webhooks/stripe/route.ts file

#

How do I have it first check from the event if its a payoutIntent or a subscription

#

based on that, I can do an if-else here, and update the db accordingly

quiet wedge
# shrewd sail

Can you please copy the event id from the line ending in 7u

#

in the image in the message i replied to

shrewd sail
#

one moment

#

sending

#

cs_test_a1frErJJVRJ0Oa8WvEaNfyEYvXJ9rGxPonZSsLdxqqhuS4HWxvBSsEuEny

#

Hi

#

I understood it!

#

i want to access this mode

#

how can i do that with the event object

quiet wedge
#

Looking at your session 👀

shrewd sail
#

okayy

quiet wedge
#

mode=payment

shrewd sail
#

mhm

#

ack.

quiet wedge
#

So to get these line items, you'd retrieve the checkout session (cs_test_123...) and expand the line_items:

#

expand[]='line_items.data.price.product if you want to get to the product details

shrewd sail
#

one moment

#

trying to understand

quiet wedge
#

Checkout events don't inlude the line_items data by default, so you have to request it during retrieval using expansion

chrome cairnBOT
shrewd sail
#

there's not data or product attr

#

guys, sorry
but im still not clear

#

as to how i can access the mode

remote nimbus
#

Prices are connected to a product, you can get the product ID from the price

#

As synthrider noted it would be easiest to do this by expanding that price while retrieving the session

shrewd sail
#

please give me a moment to try

#

i'll get back asap

#

please dont close the thread

#

guys, not asking about price, asking about mode

shrewd sail
#

if its subscription or payment

remote nimbus
#

The mode property is included on the Checkout Session object

shrewd sail
#

and further, where can i find the paymentIntent object

remote nimbus
#

If you are in payment mode, the intent will be directly on the Checkout Session, if you are in subscription mode it will be on the Subscription's invoice(s)

shrewd sail
#

so if i console logged session.mode i'd get it

remote nimbus
#

Correct, I suggest trying it out in test mode

shrewd sail
#

where in the docs can i find what all i can access through the paymentIntent object

shrewd sail
remote nimbus
shrewd sail
#

Thank you!

#

Guys, here, in all these attributes, there's no mention of quantity

#

How many credits were bought

#

only the amount spent

#

I can get it by dividing, but isnt it present in the object?

remote nimbus
#

PaymentIntents don't have a concept of quantity, they are just used to charge a certain amount

shrewd sail
#

Thank you so much guys

#

for making everythig so clear

#

you can close this thread now

#

Also, how can we easily access old threads here

#

I might need to come back to this ifo

#

info

remote nimbus
#

I typically use the search in the upper right. If you search from:aooa1009 that will show you threads you have messaged in

shrewd sail
#

Cool, thanks!