question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InvoiceItem.api_retrieve() always fails

See original GitHub issue

InvoiceItem is … weird, to say the least.

There’s this whole thing going on:


    @classmethod
    def _stripe_object_to_invoice_items(cls, target_cls, data, invoice):
        lines = data.get("lines")
        if not lines:
            return []

        invoiceitems = []
        for line in lines.get("data", []):
            if invoice.stripe_id:
                save = True
                line.setdefault("invoice", invoice.stripe_id)

                if line.get("type") == "subscription":
                    # Lines for subscriptions need to be keyed based on invoice and
                    # subscription, because their id is *just* the subscription
                    # when received from Stripe. This means that future updates to
                    # a subscription will change previously saved invoices - Doing
                    # the composite key avoids this.
                    if not line["id"].startswith(invoice.stripe_id):
                        line["id"] = "{invoice_id}-{subscription_id}".format(
                            invoice_id=invoice.stripe_id,
                            subscription_id=line["id"])
            else:
                # Don't save invoice items for ephemeral invoices
                save = False

            line.setdefault("customer", invoice.customer.stripe_id)
            line.setdefault("date", int(dateformat.format(invoice.date, 'U')))

            item, _ = target_cls._get_or_create_from_stripe_object(
                line, refetch=False, save=save)
            invoiceitems.append(item)

        return invoiceitems

So, the stripe_id of InvoiceItem is, for subscriptions at least, never correct. I don’t even know what’s happening here. Stripe doesn’t seem to be using InvoiceItem by itself. The only InvoiceItem I get when doing InvoiceItem.list() is Unused time on $plan (with 100% off) after 09 Apr 2017, which is an item which begins with ii_.

I think dj-stripe might be mistaking InvoiceItem for SubscriptionItem. Or, well, I’m not sure. I don’t know what’s going on. Anyone more familiar with the system?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jleclanchecommented, Nov 14, 2020

In db7c7dd30a8853aec3477c3064fb5cf3c887fdde, a new management command has been implemented to migrate the old InvoiceItem IDs to the new format.

Why isn’t this part of database migrations?

  1. Because it’s potentially expensive to run for databases with large amounts of InvoiceItems
  2. It’s mostly harmless, but may lead to weird data inconsistencies
  3. The management command does it more safely, by actively retrieving and re-syncing the data from upstream for every InvoiceItem that uses the old format, one at a time. This means the command can fail / be interrupted, and be resumed at a later time (safely, too; it’s okay to only run it for a portion of the data).
1reaction
jleclanchecommented, Sep 3, 2020

Possibly… Stripe changed a lot about the II interface.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stripe API reference – Invoice Items – curl
Each entry in the array is a separate invoice item object. If no more invoice items are available, the resulting array will be...
Read more >
Stripe invoice items not always being sent together in one ...
I think the expected behaviour is that invoice items are added to the customer's upcoming invoice not the current.
Read more >
Models - Dj-Stripe
When left blank, the invoice item will be added to the next upcoming ... are still accessible by the Stripe API and sync...
Read more >
Invoice Item don't appear always - WordPress.org
Hi, I am trying to write an invoice using the existing invoice items. But the invoice items don't appear every time. Sometimes i...
Read more >
Type-Directed Program Synthesis for RESTful APIs - arXiv
(e.g. programs that always return an empty array), re- ... second program always fails at run time, because a successful.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found