stripe_tester 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/.gitignore +19 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +122 -0
  5. data/Rakefile +7 -0
  6. data/lib/stripe_tester.rb +97 -0
  7. data/lib/stripe_tester/version.rb +3 -0
  8. data/spec/spec_helper.rb +1 -0
  9. data/spec/stripe_tester_spec.rb +101 -0
  10. data/stripe_tester.gemspec +25 -0
  11. data/stripe_webhooks/.DS_Store +0 -0
  12. data/stripe_webhooks/2013-02-13/charge_failed.yml +44 -0
  13. data/stripe_webhooks/2013-02-13/charge_refunded.yml +44 -0
  14. data/stripe_webhooks/2013-02-13/charge_succeeded.yml +43 -0
  15. data/stripe_webhooks/2013-02-13/customer_created.yml +19 -0
  16. data/stripe_webhooks/2013-02-13/customer_deleted.yml +19 -0
  17. data/stripe_webhooks/2013-02-13/customer_subscription_created.yml +30 -0
  18. data/stripe_webhooks/2013-02-13/customer_subscription_deleted.yml +30 -0
  19. data/stripe_webhooks/2013-02-13/customer_subscription_trial_will_end.yaml +30 -0
  20. data/stripe_webhooks/2013-02-13/customer_subscription_updated.yml +41 -0
  21. data/stripe_webhooks/2013-02-13/invoice_created.yml +55 -0
  22. data/stripe_webhooks/2013-02-13/invoice_payment_failed.yml +55 -0
  23. data/stripe_webhooks/2013-02-13/invoice_payment_succeeded.yml +55 -0
  24. data/stripe_webhooks/2013-02-13/invoice_updated.yml +57 -0
  25. data/stripe_webhooks/2013-07-05/charge_failed.yaml +45 -0
  26. data/stripe_webhooks/2013-07-05/charge_refunded.yaml +45 -0
  27. data/stripe_webhooks/2013-07-05/charge_succeeded.yaml +45 -0
  28. data/stripe_webhooks/2013-07-05/customer_created.yaml +24 -0
  29. data/stripe_webhooks/2013-07-05/customer_deleted.yaml +24 -0
  30. data/stripe_webhooks/2013-07-05/customer_subscription_created.yaml +30 -0
  31. data/stripe_webhooks/2013-07-05/customer_subscription_deleted.yaml +30 -0
  32. data/stripe_webhooks/2013-07-05/customer_subscription_trial_will_end.yaml +30 -0
  33. data/stripe_webhooks/2013-07-05/customer_subscription_updated.yaml +41 -0
  34. data/stripe_webhooks/2013-07-05/invoice_created.yaml +55 -0
  35. data/stripe_webhooks/2013-07-05/invoice_payment_failed.yaml +55 -0
  36. data/stripe_webhooks/2013-07-05/invoice_payment_succeeded.yaml +55 -0
  37. data/stripe_webhooks/2013-07-05/invoice_updated.yaml +57 -0
  38. metadata +136 -0
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-gemset
19
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stripe_tester.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ButterCloud LLC.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # StripeTester
2
+
3
+ StripeTester is a testing gem used to simulate Stripe webhooks and post them to a specified URL.
4
+
5
+ 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.
6
+
7
+ ## Installation
8
+ ---------------
9
+
10
+ Add this line to your application's Gemfile:
11
+ ```ruby
12
+ gem 'stripe_tester'
13
+ ```
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+ Or install it yourself as:
19
+ ```bash
20
+ $ gem install stripe_tester
21
+ ```
22
+ Ruby Version `>= 1.9.3`
23
+
24
+ ## Usage
25
+ --------
26
+ In your test:
27
+
28
+ 1. Set the URL where the webhooks are handled:
29
+ ```ruby
30
+ StripeTester.webhook_url = "http://www.example.com/my_post_url"
31
+ ```
32
+
33
+ 2. Send the webhook. This will send a POST request to the URL with the event data as JSON:
34
+
35
+ The default Stripe webhook version will be the latest supported version
36
+ ```ruby
37
+ # as a symbol
38
+ StripeTester.create_event(:invoice_created)
39
+
40
+ # or as a string
41
+ StripeTester.create_event("invoice_created")
42
+ ```
43
+
44
+ Or if you want to overwrite certain attributes:
45
+ ```ruby
46
+ StripeTester.create_event(:invoice_created, {"amount" => 100, "currency" => 'gbp'})
47
+ ```
48
+
49
+ If you want to specify which stripe webhook API version you would like to use:
50
+ ```ruby
51
+ StripeTester.create_event(:invoice_created, "2013-05-07")
52
+ ```
53
+
54
+ ## Supported Webhooks
55
+ ---------------------
56
+
57
+ * charge_failed
58
+ * charge_refunded
59
+ * charge_succeeded
60
+ * customer_created
61
+ * customer_deleted
62
+ * customer_subscription_created
63
+ * customer_subscription_deleted
64
+ * customer_subscription_updated
65
+ * customer_subscription_trial_will_end
66
+ * invoice_created
67
+ * invoice_payment_failed
68
+ * invoice_payment_succeeded
69
+ * invoice_updated
70
+
71
+ ## Supported Stripe Webhook API Versions
72
+ ----------------------------------------
73
+
74
+ * 2013-07-05
75
+ * 2013-02-13
76
+
77
+ ## Issues
78
+ ---------
79
+
80
+ * Overwriting attributes only overwrites the first occurrence of the key. It needs to overwrite all of the occurrences or make the user specify a certain one.
81
+
82
+
83
+ ## Contributing
84
+ ---------------
85
+
86
+ * Fork it
87
+ * Create your feature branch
88
+ * Add your changes, and add a test for the changes.
89
+ * Run tests using
90
+
91
+ ```bash
92
+ $ rspec spec
93
+ ```
94
+ * Make sure everything is passing
95
+ * Push to the branch
96
+ * Create a new Pull Request
97
+
98
+ ## License
99
+ ----------
100
+
101
+ Copyright (c) 2013 ButterCloud LLC.
102
+
103
+ MIT License
104
+
105
+ Permission is hereby granted, free of charge, to any person obtaining
106
+ a copy of this software and associated documentation files (the
107
+ "Software"), to deal in the Software without restriction, including
108
+ without limitation the rights to use, copy, modify, merge, publish,
109
+ distribute, sublicense, and/or sell copies of the Software, and to
110
+ permit persons to whom the Software is furnished to do so, subject to
111
+ the following conditions:
112
+
113
+ The above copyright notice and this permission notice shall be
114
+ included in all copies or substantial portions of the Software.
115
+
116
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
117
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
118
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
119
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
120
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
121
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
122
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,97 @@
1
+ require "stripe_tester/version"
2
+ require 'uri'
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ module StripeTester
7
+
8
+ LATEST_STRIPE_VERSION = "2013-07-05"
9
+
10
+ # run callback with options to customize the json
11
+ def self.create_event(callback_type, stripe_version=LATEST_STRIPE_VERSION, options={})
12
+ webhook_data = self.load_template(callback_type, stripe_version)
13
+ if webhook_data
14
+ webhook_data = overwrite_attributes(webhook_data, options) unless options.empty?
15
+ post_to_url(webhook_data)
16
+ true
17
+ end
18
+ end
19
+
20
+ # replace multiple values for multiple keys in a hash
21
+ def self.overwrite_attributes(original_data, options={})
22
+ data = original_data.clone
23
+ if options
24
+ options.each do |k,v|
25
+ replace_value(data, k, v)
26
+ end
27
+ end
28
+ data
29
+ end
30
+
31
+ # replaces a value for a single key in a hash
32
+ def self.replace_value(hash, key, new_value)
33
+ if hash.key?(key)
34
+ hash[key] = new_value
35
+ else
36
+ hash.values.each do |value|
37
+ value = value.first if value.is_a?(Array)
38
+ replace_value(value, key, new_value) if value.is_a?(Hash)
39
+ end
40
+ hash
41
+ end
42
+ end
43
+
44
+ def self.post_to_url(data={})
45
+ post_url = webhook_url
46
+
47
+ if post_url
48
+ # set up request
49
+ req = Net::HTTP::Post.new(post_url.path)
50
+ req.content_type = 'application/json'
51
+ req.body = data.to_json
52
+
53
+ # send request
54
+ res = Net::HTTP.start(post_url.hostname, post_url.port) do |http|
55
+ http.request(req)
56
+ end
57
+
58
+ case res
59
+ when Net::HTTPSuccess, Net::HTTPRedirection
60
+ # good
61
+ else
62
+ res.value
63
+ end
64
+ else
65
+ raise "Could not post to URL. Please set URL."
66
+ end
67
+ end
68
+
69
+ # load yaml with specified callback type
70
+ def self.load_template(callback_type, version)
71
+ spec = Gem::Specification.find_by_name("stripe_tester")
72
+ gem_root = spec.gem_dir
73
+
74
+ path = gem_root + "/stripe_webhooks/#{version}/#{callback_type}.yml"
75
+ if File.exists?(path)
76
+ template = Psych.load_file(path)
77
+ else
78
+ raise "Webhook not found. Please use a correct webhook type or correct stripe version"
79
+ end
80
+ end
81
+
82
+ # save the url and a URI object
83
+ def self.webhook_url=(url)
84
+ if url =~ /^#{URI::regexp}$/
85
+ temp_url = URI.parse(url)
86
+ @url = temp_url
87
+ end
88
+ end
89
+
90
+ def self.webhook_url
91
+ @url
92
+ end
93
+
94
+ def self.remove_url
95
+ @url = nil
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module StripeTester
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ require 'stripe_tester'
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe StripeTester do
4
+
5
+ describe "instance methods" do
6
+
7
+ before(:each) do
8
+ @version = "2013-02-13"
9
+ end
10
+
11
+ it "#load_template should return hash" do
12
+ result = StripeTester.load_template(:invoice_created, @version)
13
+
14
+ expect(result).to be_a_kind_of(Hash)
15
+ end
16
+
17
+ it "#load_template should return correct callback type" do
18
+ type = "invoice_created"
19
+
20
+ returned_hash = StripeTester.load_template(type, @version)
21
+ returned_type = returned_hash["type"]
22
+
23
+ returned_type.sub!('.', '_')
24
+
25
+ expect(returned_type).to eq(type)
26
+ end
27
+
28
+ it "#load_template should raise an exception when invalid event is given" do
29
+ type = "incorrect_type"
30
+
31
+ expect { StripeTester.load_template(type, @version) }.to raise_error
32
+ end
33
+
34
+ it "#webhook_url should set the default url for the class" do
35
+ url = 'http://www.google.com'
36
+ StripeTester.remove_url
37
+ StripeTester.webhook_url = url
38
+
39
+ result_url = StripeTester.webhook_url
40
+ expect(result_url.to_s).to eq(url)
41
+ end
42
+
43
+ it "#webhook_url should store url as and URI object" do
44
+ url = 'http://www.hello.com'
45
+ StripeTester.remove_url
46
+ StripeTester.webhook_url = url
47
+
48
+ result_url = StripeTester.webhook_url
49
+ expect(result_url).to be_a_kind_of(URI)
50
+ end
51
+
52
+ it "#webhook_url should not store URL when URL is invalid" do
53
+ StripeTester.remove_url
54
+ StripeTester.webhook_url = 'datatatat'
55
+ expect(StripeTester.webhook_url).to eq(nil)
56
+ end
57
+
58
+ it "#post_to_url should send data to url" do
59
+
60
+ end
61
+
62
+ it "#post_to_url should raise an error if webhook URL is not set" do
63
+ StripeTester.remove_url
64
+ expect { StripeTester.post_to_url() }.to raise_error
65
+ end
66
+
67
+ it "#overwrite_attributes should overwrite attributes in default data to custom data" do
68
+ original_data = {name: 'john smith', info: {age: 45, gender: 'male'}}
69
+ overwrite_data = {name: 'smith john', age: 99}
70
+
71
+ new_data = StripeTester.overwrite_attributes(original_data, overwrite_data)
72
+
73
+ expect(new_data[:name]).to eq(overwrite_data[:name])
74
+ expect(new_data[:info][:age]).to eq(overwrite_data[:age])
75
+ end
76
+
77
+ it "#overwrite_attributes should return an unmodified hash when options don't exist" do
78
+ original_data = {name: 'john smith', info: {age: 45, gender: 'male'}}
79
+
80
+ new_data = StripeTester.overwrite_attributes(original_data)
81
+
82
+ expect(new_data).to eq(original_data)
83
+ end
84
+
85
+ it "#replace_value should replace a value of a given key in the hash" do
86
+ original_data = {name: 'john smith', info: {age: 45, gender: 'male'}}
87
+
88
+ new_data = StripeTester.replace_value(original_data, :age, 99)
89
+
90
+ expect(new_data[:info][:age]).to eq(99)
91
+ end
92
+
93
+ it "#create_event should send the event to the URL with the modified hash if options exist" do
94
+
95
+ end
96
+
97
+ it "#create_event should send the event with an unmodified hash to the URL if options don't exist" do
98
+
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stripe_tester/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stripe_tester"
8
+ spec.version = StripeTester::VERSION
9
+ spec.authors = ["Buttercloud"]
10
+ spec.email = ["info@buttercloud.com"]
11
+ spec.description = %q{Test Stripe webhooks locally}
12
+ spec.summary = %q{Test Stripe webhooks locally}
13
+ spec.homepage = "https://github.com/buttercloud/stripe_tester"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
Binary file
@@ -0,0 +1,44 @@
1
+ ---
2
+ created: 1326853478
3
+ data:
4
+ object:
5
+ amount: 100
6
+ amount_refunded: 0
7
+ captured: true
8
+ card:
9
+ address_city:
10
+ address_country:
11
+ address_line1:
12
+ address_line1_check:
13
+ address_line2:
14
+ address_state:
15
+ address_zip:
16
+ address_zip_check:
17
+ country: US
18
+ cvc_check:
19
+ exp_month: 8
20
+ exp_year: 2014
21
+ fingerprint: TTKQdP1Omgi1czlY
22
+ last4: '4242'
23
+ name:
24
+ object: card
25
+ type: Visa
26
+ created: 1372666708
27
+ currency: usd
28
+ customer:
29
+ description: My First Test Charge (created for API docs)
30
+ dispute:
31
+ failure_code:
32
+ failure_message:
33
+ fee: 0
34
+ fee_details: []
35
+ id: ch_00000000000000
36
+ invoice:
37
+ livemode: false
38
+ object: charge
39
+ paid: false
40
+ refunded: false
41
+ id: evt_00000000000000
42
+ livemode: false
43
+ object: event
44
+ type: charge.failed