#2Clutch-customer-invoice

1 messages · Page 1 of 1 (latest)

tough rose
#

What's the goal here though?

grave cave
#

i'm offering a subscription through stripe. so when a user signs up, a customer and a subscription are created.

tough rose
#

Gotcha. I'm not sure I would listen to customer.created in that case then.

grave cave
#

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
tough rose
#

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

grave cave
#

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?

tough rose
#

Sure, the Subscription has a latest_invoice property

#

This would be the best way without using Webhooks if you want to go that route

#

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

grave cave
#

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?

tough rose
#

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.

grave cave
#

why isn't everything under price then?

#

in theory each item should/would have a price

tough rose
#

I'm not sure what you mean? It does have a price

grave cave
#

sorry, i meant price_id

tough rose
#

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!

grave cave
#

no problem. thank you so much for the help. i'll continue troubleshooting.

tough rose
#

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!

grave cave
#

enjoy your weekend!