#2clutch-500-list

1 messages ยท Page 1 of 1 (latest)

proper solar
#

It can happen if it timeouts, best option is to retry

magic cedar
#

what do you mean by timeouts?

#

i'm processing a webhook notification. how can I guarantee a result everytime?

proper solar
#

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

magic cedar
#

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

proper solar
#

listen to customer.subscription.updated which has the Subscription id so that you know which one changed

magic cedar
#

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
magic cedar
proper solar
#

yeah hard to say with so much abstraction. But really don't call the API there, use your own database, way faster

magic cedar
#

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)

proper solar
#

it really depends which event you are handling

#

like if you get a payout.created then no

magic cedar
#

customer-created, customer.subscription.updated and customer.subscription.deleted

#

these are the events i'm handling.

proper solar
#

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

magic cedar
#

so i'm better listening to customer.subscription.created instead?

#

of customer-created i mean?

proper solar
#

I mean I'd listen to both, I would just have different code handling each one

magic cedar
#

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?

proper solar
#

I recommend trying in Test mode, it takes a few seconds and you can see the raw event with all the information you need!

magic cedar
#
"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.

proper solar
#

only look at price and ignore plan

magic cedar
#

this may be a stupid question, but can i do this?

if _event['type'] == 'customer.created':
    if _event['type'] == 'customer.subscription.created':
        pass
proper solar
#

I don't understand what that code could mean

#

if an event is type A it can't also be type B

magic cedar
#

let's say a new customer signs up for a subscription.

#

then you would have both events no?

proper solar
#

but each event would be send completely separately

magic cedar
#

got it.

#

thanks for confirming.

proper solar
#

sure!

magic cedar
#

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)
timid wedge
#

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?

magic cedar
#

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

timid wedge
#

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.

magic cedar
#

right.

timid wedge
#

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?

magic cedar
#

but this is the part where i'm trying to store it in the database.

magic cedar
#

not sure i understand.

timid wedge
#

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

magic cedar
#

i didn't know about invoice.paid. will go ahead and implement something for it.

timid wedge
magic cedar
#

Thank you!

#

is there a url for invoice.paid showing a sample response object?

timid wedge
#

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

magic cedar
#

invoice_id will always be unique for each payment correct?

timid wedge
magic cedar
timid wedge
#

yes, it's unique

magic cedar
#

thank you!

magic cedar
#

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.

timid wedge
#

can you share the invoice id? i'll take a look from there

magic cedar
#

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

timid wedge
#

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?

magic cedar
#

to match with the corresponding user in our database before saving it to the database.

timid wedge
magic cedar
#

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

onyx loom
#

๐Ÿ‘‹ stepping in

onyx loom