Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Django Responses to Stripe Webhooks

I'm trying to figure out an effective way to test how my server handles webhooks from Stripe. I'm setting up a system to add multiple subscriptions to a customer's credit card, which is described on Stripe's website:

https://support.stripe.com/questions/can-customers-have-multiple-subscriptions

The issue I'm having is figuring out how to effectively test that my server is executing the scripts correctly (i.e., adding the correct subscriptions to the invoice, recording the events in my database, etc.). I'm not too concerned about automating the test right now, I'm just struggling to effectively run any good test on the script. Has anyone done this with Django previously? What resources and tools did you use to run these tests?

Thanks!

like image 490
bgmaster Avatar asked Mar 15 '13 18:03

bgmaster


2 Answers

I did not use any tools to run the tests. Impact the stripe has a FULL API REFERENCE which display the information you have send to them and they also display the error. Stripe is very easy to setup, cheap, and have full details in documentation.

What I did is?

  1. First I create a stripe account. In that account, they will give you:

    • TEST_SECRET_KEY: use for sending payment and information in stripe (for testing)
    • TEST_PUBS_KEY: identifies your website when communicating with Stripe (for testing)
    • LIVE_SECRET_KEY: use for sending payment and information in stripe (for live)
    • LIVE_PUBS_KEY: identifies your website when communicating with Stripe (for live)
    • API_VERSION: "2012-11-07" //this is the version for testing only
  2. When you login you will see Documentation at the top. Click the documentation and they will give you step by step tutorial on how to create a form, how to create subscription, how to handle errors and many more.

  3. To check if your script is executing and connecting to stripe. Click FULL API REFERENCE then choose Python. In that page you will see the information you have send and error that you have encountered.

What I really like is, if the Stripe detect an error the system will point out that and give you a solution. The solution is in the left side and checking the information send is on the right side.

Stripe is divided into two worlds: the test mode and the live. In test mode, you can perform creating new customer, add new invoices, set up your subscription, and many more. What ever you do in test mode, is the same when your Stripe is live.

like image 74
catherine Avatar answered Oct 26 '22 03:10

catherine


I really love that stripe provides the logs for the web hooks, however, it is difficult to view the error responses from them, so I set up a script using the Requests library. First, I went to the Stripe dashboard and copied one of the requests they were sending.

Events & Webhooks --> click on one of the requests --> copy the entire request

import requests

data = """ PASTE COPIED JSON REQUEST HERE """

# insert the appropriate url/endpoint below
res = requests.post("http://localhost:8000/stripe_hook/", data=data).text
output = open("hook_result.html", "w")
output.write(res)
output.close()

Now I could open hook_result.html and see any django errors that may have come up (given DEBUG=True in django).

like image 21
Sanketh Katta Avatar answered Oct 26 '22 03:10

Sanketh Katta