Is there a more elegant way to retrieve a plan id from an invoice in a webhook handler? (Python)

522 views
Skip to first unread message

Anthony Kong

unread,
Nov 9, 2015, 1:33:11 PM11/9/15
to Stripe API Discussion
Hi

In my webhook handler of event 'invoice.payment_succeeded', I want to retrieve the plan id of a subscription as part of the processing.



e = stripe.Event.retrieve(eid)

print e.data['object'].lines.data[0].plan.id



The above show the code I have come up with. It does the job but it does not look very elegant at all. Is there a better way to write it?

Cheers

Matthew Arkin

unread,
Nov 9, 2015, 1:44:09 PM11/9/15
to Jake K.
you could retrieve the subscription from the invoice (invoice.subscription) and then the subscription object would include the plan information.

--
You received this message because you are subscribed to the Google Groups "Stripe API Discussion" group.
To post to this group, send email to api-d...@lists.stripe.com.
Visit this group at http://groups.google.com/a/lists.stripe.com/group/api-discuss/.

To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.

Anthony Kong

unread,
Nov 12, 2015, 12:42:47 AM11/12/15
to Stripe API Discussion
Hi Matt,

Thanks for the answer. Sorry I did not make myself very clear.

I find the expression e.data['object'].lines.data[0].plan.id  a bit obscure.  In the first  occurrence of 'data' it is a dictionary, but in the second occurrence it is a list. I want to see if there is any way to improve the readability.

The language I am using is python.

In your suggestion, do you mean to extract the invoice id from an event and then call the invoice API with the id?

Cheers

Jack Royal-Gordon

unread,
Nov 12, 2015, 1:02:57 AM11/12/15
to api-d...@lists.stripe.com
HI Anthony,

I’ll make a suggestion here (I use the Ruby API, but the data structure is similar): Assign the object to a variable that suggests what kind of object you’re dealing with (in this case, “invoice”). Most of the time, I’m looking at more than one field, so this not only makes your code clearer, but slightly more efficient (you only have to resolve e.data[‘object’] once. You can also assign the plan to an intermediate variable. Your code then becomes:

invoice = e.data[‘object’]
plan = invoice.lines.data[0].plan
print plan.id

However, if you’re really just looking at the one field and efficiency is foremost, what you wrote is best.

HTH

Blake

unread,
Sep 22, 2016, 8:34:11 AM9/22/16
to Stripe API Discussion
Hi,
on a related note, when working with subscriptions only, will there always be exactly one item in the "data" list?
Would I ever have to deal with more than one subscription occurring in this list?
Thanks

ITV plc (Registration No. 4967001) (ITV) is incorporated in England and Wales with its registered office at The London Television Centre, Upper Ground, London SE1 9LT. Please visit www.itv.com for further information.

This email and any attachments are intended solely for the addressee and may contain confidential, copyrighted and/or privileged information.  If you have received this message in error, please notify us and delete it.  Recipients should scan the email and any attachments for their own protection. 

This email does not conclude a binding agreement.  The views or opinions presented in this email do not necessarily represent those of ITV.

Remi J.

unread,
Sep 22, 2016, 8:41:13 AM9/22/16
to api-d...@lists.stripe.com
Hey Blake,

Invoices can definitely have more than one line item as you can add invoice items on your end for example as documented here [1]. Even when working only with subscriptions, this can still happen if you moved the customer to a new plan or updated the quantity as it would trigger proration invoice items to be created [2].

Currently, there can only be one `subscription` line item per invoice though there's no guarantee that this will not change in the future depending on improvements we release to the Subscriptions product.


--
You received this message because you are subscribed to the Google Groups "Stripe API Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss+unsubscribe@lists.stripe.com.
To post to this group, send email to api-d...@lists.stripe.com.
Reply all
Reply to author
Forward
0 new messages