#pratikco
1 messages ยท Page 1 of 1 (latest)
Hi there
Hii
Where are you setting the metadata exactly?
Are you setting it as subcription_data.metadata?
which creating a session for checkout
Right. But are you setting it here: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-subscription_data-metadata?
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
checkout_session = stripe.checkout.Session.create(
payment_method_types=["card"],
line_items=[
{
"price": price["id"],
"quantity": 1,
}
],
metadata={
"user_id": request.user.id,
"art_price": art_price,
"amount": amount,
"free_trial": free_trial,
},
mode="subscription",
customer=customer_id,
success_url='http://127.0.0.1:8000/success' + "?session_id={CHECKOUT_SESSION_ID}",
cancel_url='http://127.0.0.1:8000/failed/',
subscription_data=subscription_data,
)
like this
no
Got it
So in that case, the metadata is only being set on the Checkout Session object
If you want the metadata to be on the Subscription itself, then you should set it as the subscription_data.metadata
That way, each time a payment is made, you can check the customer.subscription.updated webhook and you will see that metadata there
(Or you can retrieve the Subscription based on the PaymentIntent if you are listening to payment_intent.succeeded for instance)
can you make changes in my code for this attribute
Do you mean can I help you with the syntax?
yeah
so that I can write it in my this code
So you would move your metadata object into your subscription_data variable
customer_id = stripe.Customer.create(
name= 'abcd,
)
stripe.Customer.modify(
customer_id,
metadata={
"user_id": request.user.id,
"art_price": art_price,
"amount": amount,
"free_trial": free_trial,
},
)
instance = request.user
instance.stripe_customer_id = customer_id["id"]
instance.save()
# create checkout session for recurring subscription
checkout_session = stripe.checkout.Session.create(
payment_method_types=["card"],
line_items=[
{
"price": price["id"],
"quantity": 1,
}
],
mode="subscription",
customer=customer_id,
success_url='http://127.0.0.1:8000/success' + "?session_id={CHECKOUT_SESSION_ID}",
cancel_url='http://127.0.0.1:8000/failed/',
subscription_data=subscription_data,
)
So you means I should do it like this right?
No that is putting the metadata at the Customer level. Which you can do if you want the metadata to be there. But doesn't really work if the Customer has multiple Subscriptions. Where is your subscription_data variable being created that you are passing? See: subscription_data=subscription_data in your code
subscription_data = {
"trial_end": 1665269153,
metadata={
"user_id": request.user.id,
"art_price": art_price,
"amount": amount,
"free_trial": free_trial,
},
}
like this? but the syntax is not right
gettng error
ohh ok got it
subscription_data = {
"trial_end": 1665269153,
'metadata':{
"user_id": request.user.id,
"art_price": art_price,
"amount": amount,
"free_trial": free_trial,
},
}
now looks good
๐
now how will I extract data in my webhook event
if event['type'] == 'checkout.session.completed':
data = session["subscription_data"]["metadata"]["amount"]
Like this?
Nope
The metadata will now be on the Subscription object
so instead of using if event['type'] == 'checkout.session.completed':
You would use if event['type'] == 'customer.subscription.created':
And then you would handle the Subscription object instead of the Checkout Session
so in every month when stripe make payment from same account it will trigger 'customer.subscription.created' this event on sucessful transaction?
Nope, that is just on creation. On renewal you would use customer.subscription.updated
Take a look at: https://stripe.com/docs/billing/subscriptions/webhooks#events
That details the different events you can use and what you might use them for
I will receive this event every month when stipe successfully deducts the amount from the user account right?
Correct
I recommend reading this doc: https://stripe.com/docs/billing/subscriptions/webhooks
I tested this code at first I am only receiving checkout.session.completed not payment_intent.succeded
and how will I extract subscription metadata?
My colleague answered this above: #1028008499030007829 message
The event data will contain the subscription object, which will have the metadata attached
how will I update subscription every time
because its a recurring job strip itself will deduct the amount
You don't have to update anything
That event is generated each billing cycle
Ah apologies. No it's not.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Which contains a subscription id: https://stripe.com/docs/api/invoices/object#invoice_object-subscription
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.