webhookr-stripe 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e44a4eff452da02349caf03645e939774f9b52e2
4
+ data.tar.gz: 149bb84c8d0f32f3b56866424a1381a419f33abc
5
+ SHA512:
6
+ metadata.gz: adaaa21f83c4ebeea593dbf690924d6acdd459b9a9911d4b69bcc63913a2c0b4a7184c105ae97148bd8d22e378c0b1ebfebdb3df27c659078afac35783918064
7
+ data.tar.gz: 269e92148e68729741465c798dfc68f65dc627ed7e74840d4a9813c37e58363d489681a85a4e387b078571f09c7da40cdc5e05b21fdc1b9b6d0b0250229455eb
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ Gemfile.local
8
+ Guardfile.local
9
+ README.html
10
+ InstalledFiles
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+ - jruby-19mode # JRuby in 1.9 mode
6
+ - ruby-head
7
+ matrix:
8
+ allow_failures:
9
+ - rvm: "1.9.3"
10
+ - rvm: jruby-19mode
11
+ - rvm: ruby-head
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem "rake", ">= 10.0"
7
+ gem "minitest"
8
+ gem "minitest-reporters"
9
+ gem "em-websocket"
10
+ gem "guard"
11
+ gem "guard-minitest"
12
+ gem "guard-markdown"
13
+ gem "guard-livereload"
14
+ gem "simplecov", :require => false
15
+ gem "coveralls", :require => false
16
+ end
17
+
18
+ if File.exists?('Gemfile.local')
19
+ instance_eval File.read('Gemfile.local')
20
+ end
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+
2
+ guard 'minitest', :test_folders => 'test', :test_file_patterns => '*_tests.rb' do
3
+ watch(%r|^test/(.+)_tests\.rb|)
4
+
5
+ watch(%r|^lib/(.*)([^/]+)\.rb|) do |m|
6
+ "test/#{m[1]}#{m[2]}_tests.rb"
7
+ end
8
+
9
+ watch(%r|^test/test_helper\.rb|) do
10
+ "test"
11
+ end
12
+ end
13
+
14
+ guard 'markdown', :convert_on_start => true do
15
+ watch ('README.md') { "./README.md|./README.html" }
16
+ end
17
+
18
+ guard 'livereload' do
19
+ watch('README.html')
20
+ end
21
+
22
+ if File.exists?('Guardfile.local')
23
+ instance_eval File.read('Guardfile.local')
24
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Gerry Power
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/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) Gerry Power
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,265 @@
1
+ # Webhookr::Stripe
2
+ [![Build Status](https://travis-ci.org/gerrypower/webhookr-stripe.png?branch=master)](https://travis-ci.org/gerrypower/webhookr-stripe)
3
+ [![Dependency Status](https://gemnasium.com/gerrypower/webhookr-stripe.png)](https://gemnasium.com/gerrypower/webhookr-stripe)
4
+ [![Code Climate](https://codeclimate.com/repos/51e8b329f3ea00509b021908/badges/afc761788ad82d3a9852/gpa.png)](https://codeclimate.com/repos/51e8b329f3ea00509b021908/feed)
5
+ [![Coverage Status](https://coveralls.io/repos/gerrypower/webhookr-stripe/badge.png)](https://coveralls.io/r/gerrypower/webhookr-stripe)
6
+
7
+ This gem is a plugin for [Webhookr](https://github.com/zoocasa/webhookr) that enables
8
+ your application to accept [webhooks from Stripe](https://stripe.com/docs/webhooks).
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'webhookr-stripe'
15
+
16
+ Or install it yourself:
17
+
18
+ $ gem install webhookr-stripe
19
+
20
+ [webhookr](https://github.com/zoocasa/webhookr) is installed as a dependency of webhookr-stripe. If you have not [setup Webhookr](https://github.com/zoocasa/webhookr#usage--setup), do so now:
21
+
22
+ ```console
23
+ rails g webhookr:add_route
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ Once you have the gem installed run the generator to add the code to your initializer.
29
+ An initializer will be created if you do not have one.
30
+
31
+ ```console
32
+ rails g webhookr:stripe:init *initializer_name* -s
33
+ ```
34
+
35
+ Run the generator to create an example file to handle stripe webhooks.
36
+
37
+ ```console
38
+ rails g webhookr:stripe:example_hooks
39
+ ```
40
+
41
+ Or create a stripe handler class for any event that you want to handle. For example
42
+ to handle unsubscribes you would create a class as follows:
43
+
44
+ ```ruby
45
+ class StripeHooks
46
+ def on_charge_succeeded(incoming)
47
+ # Your custom logic goes here.
48
+ charge = incoming.payload
49
+ puts("charge.succeeded: (#{charge})")
50
+ end
51
+ end
52
+ ```
53
+
54
+ For a complete list of events, and the payload format, see below.
55
+
56
+ Edit config/initializers/*initializer_name* and change the commented line to point to
57
+ your custom Stripe event handling class. If your class was called *StripeHooks*
58
+ the configuration line would look like this:
59
+
60
+ ```ruby
61
+ Webhookr::Stripe::Adapter.config.callback = StripeHooks
62
+ ```
63
+
64
+ To see the list of Stripe URLs your application can use when you configure
65
+ stripe webhooks,
66
+ run the provided webhookr rake task:
67
+
68
+ ```console
69
+ rake webhookr:services
70
+ ```
71
+
72
+ Example output:
73
+
74
+ ```console
75
+ stripe:
76
+ GET /webhookr/events/stripe/19xl64emxvn
77
+ POST /webhookr/events/stripe/19xl64emxvn
78
+ ```
79
+
80
+ ## Stripe Events & Payload
81
+
82
+ ### Events
83
+
84
+ All webhook events are supported. For further information on these events, see the
85
+ [stripe documentation](https://stripe.com/docs/api#event_types).
86
+
87
+ <table>
88
+ <tr>
89
+ <th>Stripe Event</th>
90
+ <th>Event Handler</th>
91
+ </tr>
92
+ <tr>
93
+ <td>account.updated</td>
94
+ <td>on_account_updated(incoming)</td>
95
+ </tr>
96
+ <tr>
97
+ <td>charge.succeeded</td>
98
+ <td>on_charge_succeeded(incoming)</td>
99
+ </tr>
100
+ <tr>
101
+ <td>charge.failed</td>
102
+ <td>on_charge_failed(incoming)</td>
103
+ </tr>
104
+ <tr>
105
+ <td>charge.refunded</td>
106
+ <td>on_charge_refunded(incoming)</td>
107
+ </tr>
108
+ <tr>
109
+ <td>charge.dispute.created</td>
110
+ <td>on_charge_dispute_created(incoming)</td>
111
+ </tr>
112
+ <tr>
113
+ <td>charge.dispute.updated</td>
114
+ <td>on_charge_dispute_updated(incoming)</td>
115
+ </tr>
116
+ <tr>
117
+ <td>charge.dispute.closed</td>
118
+ <td>on_charge_dispute_closed(incoming)</td>
119
+ </tr>
120
+ <tr>
121
+ <td>coupon.created</td>
122
+ <td>on_coupon_created(incoming)</td>
123
+ </tr>
124
+ <tr>
125
+ <td>coupon.deleted</td>
126
+ <td>on_coupon_deleted(incoming)</td>
127
+ </tr>
128
+ <tr>
129
+ <td>customer.created</td>
130
+ <td>on_customer_created(incoming)</td>
131
+ </tr>
132
+ <tr>
133
+ <td>customer.updated</td>
134
+ <td>on_customer_updated(incoming)</td>
135
+ </tr>
136
+ <tr>
137
+ <td>customer.deleted</td>
138
+ <td>on_customer_deleted(incoming)</td>
139
+ </tr>
140
+ <tr>
141
+ <td>customer.discount.created</td>
142
+ <td>on_customer_discount_created(incoming)</td>
143
+ </tr>
144
+ <tr>
145
+ <td>customer.discount.updated</td>
146
+ <td>on_customer_discount_updated(incoming)</td>
147
+ </tr>
148
+ <tr>
149
+ <td>customer.discount.deleted</td>
150
+ <td>on_customer_discount_deleted(incoming)</td>
151
+ </tr>
152
+ <tr>
153
+ <td>customer.subscription.created</td>
154
+ <td>on_customer_subscription_created(incoming)</td>
155
+ </tr>
156
+ <tr>
157
+ <td>customer.subscription.updated</td>
158
+ <td>on_customer_subscription_updated(incoming)</td>
159
+ </tr>
160
+ <tr>
161
+ <td>customer.subscription.deleted</td>
162
+ <td>on_customer_subscription_deleted(incoming)</td>
163
+ </tr>
164
+ <tr>
165
+ <td>invoice.created</td>
166
+ <td>on_invoice_created(incoming)</td>
167
+ </tr>
168
+ <tr>
169
+ <td>invoice.updated</td>
170
+ <td>on_invoice_updated(incoming)</td>
171
+ </tr>
172
+ <tr>
173
+ <td>invoice.payment_succeeded</td>
174
+ <td>on_invoice_payment_succeeded(incoming)</td>
175
+ </tr>
176
+ <tr>
177
+ <td>invoice.payment_failed</td>
178
+ <td>on_invoice_payment_failed(incoming)</td>
179
+ </tr>
180
+ <tr>
181
+ <td>invoiceitem.created</td>
182
+ <td>on_invoiceitem_created(incoming)</td>
183
+ </tr>
184
+ <tr>
185
+ <td>invoiceitem.updated</td>
186
+ <td>on_invoiceitem_updated(incoming)</td>
187
+ </tr>
188
+ <tr>
189
+ <td>invoiceitem.deleted</td>
190
+ <td>on_invoiceitem_deleted(incoming)</td>
191
+ </tr>
192
+ <tr>
193
+ <td>plan.created</td>
194
+ <td>on_plan_created(incoming)</td>
195
+ </tr>
196
+ <tr>
197
+ <td>plan.updated</td>
198
+ <td>on_plan_updated(incoming)</td>
199
+ </tr>
200
+ <tr>
201
+ <td>plan.deleted</td>
202
+ <td>on_plan_deleted(incoming)</td>
203
+ </tr>
204
+ <tr>
205
+ <td>transfer.created</td>
206
+ <td>on_transfer_created(incoming)</td>
207
+ </tr>
208
+ <tr>
209
+ <td>transfer.updated</td>
210
+ <td>on_transfer_updated(incoming)</td>
211
+ </tr>
212
+ <tr>
213
+ <td>transfer.paid</td>
214
+ <td>on_transfer_paid(incoming)</td>
215
+ </tr>
216
+ <tr>
217
+ <td>transfer.failed</td>
218
+ <td>on_transfer_failed(incoming)</td>
219
+ </tr>
220
+ </table>
221
+
222
+ ### Payload
223
+
224
+ The payload is the full payload data from as per the
225
+ [stripe documentation](https://stripe.com/docs/api#event_types), converted to an OpenStruct
226
+ for ease of access. Examples can be found in the example hook file.
227
+
228
+ ### <a name="supported_services"></a>Supported Service - stripe
229
+
230
+ * [https://stripe.com/docs/webhooks](stripe - version: 2013-07-05)
231
+
232
+ ## <a name="works_with"></a>Works with:
233
+
234
+ webhookr-stripe works with Rails 3.1, 3.2 and 4.0, and has been tested on the following Ruby
235
+ implementations:
236
+
237
+ * 2.0.0
238
+
239
+ Pending:
240
+
241
+ * 1.9.3
242
+ * jruby-19mode
243
+
244
+ ### Versioning
245
+ This library aims to adhere to [Semantic Versioning 2.0.0](http://semver.org/). Violations of this scheme should be reported as
246
+ bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, that
247
+ version should be immediately yanked and/or a new version should be immediately released that restores
248
+ compatibility. Breaking changes to the public API will only be introduced with new major versions. As a
249
+ result of this policy, once this gem reaches a 1.0 release, you can (and should) specify a dependency on
250
+ this gem using the [Pessimistic Version Constraint](http://docs.rubygems.org/read/chapter/16#page74) with
251
+ two digits of precision. For example:
252
+
253
+ spec.add_dependency 'webhookr-stripe', '~> 1.0'
254
+
255
+ While this gem is currently a 0.x release, suggestion is to require the exact version that works for your code:
256
+
257
+ spec.add_dependency 'webhookr-stripe', '0.0.1'
258
+
259
+ ## License
260
+
261
+ webhookr-stripe is released under the [MIT license](http://www.opensource.org/licenses/MIT).
262
+
263
+ ## Author
264
+
265
+ * [Gerry Power](https://github.com/gerrypower)
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+
2
+ # -*- ruby -*-
3
+
4
+ require 'rubygems'
5
+ require 'rubygems/package_task'
6
+ require 'rake/testtask'
7
+ require 'rdoc/task'
8
+ require 'bundler/gem_tasks'
9
+
10
+ if RUBY_VERSION >= '1.9'
11
+ begin
12
+ gem 'psych'
13
+ rescue Exception => e
14
+ # it's okay, fall back on the bundled psych
15
+ end
16
+ end
17
+
18
+ $:.push 'lib'
19
+
20
+ version = Webhookr::Stripe::VERSION
21
+
22
+ desc 'Test Webhookr Stripe'
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.test_files = FileList['test/**/*_tests.rb']
25
+ t.verbose = !!ENV['VERBOSE_TESTS']
26
+ t.warning = !!ENV['WARNINGS']
27
+ end
28
+
29
+ desc 'Build docs'
30
+ Rake::RDocTask.new do |t|
31
+ t.main = 'README.md'
32
+ t.title = "Webhookr Stripe #{version}"
33
+ t.rdoc_dir = 'doc'
34
+ t.rdoc_files.include('README.md', 'MIT-LICENSE', 'lib/**/*.rb')
35
+ end
36
+
37
+ task :default => :test
@@ -0,0 +1,17 @@
1
+ module Webhookr
2
+ module Stripe
3
+ module Generators
4
+
5
+ class ExampleHooksGenerator < Rails::Generators::Base
6
+ EXAMPLE_HOOK_FILE = 'app/models/stripe_hooks.rb'
7
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
8
+
9
+ desc "Creates an example Stripe hook file: '#{EXAMPLE_HOOK_FILE}'"
10
+ def example_hooks
11
+ copy_file( "stripe_hooks.rb", EXAMPLE_HOOK_FILE)
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ require 'generators/webhookr/init_generator'
2
+
3
+ module Webhookr
4
+ module Stripe
5
+ module Generators
6
+ class InitGenerator < Webhookr::Generators::InitGenerator
7
+
8
+ desc "This generator updates the named initializer with Stripe options"
9
+ def init
10
+ super
11
+ append_to_file "config/initializers/#{file_name}.rb" do
12
+ plugin_initializer_text
13
+ end
14
+ end
15
+
16
+ def plugin_initializer_text
17
+ "\nWebhookr::Stripe::Adapter.config.security_token = '#{generate_security_token}'" +
18
+ "\n# Uncomment the next line to include your custom Stripe handler\n" +
19
+ "# <-- Webhookr::Stripe::Adapter.config.callback = your_custom_class --> \n"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,169 @@
1
+ class StripeHooks
2
+
3
+ # All 'on_' handlers are optional. Omit any you do not require.
4
+ # Details on the payload structure: http://help.stripe.com/entries/24466132-Webhook-Format
5
+ #
6
+ # Note: You are responsible for checking 'livemode' on incoming payloads.
7
+ # See https://stripe.com/docs/webhooks#receiving-application-webhooks
8
+ #
9
+
10
+ def on_account_updated(incoming)
11
+ account = incoming.payload
12
+ puts("account.updated: (#{account})")
13
+ end
14
+
15
+ def on_charge_succeeded(incoming)
16
+ charge = incoming.payload
17
+ puts("charge.succeeded: (#{charge})")
18
+ end
19
+
20
+ def on_charge_failed(incoming)
21
+ charge = incoming.payload
22
+ puts("charge.failed: (#{charge})")
23
+ end
24
+
25
+ def on_charge_refunded(incoming)
26
+ charge = incoming.payload
27
+ puts("charge.refunded: (#{charge})")
28
+ end
29
+
30
+ def on_charge_dispute_created(incoming)
31
+ dispute = incoming.payload
32
+ puts("charge.dispute.created: (#{dispute})")
33
+ end
34
+
35
+ def on_charge_dispute_updated(incoming)
36
+ dispute = incoming.payload
37
+ puts("charge.dispute.updated: (#{dispute})")
38
+ end
39
+
40
+ def on_charge_dispute_closed(incoming)
41
+ dispute = incoming.payload
42
+ puts("charge.dispute.closed: (#{dispute})")
43
+ end
44
+
45
+ def on_coupon_created(incoming)
46
+ coupon = incoming.payload
47
+ puts("coupon.created: (#{coupon})")
48
+ end
49
+
50
+ def on_coupon_deleted(incoming)
51
+ coupon = incoming.payload
52
+ puts("coupon.deleted: (#{coupon})")
53
+ end
54
+
55
+ def on_customer_created(incoming)
56
+ customer = incoming.payload
57
+ puts("customer.created: (#{customer})")
58
+ end
59
+
60
+ def on_customer_updated(incoming)
61
+ customer = incoming.payload
62
+ puts("customer.updated: (#{customer})")
63
+ end
64
+
65
+ def on_customer_deleted(incoming)
66
+ customer = incoming.payload
67
+ puts("customer.deleted: (#{customer})")
68
+ end
69
+
70
+ def on_customer_discount_created(incoming)
71
+ discount = incoming.payload
72
+ puts("customer.discount.created: (#{discount})")
73
+ end
74
+
75
+ def on_customer_discount_updated(incoming)
76
+ discount = incoming.payload
77
+ puts("customer.discount.updated: (#{discount})")
78
+ end
79
+
80
+ def on_customer_discount_deleted(incoming)
81
+ discount = incoming.payload
82
+ puts("customer.discount.deleted: (#{discount})")
83
+ end
84
+
85
+ def on_customer_subscription_created(incoming)
86
+ subscription = incoming.payload
87
+ puts("customer.subscription.created: (#{subscription})")
88
+ end
89
+
90
+ def on_customer_subscription_updated(incoming)
91
+ subscription = incoming.payload
92
+ puts("customer.subscription.updated: (#{subscription})")
93
+ end
94
+
95
+ def on_customer_subscription_deleted(incoming)
96
+ subscription = incoming.payload
97
+ puts("customer.subscription.deleted: (#{subscription})")
98
+ end
99
+
100
+ def on_invoice_created(incoming)
101
+ invoice = incoming.payload
102
+ puts("invoice.created: (#{invoice})")
103
+ end
104
+
105
+ def on_invoice_updated(incoming)
106
+ invoice = incoming.payload
107
+ puts("invoice.updated: (#{invoice})")
108
+ end
109
+
110
+ def on_invoice_payment_succeeded(incoming)
111
+ invoice = incoming.payload
112
+ puts("invoice.payment_succeeded: (#{invoice})")
113
+ end
114
+
115
+ def on_invoice_payment_failed(incoming)
116
+ invoice = incoming.payload
117
+ puts("invoice.payment_failed: (#{invoice})")
118
+ end
119
+
120
+ def on_invoiceitem_created(incoming)
121
+ invoiceitem = incoming.payload
122
+ puts("invoiceitem.created: (#{invoiceitem})")
123
+ end
124
+
125
+ def on_invoiceitem_updated(incoming)
126
+ invoiceitem = incoming.payload
127
+ puts("invoiceitem.updated: (#{invoiceitem})")
128
+ end
129
+
130
+ def on_invoiceitem_deleted(incoming)
131
+ invoiceitem = incoming.payload
132
+ puts("invoiceitem.deleted: (#{invoiceitem})")
133
+ end
134
+
135
+ def on_plan_created(incoming)
136
+ plan = incoming.payload
137
+ puts("plan.created: (#{plan})")
138
+ end
139
+
140
+ def on_plan_updated(incoming)
141
+ plan = incoming.payload
142
+ puts("plan.updated: (#{plan})")
143
+ end
144
+
145
+ def on_plan_deleted(incoming)
146
+ plan = incoming.payload
147
+ puts("plan.deleted: (#{plan})")
148
+ end
149
+
150
+ def on_transfer_created(incoming)
151
+ transfer = incoming.payload
152
+ puts("transfer.created: (#{transfer})")
153
+ end
154
+
155
+ def on_transfer_updated(incoming)
156
+ transfer = incoming.payload
157
+ puts("transfer.updated: (#{transfer})")
158
+ end
159
+
160
+ def on_transfer_paid(incoming)
161
+ transfer = incoming.payload
162
+ puts("transfer.paid: (#{transfer})")
163
+ end
164
+
165
+ def on_transfer_failed(incoming)
166
+ transfer = incoming.payload
167
+ puts("transfer.failed: (#{transfer})")
168
+ end
169
+ end
@@ -0,0 +1,54 @@
1
+ require "webhookr"
2
+ require "webhookr-stripe/version"
3
+ require "webhookr/ostruct_utils"
4
+
5
+ module Webhookr
6
+ module Stripe
7
+ class Adapter
8
+ SERVICE_NAME = "stripe"
9
+ EVENT_KEY = "type"
10
+ PAYLOAD_KEY = "data"
11
+ OBJECT_KEY = "object"
12
+
13
+ include Webhookr::Services::Adapter::Base
14
+
15
+ def self.process(raw_response)
16
+ new.process(raw_response)
17
+ end
18
+
19
+ def process(raw_response)
20
+ Array.wrap(parse(raw_response)).collect do |p|
21
+ Webhookr::AdapterResponse.new(
22
+ SERVICE_NAME,
23
+ convert_periods_to_underscores(p.fetch(EVENT_KEY)),
24
+ OstructUtils.to_ostruct(p)[PAYLOAD_KEY][OBJECT_KEY]
25
+ ) if assert_valid_packet(p)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def convert_periods_to_underscores(event)
32
+ return if event.nil?
33
+ event.gsub(".", "_")
34
+ end
35
+
36
+ def parse(raw_response)
37
+ begin
38
+ ActiveSupport::JSON.decode(
39
+ CGI.unescape(raw_response)
40
+ )
41
+ rescue Exception => e
42
+ raise InvalidPayloadError.new(e)
43
+ end
44
+ end
45
+
46
+ def assert_valid_packet(packet)
47
+ raise(Webhookr::InvalidPayloadError, "Unknown event #{packet[EVENT_KEY]}") unless packet[EVENT_KEY]
48
+ raise(Webhookr::InvalidPayloadError, "No data in the response") unless packet[PAYLOAD_KEY] && packet[PAYLOAD_KEY][OBJECT_KEY]
49
+ true
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ module Webhookr
2
+ module Stripe
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+
2
+ $: << File.join(File.dirname(__FILE__), %w{ .. .. .. })
3
+ require 'test_helper'
4
+ require 'generators/webhookr/stripe/init_generator'
5
+
6
+ class InitGeneratorTests < Rails::Generators::TestCase
7
+ tests Webhookr::Stripe::Generators::InitGenerator
8
+ destination File.expand_path("../../../tmp", File.dirname(__FILE__))
9
+ setup :prepare_destination
10
+
11
+ def setup
12
+ @name = "test_initializer"
13
+ @initializer = "config/initializers/#{@name}.rb"
14
+ run_generator Array.wrap(@name)
15
+ end
16
+
17
+ test "it should create the initializer" do
18
+ assert_file @initializer
19
+ end
20
+
21
+ test "it should have authorization information" do
22
+ assert_file @initializer do |content|
23
+ assert_match(%r{Webhookr::Stripe::Adapter\.config\.security_token}, content)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.start
5
+ Coveralls.wear!
6
+
7
+ require 'minitest/autorun'
8
+ require 'minitest/reporters'
9
+ require 'rails'
10
+ require "rails/generators/test_case"
11
+ require File.expand_path('../../lib/webhookr-stripe.rb', __FILE__)
12
+
13
+ if RUBY_VERSION >= "1.9"
14
+ MiniTest::Reporters.use!(MiniTest::Reporters::SpecReporter.new)
15
+ end
@@ -0,0 +1,8 @@
1
+ $: << File.join(File.dirname(__FILE__), %w{ .. .. })
2
+ require 'test_helper'
3
+
4
+ describe Webhookr::Stripe do
5
+ it "must be defined" do
6
+ Webhookr::Stripe::VERSION.wont_be_nil
7
+ end
8
+ end
@@ -0,0 +1,126 @@
1
+
2
+ $: << File.join(File.dirname(__FILE__), "..")
3
+ require 'test_helper'
4
+
5
+ describe Webhookr::Stripe::Adapter do
6
+
7
+ before do
8
+ @event_type = "charge_succeeded"
9
+ @blort_value = "blort_equals_100"
10
+ end
11
+
12
+ def valid_response
13
+ '{ "created": 1326853478,
14
+ "livemode": false,
15
+ "id": "evt_00000000000000",
16
+ "type": "charge.succeeded",
17
+ "object": "event",
18
+ "data":{
19
+ "object": { "blort": "' + @blort_value + '" }
20
+ }
21
+ }'
22
+ end
23
+
24
+ def no_event_type_response
25
+ '{ "created": 1326853478,
26
+ "livemode": false,
27
+ "id": "evt_00000000000000",
28
+ "object": "event",
29
+ "data":{
30
+ "object": { "blort": "' + @blort_value + '" }
31
+ }
32
+ }'
33
+ end
34
+
35
+ def no_data_response
36
+ '{ "created": 1326853478,
37
+ "livemode": false,
38
+ "id": "evt_00000000000000",
39
+ "type": "charge.succeeded",
40
+ "object": "event"
41
+ }'
42
+ end
43
+
44
+ describe "the class" do
45
+
46
+ subject { Webhookr::Stripe::Adapter }
47
+
48
+ it "must support process" do
49
+ subject.must_respond_to(:process)
50
+ end
51
+
52
+ it "should not return an error for a valid packet" do
53
+ lambda {
54
+ subject.process(valid_response)
55
+ }.must_be_silent
56
+ end
57
+
58
+ end
59
+
60
+ describe "the instance" do
61
+
62
+ subject { Webhookr::Stripe::Adapter.new }
63
+
64
+ it "should not return an error for a valid packet" do
65
+ lambda {
66
+ subject.process(valid_response)
67
+ }.must_be_silent
68
+ end
69
+
70
+ it "should raise Webhookr::InvalidPayloadError for no packet" do
71
+ lambda {
72
+ subject.process("")
73
+ }.must_raise(Webhookr::InvalidPayloadError)
74
+ end
75
+
76
+ it "should raise Webhookr::InvalidPayloadError for a missing event type" do
77
+ lambda {
78
+ subject.process(no_event_type_response)
79
+ }.must_raise(Webhookr::InvalidPayloadError)
80
+ end
81
+
82
+ it "should raise Webhookr::InvalidPayloadError for a missing data packet" do
83
+ lambda {
84
+ subject.process(no_data_response)
85
+ }.must_raise(Webhookr::InvalidPayloadError)
86
+ end
87
+
88
+ end
89
+
90
+ describe "it's response" do
91
+ before do
92
+ @adapter = Webhookr::Stripe::Adapter.new
93
+ end
94
+
95
+ subject { @adapter.process(valid_response).first }
96
+
97
+ it "must respond to service_name" do
98
+ subject.must_respond_to(:service_name)
99
+ end
100
+
101
+ it "should return the correct service name" do
102
+ assert_equal(Webhookr::Stripe::Adapter::SERVICE_NAME, subject.service_name)
103
+ end
104
+
105
+ it "must respond to event_type" do
106
+ subject.must_respond_to(:event_type)
107
+ end
108
+
109
+ it "should return the correct event type" do
110
+ assert_equal(@event_type, subject.event_type)
111
+ end
112
+
113
+ it "must respond to payload" do
114
+ subject.must_respond_to(:payload)
115
+ end
116
+
117
+ it "must respond to payload.blort" do
118
+ subject.payload.must_respond_to(:blort)
119
+ end
120
+
121
+ it "should return the correct data packet" do
122
+ assert_equal(@blort_value, subject.payload.blort)
123
+ end
124
+ end
125
+
126
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'webhookr-stripe/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "webhookr-stripe"
8
+ gem.version = Webhookr::Stripe::VERSION
9
+ gem.authors = ["Gerry Power"]
10
+ gem.email = ["gerry@thepowerhouse.com"]
11
+ gem.description = "A webhookr extension to support Stripe webhooks."
12
+ gem.summary = gem.description
13
+ gem.homepage = "http://github.com/gerrypower/webhookr-stripe"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency("webhookr")
22
+ gem.add_dependency("activesupport", [">= 3.1"])
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webhookr-stripe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gerry Power
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: webhookr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ description: A webhookr extension to support Stripe webhooks.
42
+ email:
43
+ - gerry@thepowerhouse.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .coveralls.yml
49
+ - .gitignore
50
+ - .travis.yml
51
+ - Gemfile
52
+ - Guardfile
53
+ - LICENSE.txt
54
+ - MIT-LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - lib/generators/webhookr/stripe/example_hooks_generator.rb
58
+ - lib/generators/webhookr/stripe/init_generator.rb
59
+ - lib/generators/webhookr/stripe/templates/stripe_hooks.rb
60
+ - lib/webhookr-stripe.rb
61
+ - lib/webhookr-stripe/version.rb
62
+ - test/generators/webhookr/stripe_plugin/init_generator_tests.rb
63
+ - test/test_helper.rb
64
+ - test/unit/webhookr-stripe/version_tests.rb
65
+ - test/unit/webhookr-stripe_tests.rb
66
+ - webhookr-stripe.gemspec
67
+ homepage: http://github.com/gerrypower/webhookr-stripe
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.0.3
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: A webhookr extension to support Stripe webhooks.
91
+ test_files:
92
+ - test/generators/webhookr/stripe_plugin/init_generator_tests.rb
93
+ - test/test_helper.rb
94
+ - test/unit/webhookr-stripe/version_tests.rb
95
+ - test/unit/webhookr-stripe_tests.rb