#2Clutch-customer-invoice
1 messages · Page 1 of 1 (latest)
Hey there. Yeah you won't see invoices in that event. If you want to you can list invoices by customer using https://stripe.com/docs/api/invoices/list
What's the goal here though?
i'm offering a subscription through stripe. so when a user signs up, a customer and a subscription are created.
Gotcha. I'm not sure I would listen to customer.created in that case then.
Currently, I have the following:
# Handle the event
if _event:
stripe_session = _event['data']['object']
stripe_object.customer_id = stripe_session['id']
subscription = stripe.Subscription.list(customer=stripe_object.customer_id)
price_id = subscription['items']['data'][0]['price']['id']
_email = stripe.Customer.retrieve(stripe_object.customer_id)['email']
_user_id = await get_user_id_from_email(_email)
if _event['type'] == 'customer.created':
if price_id == enhanced_price_id:
stripe_object.subscription_plan = 'Enhanced'
elif price_id == premier_price_id:
stripe_object.subscription_plan = 'Premier'
_update_subscription_db_request = update_subscription_fields(stripe_object.customer_id, stripe_object.subscription_plan, _email)
_update_auth0_request = await update_subscription_data(_user_id, stripe_object.dict())
elif _event['type'] == 'customer.subscription.updated':
if price_id == enhanced_price_id:
stripe_object.subscription_plan = 'Enhanced'
elif price_id == premier_price_id:
stripe_object.subscription_plan = 'Premier'
_update_subscription_db_request = update_subscription_fields(stripe_object.customer_id, stripe_object.subscription_plan, _email)
_update_auth0_request = await update_subscription_data(_user_id, stripe_object.dict())
elif _event['type'] == 'customer.subscription.deleted':
stripe_object.subscription_plan = 'Essentials'
_update_subscription_db_request = update_subscription_fields(stripe_object.customer_id, stripe_object.subscription_plan, _email)
_update_auth0_request = await update_subscription_data(_user_id, stripe_object.dict())
this allows to catch whenever a subscription is created, updated, or deleted.
i would like to add the ability to store each relevant transaction to a database when a payment is succesfully processed.
specifically, i would like to get the following info from stripe:
invoice_period_start_date
invoice_period_end_date
invoice_description
invoice_creation_timestamp
invoice_due_timestamp
invoice_paid_timestamp
Gotcha, I'm a bit confused why you are relying on customer.created in that case. You likely want to be listening for Subscription and Invoice events, no?
Ah I see you are listening to the subscription updated/deleted events
So you likely want invoice.finalized and invoice.paid as well
For the description, due_timestamp, and paid_timestamp.
The period and creation timestamp you can get from the Subscription billing cycle
is there a way to do it without having to listen to more webhooks? because how would i know which is associated with which.
when a customer & subscription is created, it would be nice if i could just get the invoice_id associated with that transaction, and use it to query the invoice endpoint
same for when a subscription is updated as well
that way i have everything in one place, and i know that i'm storing the right thing.
is this something i can do?
Sure, the Subscription has a latest_invoice property
This would be the best way without using Webhooks if you want to go that route
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
So when you retrieve a Sub you can expand the latest_invoice and get all the details of the most recent invoice that was created.
So you can do this for new Subs or when a Sub renews or updates
when interacting with the invoice endpoint, i'm confused by this section of the response payload:
"data": [
{
"id": "il_1Ku1Z72eZvKYlo2CUE321BlT",
"object": "line_item",
"amount": 2000,
"currency": "usd",
"description": "My First Invoice Item (created for API docs)",
"discount_amounts": [],
"discountable": true,
"discounts": [],
"invoice_item": "ii_1Ku1Z72eZvKYlo2CblENtwLE",
"livemode": false,
"metadata": {},
"period": {
"end": 1651268489,
"start": 1651268489
},
"price": {
"id": "price_1Ku1LF2eZvKYlo2CRds0QM1q",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1651267629,
"currency": "usd",
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LbDllwUwr7gf3V",
"recurring": null,
"tax_behavior": "unspecified",
"tiers_mode": null,
"transform_quantity": null,
"type": "one_time",
"unit_amount": 2000,
"unit_amount_decimal": "2000"
},
what is the first section of data supposed to be?
fyi, i got this from https://stripe.com/docs/api/invoices/retrieve
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
That is an invoice line item. See: https://stripe.com/docs/api/invoices/line_item
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
It represents all the details about the line item of your invoice.
Since an invoice could have multiple items, it can have mutliple line items.
I'm not sure what you mean? It does have a price
sorry, i meant price_id
Prices sit within line items as they are one piece of the object. They are not top level though
@grave cave I hate to do this to you but I have to step away and we don't monitor this channel on weekends. We will be back here starting at around 8pm EST on Sunday!
no problem. thank you so much for the help. i'll continue troubleshooting.
I'm going to archive this thread, but you can continue to chat in the main channel and some other folks might be around to help!
enjoy your weekend!