#ayushh_webhooks
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1345118956327014451
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- ayushh_webhooks, 6 hours ago, 13 messages
Does my question provide much info or you need more?
Hi, that is expected as I assume you're setting the metadata on the Checkout object when you create it. If you'd like to see the metadata on the subscription object, you'd need to pass that information here: https://docs.stripe.com/api/checkout/sessions/create#create_checkout_session-subscription_data-metadata
Are you able to try this?
Sure
case "customer.subscription.created":
subscription = event.data.object;
status = subscription.status;
const metadata = event.data.object.subscription_data.metadata;
console.log("Event Data", event.data.object);
console.log("Metadata", metadata);
console.log("Subscription status : ", subscription.status);
console.log(`Subscription created. Status: ${status}`);
// Store the new subscription
await storage.updateUserSubscription(
metadata.userId,
subscription.id,
status,
);
break;
Should I handle the metadata like this?
No, you said you're using Checkout Sessions right?
Yes, I am using checkout session
I am creating subscription too
I should use the checkout.session.completed event only to handle the subscription change too?
When you create the CheckoutSessions, you pass the metadata on the subscription: https://docs.stripe.com/api/checkout/sessions/create#create_checkout_session-subscription_data-metadata
Separately, when you use the subscriptions API, you pass the metadata: https://docs.stripe.com/api/subscriptions/create#create_subscription-metadata
What does 'I should use the checkout.session.completed event only to handle the subscription change too' mean? Are you able to reword this?
I mean, i have price id for these plans
so they are like subscription
I can manipulate them through my profile
Like cancel the subs or change the plan
forgive me if i am using incorrect words/phrases
So what do you want to see now and where?
async updateUserSubscription(
userId: string,
subscriptionId: string,
subscriptionStatus: "active" | "past_due" | "canceled" | "incomplete" | "incomplete_expired" | "trialing" | "unpaid",
sessionId?: string
): Promise<User> {
try {
console.log(':pencil: Updating user subscription:', {
userId,
subscriptionId,
subscriptionStatus,
sessionId,
timestamp: new Date().toISOString()
});
const updateValues: Partial<User> = {
subscriptionId,
subscriptionStatus,
};
if (sessionId) {
updateValues.sessionId = sessionId;
console.log(':pencil: Including session ID in update:', sessionId);
}
const [user] = await db
.update(users)
.set(updateValues)
.where(eq(users.id, userId))
.returning();
if (!user) {
const error = new Error(`No user found with ID ${userId}`);
console.error(':x: Update failed:', error);
throw error;
}
console.log(':white_check_mark: User subscription updated successfully:', {
id: user.id,
subscriptionId: user.subscriptionId,
subscriptionStatus: user.subscriptionStatus,
sessionId: user.sessionId,
timestamp: new Date().toISOString()
});
return user;
} catch (error) {
console.error(':x: Error updating user subscription:', error);
throw error;
}
}
I want to update the subscription of the user
in the users table
I know I am missing some values here in the webhook for which I need help to handle events and get the correct data
Hi! Things starting with whsec_ are secrets, and shouldn't be shared. Just FYI.
Sorry, but did I share here?
In the form, ya. Just something to think about for next time.
oh wait I am so sorry
I'm taking over for my colleague; can you just sum up the issue for me if you don't mind?
I wanted to share the evt id
at the end I just wanted to update the user scription
Here you can see
And what error are you getting?
Or what issue are you seeing?
Also what's the Event ID? ๐
evt_1QxZIGKvfF85f0JWXnDuyZYp
wasn't getting error so far but I wanted to get the userId from the metadata
and fill in the required subscriptionId and the stripe session id
maybe I was handling events wrongly
What event were you listening to?
Ahh I see the code now.
You can actually provide Metadata to be added to the created Subscription here: https://docs.stripe.com/api/checkout/sessions/create#create_checkout_session-subscription_data-metadata
There's nothing wrong with the code you've got, it isn't working the way you expect because the Subscription currently has no Metadata.