#2clutch-500-list
1 messages ยท Page 1 of 1 (latest)
what do you mean by timeouts?
i'm processing a webhook notification. how can I guarantee a result everytime?
you can't
Our API shouldn't return a 500, but it can always happen and you have to be resilient to that
alternatively, store data in your own database to rely less on API requests like this
is there another safer or more guaranteed way to do this?
i'm trying to catch subscription changes and update our database when a payment is made for a subscription
listen to customer.subscription.updated which has the Subscription id so that you know which one changed
i have more logic, but in troubleshooting, it sure looks like the error is coming from the request.
here's what i'm doing:
stripe_session = _event['data']['object']
stripe_object.customer_id = stripe_session['id']
subscription = await stripe.Subscription.list(customer=stripe_object.customer_id)
return subscription
i was doing this initially, but there was a lot of duplicated code since i try to streamline it a little in getting some of this data organized and ready to be saved to our database.
yeah hard to say with so much abstraction. But really don't call the API there, use your own database, way faster
is it possible to get the subscription_id from the event object?
i'm looking to update the database though. when a subscription has been updated (e.g. when a user goes from Subscription A to Subscription B)
it really depends which event you are handling
like if you get a payout.created then no
customer-created, customer.subscription.updated and customer.subscription.deleted
these are the events i'm handling.
the last 2 are about the Subscription and event['data']['object'] is already the entire Subscription object
customer.created represents the Customer so it doesn't have the Subscription information no
so i'm better listening to customer.subscription.created instead?
of customer-created i mean?
I mean I'd listen to both, I would just have different code handling each one
so one thing that caused me to make the API call was that I couldn't what was the price_id associated with the most recent payment event.
the price_id is what helps me determine which plan the user is one.
in looking at event['data']['object'], what would be the line item associated with the price_id?
I recommend trying in Test mode, it takes a few seconds and you can see the raw event with all the information you need!
"items": {
"object": "list",
"data": [
{
"id": "si_LAZ4nOKARo7nRQ",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1645119513,
"metadata": {},
"plan": {
"id": "price_1IlNlhJF8oW0qG5sGAFNxIlW",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 5000,
"amount_decimal": "5000",
"billing_scheme": "per_unit",
"created": 1619656213,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_JOA5DMWPvJ1xOO",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1IlNlhJF8oW0qG5sGAFNxIlW",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1619656213,
"currency": "usd",
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_JOA5DMWPvJ1xOO",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
this is the section I care. there is a plan object and a price object.
i'm guessing i should focus on the price object?
is it always going to be the same as plan?
that's the part that's confusing me a little.
only look at price and ignore plan
this may be a stupid question, but can i do this?
if _event['type'] == 'customer.created':
if _event['type'] == 'customer.subscription.created':
pass
I don't understand what that code could mean
if an event is type A it can't also be type B
let's say a new customer signs up for a subscription.
then you would have both events no?
but each event would be send completely separately
sure!
ok so is it not recommend to make api calls at all?
here's another call i'm making further down:
invoice = stripe.Invoice.retrieve(transaction.invoice_id)
stepping in for koopajah. I don't really understand what do you mean by it's not recommended to make api calls at all? can you explain more on what you're trying to do by retrieving the invoice?
i'm handling a webhook notification, and in trying to fetch the information around the new subscription, i was told (paraphrasing) that making api calls is not recommended when dealing with event from the webhook.
so now i'm asking if that was a general statement or just about the call i was making
from what i understood, the suggestion made was that it's faster to store the relevant data in your own database and retrieve it from there, than make an API request.
right.
Stripe will send invoice.paid events which you can save to your DB, so is there a need to make an API call to Stripe to retrieve the invoice again?
but this is the part where i'm trying to store it in the database.
mind expanding on this?
not sure i understand.
When an invoice is paid, Stripe sends an invoice.paid event to your webhook endpoint. Upon receipt of that event, you can save the invoice details from the invoice.paid event to your DB. So i'm a bit confused why you're retrieving the invoice by making an API call
i didn't know about invoice.paid. will go ahead and implement something for it.
i think this guide might be helpful if you're just getting started with subscriptions : https://stripe.com/docs/billing/subscriptions/overview
Thank you!
is there a url for invoice.paid showing a sample response object?
the closest thing i could find is https://stripe.com/docs/api/invoices/pay
not really. you can easily test by creating a Subscription via the Dashboard / API and find the corresponding event via the Dashboard to see how it looks. We have a list of test cards here : https://stripe.com/docs/testing
You can also use the Stripe CLI for testing webhooks e.g. to listen for webhooks and trigger specific events : https://stripe.com/docs/webhooks/test
invoice_id will always be unique for each payment correct?
are you referring to this id : https://stripe.com/docs/api/invoices/object#invoice_object-id?
yup
yes, it's unique
thank you!
last question.
when receiving an event, how can i get the email associated with that customer?
i see the customer_id, but i can't seem to find their email address.
can you share the invoice id? i'll take a look from there
i'm able to get the email when listening to invoice.paid
but when dealing with customer.subscription.created or customer.subscription.updated, i only get the customer_id
the customer.subscription.* events use the Subscription object. It's expected that it doesn't have the customer's email. Why do you need the customer email?
to match with the corresponding user in our database before saving it to the database.
Instead of using the customer email, you can use the customer id instead : https://stripe.com/docs/api/customers/object#customer_object-id
no i need both
the customer_id and the email
user_id and customer_id are 2 different things.
we generate the user_id, and customer_id comes from stripe
๐ stepping in
You should have the email from https://stripe.com/docs/api/customers/object#customer_object-email
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.