6

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!

bgmaster
  • 2,313
  • 4
  • 28
  • 41

4 Answers4

1

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.

catherine
  • 22,492
  • 12
  • 61
  • 85
  • If you have other questions, feel free to ask. I'm currently set up stripe and dwolla in our site. – catherine Mar 15 '13 at 18:48
  • Hi Catherine, thanks for the info. The issue I'm having is that for the webhooks, there doesn't seem to be a good way to view the response from my server, or to test actual events with customers, invoices, etc in test mode. You can only click "Send Test Webhook" from the account dashboard, which sends an 'id':'evt_00000000000000' event, which doesn't allow you to actually update any accounts or perform any real functions on Stripe. Any ideas? – bgmaster Mar 15 '13 at 19:17
  • You can perform adding new customer, their invoice, your subscriptions via in your app or in Stripe. Have you read there documentation. There are samples codes on how to create and test that – catherine Mar 15 '13 at 19:23
  • Webhooks only use if you want to get notified about events that happen in your Stripe account. About this, `which sends an 'id':'evt_00000000000000'`, this will only test if your webhooks is working – catherine Mar 15 '13 at 19:26
  • If you have another questions I will answer that tomorrow, need to sleep – catherine Mar 15 '13 at 19:42
1

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).

Sanketh Katta
  • 5,961
  • 2
  • 29
  • 30
  • Sanketh,I'm unfamiliar with the requests library, but it looks like it could help me with what I need. Let me do some investigation and I'll get back to you. – bgmaster Mar 15 '13 at 20:05
  • Requests is really just a nicer version of the builtin [urllib2](http://docs.python.org/2/library/urllib2.html), makes it a whole lot easier to work with http requests. – Sanketh Katta Mar 15 '13 at 20:13
1

In django-stripe-payments I have a test suite that while far from comprehensive is meant to be a start at getting there. What I do is just copy a real webhook's data, scrub it for sensitive data and add it as a data to the test.

Patrick Altman
  • 890
  • 8
  • 10
  • The test suite link seems broken, can you confirm the correct update to the link would be https://github.com/pinax/pinax-stripe/blob/master/pinax/stripe/tests/test_webhooks.py – iLoveTux Sep 10 '19 at 17:06
0

testing stripe webhooks is a pain. I don't use Django, so my answer will be more general.

My php webhook handler parses the webhook data and dispatches handler functions accordingly. In my handler class, I set up class properties with legitimate data for all the ids that the test webhooks mangles. Then I have a condition in each of my handler functions that tests for livemode. If false, I replace the mangled ids with legit test ids.

I also have another class property called $fakeLiveMode, which I set to true when I'm testing. This allows me to force the code to process as though in live mode.

So, for example, when testing the customer.subscription.updated event, the plan id and customer id get botched. So in that handler I would do this:

if ($event->livemode === true || $this->fakeLivemode)
{
    if ($this->fakeLivemode)
    {
        // override botched data returned by test webhook
        $event->data->object->plan->id = $this->testPlanId;
        $event->data->object->customer = $this->testCustomerId;
    }

    // process webhook
}

Does that help?

Codasaurus
  • 860
  • 5
  • 11