Dominic M.
Dominic M.

Reputation: 913

Stripe Subscription Events Django

I have setup a basic subscription system using Stripe on my Django Web Application, but I'm confused on how to record the events sent from stripe in my database, would I utilize Django REST API to listen for the events and use that to run a method to correspond with the event.

Here's an example, a customer signs up for a subscription and pays in full, the subscription clears stripe and becomes active. One month later, the customers credit card is charged again, but is denied. I read here that Stripe sends two events when this occurs: charge.failed event and an invoice.payment_failed. How would I listen for these events?

Upvotes: 1

Views: 388

Answers (1)

Saroj Rai
Saroj Rai

Reputation: 1609

You don't need Rest API,

your urls.py

url(r'^stripe/webhook/', stripe_webhook, name='stripe_webhook'),

your views.py

@csrf_exempt
def stripe_webhook(request):

    try:
        event_json = json.loads(request.body)

        if event_json['type'] == 'invoice.payment_failed':
            pass
            # get data from evet_json and get your logic here.
    except Exception as e:
        return JsonResponse({'error': str(e)})

Add the above url in stripe webhooks (Stripe Dashboard->Developers->Webhooks->Add endpoint) and use ngrok to debug in localhost.

Upvotes: 1

Related Questions