Skip to content

Getting dj-stripe object from webhooks handler #2082

Answered by abe-101
philitell asked this question in Q&A
Discussion options

You must be logged in to vote

Why sync the data from the stripe api if the webhook just received the latest data

Heres my suggestion:

from django.db import transaction
from djstripe import webhooks
from djstripe.models import Subscription

def do_something(subscription_id):
    subscription = Subscription.objects.get(stripe_id=subscription_id)
    # Do something with the subscription

@webhooks.handler("checkout.session.completed")
def checkout_session_completed(event, **kwargs):
    session_data: Dict[str, Any] = event.data["object"]
    subscription_id: Optional[str] = session_data["subscription"]
    if subscription_id:
        transaction.on_commit(do_something, subscription_id)

(I did not test teh above code if i…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
5 replies
@philitell
Comment options

@abe-101
Comment options

@philitell
Comment options

@philitell
Comment options

@abe-101
Comment options

Answer selected by philitell
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants