Skip to content

buttercloud/stripe_tester

Repository files navigation

StripeTester

wercker status

StripeTester is a testing gem used to simulate Stripe webhooks and post them to a specified URL.

StripeTester allows you to submit webhooks to your application without hitting Stripe or requiring connectivity. You can use it in your test suite to simulate webhooks and ensure that your application reacts accordingly. You can also use StripeTester in the console to simulate webhooks easily.

Installation

Add this line to your application's Gemfile:

gem 'stripe_tester', "~> 0.5.0"

And then execute:

$ bundle

Or install it yourself as:

$ gem install stripe_tester

Requirements

Ruby >= 1.9.3

RSpec

Usage

In your test:

  1. Set the URL where the webhooks are handled:
# Normal HTTP URL
StripeTester.webhook_url = "http://www.example.com/my_post_url"

# HTTPS URL
StripeTester.webhook_url = "https://www.secure-example.com/my_post_url"
  1. If your URL is secured with a self-signed SSL certificate, disable SSL verification:
StripeTester.verify_ssl = false
  1. If you want to specify which Stripe webhook API version you would like to use (the default will be the latest supported version):
StripeTester.stripe_version = "2015-10-16"
  1. If you are using username and password in your Stripe webhook event, you can provide it in two ways:

This will set basic auth with the default username, stripe, and the password provided:

    StripeTester.webhook_password = "<password>"

Or you can set the username and password in the URL:

    # Normal HTTP URL
    StripeTester.webhook_url = "http://stripe:[email protected]/my_post_url"

    # HTTPS URL
    StripeTester.webhook_url = "https://stripe:[email protected]/my_post_url"
  1. Send the webhook. This will send a POST request to the URL with the event data as JSON:
# as a symbol
StripeTester.create_event(:invoice_created)

# or as a string
StripeTester.create_event("invoice_created")

Or if you want to overwrite certain attributes globally:

StripeTester.create_event(:invoice_created, {"amount" => 100, "currency" => 'gbp'})

Or you can explicitly overwrite certain attributes using deep object merging:

StripeTester.create_event(:customer_subscription_created, {"data"=>{"object"=>{"plan"=>{"id"=>"gold-v1"}}}}, :method=>:merge)

If you want to load the JSON only:

json = StripeTester.load_template(:invoice_payment_failed)

You can also overwrite certain attributes in the JSON:

json = StripeTester.load_template(:invoice_payment_failed, {"data"=>{"object"=>{"customer"=>"cus_MYCUSTOMERID"}}}, :method=>:merge)

Supported Stripe Webhook API Versions

Contributing

  • Fork it
  • Create your feature branch
  • Add your changes, and add a test for the changes.
  • Run tests using
  $ rspec spec
  • Make sure everything is passing
  • Push to the branch
  • Create a new Pull Request

License

Copyright (c) 2016 ButterCloud LLC.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.