#aooa1009
1 messages · Page 1 of 1 (latest)
Are you referring specifically to the case of a checkout session that included both?
No
Ok, can you explain the scenario here a bit more then?
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
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
is it a subscription even
because its a one time payment right
also, how can I access how many units of Credits have been bought
to add here
Can you share an example event? I need to see what you're doing
where can I find these events sir im not following
It looks like you're creating subscriptions via checkout based on the code you're showing me
yes, this is for the subscription plan product i made eariler
now im trying to add credits
Well you're listening for checkout.session.completed events, eg evt_123
You can see recent events in your dashboard logs: https://dashboard.stripe.com/test/events
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
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
Can you please copy the event id from the line ending in 7u
in the image in the message i replied to
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
Looking at your session 👀
okayy
the event is evt_1NsTeBK0N66af8nJ3NDJJ07u, by the way: https://dashboard.stripe.com/test/events/evt_1NsTeBK0N66af8nJ3NDJJ07u
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
The session you shared is a one time payment only
https://dashboard.stripe.com/test/logs/req_77wEIeHvIM7ncX
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
mode=payment
So to get these line items, you'd retrieve the checkout session (cs_test_123...) and expand the 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.
expand[]='line_items.data.price.product if you want to get to the product details
Checkout events don't inlude the line_items data by default, so you have to request it during retrieval using expansion
there's not data or product attr
guys, sorry
but im still not clear
as to how i can access the mode
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
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
The mode property is included on the Checkout Session object
and further, where can i find the paymentIntent object
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)
this you mean
so if i console logged session.mode i'd get it
Correct, I suggest trying it out in test mode
where in the docs can i find what all i can access through the paymentIntent object
yes, all still in dev rn
The API reference shows all the fields for objects https://stripe.com/docs/api/payment_intents
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?
Quantity would be available in the Checkout Session or Subscription object https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items-data-quantity
PaymentIntents don't have a concept of quantity, they are just used to charge a certain amount
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
I typically use the search in the upper right. If you search from:aooa1009 that will show you threads you have messaged in
Cool, thanks!