dj-stripe / dj-stripe

dj-stripe automatically syncs your Stripe Data to your local database as pre-implemented Django Models allowing you to use the Django ORM, in your code, to work with the data making it easier and faster.
https://dj-stripe.dev
MIT License
1.56k stars 474 forks source link

StripeBaseModel.sync_from_stripe_data only persists data if its a new instance, but won't update an existing instance #2032

Open MattTheRed opened 2 months ago

MattTheRed commented 2 months ago

Describe the bug StripeBaseModel.sync_from_stripe_data seems to be to intended for synchronously updating a given DJ stripe model in the database from a Stripe API response (VS relying on the webhooks to asyncronously update the database).

To Reproduce

This looks to be working fine if you use this method for a brand new model instance (since a get_or_create_from_stripe_object is used), but does not persist changes if you are updating an existing instance.

# invoice is persisted to the database if a brand new object is created
invoice_obj = Invoice._api_create(...)
invoice = Invoice.sync_from_stripe_data(invoice_obj)

# updated to paid_invoice are NOT persisted to the database
paid_invoice_obj = invoice.api_retrieve().pay()
paid_invoice = Invoice.sync_from_stripe_data(paid_invoice_obj)

# This will persist the changes
paid_invoice.save()

I believe if you add an instance.save() call here that will fix things.

Software versions