#fabbelevin
1 messages · Page 1 of 1 (latest)
Hello 👋
Where exactly are you setting the metadata? We don't automatically copy metadata over to associated subscription objects.
https://stripe.com/docs/api/payment_links/payment_links/create#create_payment_link-metadata
It is however copied over to Checkout object that the payment link creates. Checkout object also has the client_reference_id param. So you'd likely want to listen to checkout.session.completed event instead
I've tried setting it in the dashboard and just use the payment link urls I get there. And I've tried doing something like this:
api.post('/stripe-checkout', auth, catchAsyncErrors(async (req, res) => {
const session = await stripe.checkout.sessions.create({
success_url: 'http://localhost:9000/buy-now/diagnos/17198',
cancel_url: 'http://localhost:9000/buy-now/diagnos/17198',
line_items: [{price: req.body.product, quantity: 1}],
mode: 'subscription',
client_reference_id: req.body.accountId,
metadata: {
typeTest: 'smallPack',
companySizeTest: '0-5'
}
});
res.json({ url: session.url });
})); ```
I also have metadata set for that product
Let's take a step back, are you trying to use PaymentLinks or Checkout Session or both?
What's your end goal?
It doesn't really matter for me, I've just tried both (seperately) to see if they would give something different.
The end goal is for the customer to be able to buy different subscription plans (payed monthly). There will be quite a lot of different plans with different prices so I think it would be nice to not have to setup different PaymentLinks for each, so my guess is that Checkout Session would be more comfortable?
The thing I'm struggling with at the moment is that when a new user pays I don't know in my webhook what user in my database completed the payment so I can't link it to the users account and I can't activate the account.
Gotcha. So if you want to get the metadata on customer.subscription.created event then you need to set it on the subscription when you create the checkout session. Because the code you shared is setting the metadata on the Checkout Session object (cs_xxxx) which is separate from a Subscription object (sub_xxx)
You can set the metadata on the subscription using subscription_data.metadata when you create the checkout session
https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-subscription_data-metadata
Ah okay, do you recommend passing a userId in the metadata there as well then and not caring about the client_reference_id?
Yeah you can pass it in the metadata if you only want to deal with customer.subscription.created event
client_reference_id is a property of Checkout Session object (cs_xx) so you can only see it on events that deliver checkout object (like checkout.session.completed)
I am actually a little confused as to which events I need to be listening to. What are the ones I need to listen to if I want to create a fully working subscription service? I won't have any trails connected to stripe.
It all depends on your usecase. We have a doc that covers each event in detail
https://stripe.com/docs/billing/subscriptions/webhooks
I'd recommend reading through it as it covers everything and should answer all/most of your questions.
So technically if I only want to check if the customer has paid and should get access it would be enough to listen to customer.created and customer.subscription.updated ?
That could be a way to do it. Most users like to listen to events related to the invoices actually being paid but you can just listen to events for the subscription itself if that supports your usecase properly