Manually syncing data with Stripe

If you’re using dj-stripe’s webhook handlers then data will be automatically synced from Stripe to the Django database, but in some circumstances you may want to manually sync Stripe API data as well.

For example if you write to the Stripe API and want to work with the resulting dj-stripe object without having to wait for the webhook trigger.

This can be done using the classmethod sync_from_stripe_data that exists on all dj-stripe model classes.

E.g. creating a product using the Stripe API, and then syncing the API return data to Django using dj-stripe:

import djstripe.models
import djstripe.settings
import stripe

# stripe API return value is a dict-like object
stripe_data = stripe.Product.create(
	api_key=djstripe.settings.STRIPE_SECRET_KEY,
	name="Monthly membership base fee",
	type="service",
)

# sync_from_stripe_data to save it to the database,
# and recursively update any referenced objects
djstripe_obj = djstripe.models.Product.sync_from_stripe_data(stripe_data)

return djstripe_obj