stripe_event 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/integrallis/stripe_event.png?branch=master)](http://travis-ci.org/integrallis/stripe_event)
4
4
 
5
- stripe_event is built on the ActiveSupport::Notifications API[[0]](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html). Incoming webhook requests are authenticated by retrieving the [event object](https://stripe.com/docs/api?lang=ruby#event_object) from Stripe[[1]](https://answers.stripe.com/questions/what-is-the-recommended-way-to-authenticate-a-webhook-callback). Define subscriber blocks to handle one, many, or all event types.
5
+ stripe_event is built on the [ActiveSupport::Notifications API](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html). Incoming webhook requests are authenticated by retrieving the [event object](https://stripe.com/docs/api?lang=ruby#event_object) from Stripe[[1]](https://answers.stripe.com/questions/what-is-the-recommended-way-to-authenticate-a-webhook-callback). Define subscriber blocks to handle one, many, or all event types.
6
6
 
7
7
  ## Install
8
8
 
@@ -24,7 +24,10 @@ Stripe.api_key = ENV['STRIPE_API_KEY'] # Set your api key
24
24
 
25
25
  StripeEvent.setup do
26
26
  subscribe 'charge.failed' do |event|
27
- MyClass.handle_failed_charge(event) # Define subscriber behavior
27
+ # Define subscriber behavior based on the event object
28
+ event.class #=> Stripe::Event
29
+ event.type #=> "charge.failed"
30
+ event.data #=> { ... }
28
31
  end
29
32
 
30
33
  subscribe 'customer.created', 'customer.updated' do |event|
@@ -39,7 +42,7 @@ end
39
42
 
40
43
  ## Configuration
41
44
 
42
- If you have built an application that has multiple Stripe accounts--say, each of your customers has their own--you may want to define your own way of retrieving events from Stripe (e.g. perhaps you want to use the `user_id` parameter[[2]](https://stripe.com/docs/apps/getting-started#webhooks) from the top level to detect the customer for the event, then grab their specific API key). You can do this:
45
+ If you have built an application that has multiple Stripe accounts--say, each of your customers has their own--you may want to define your own way of retrieving events from Stripe (e.g. perhaps you want to use the [user_id parameter](https://stripe.com/docs/apps/getting-started#webhooks) from the top level to detect the customer for the event, then grab their specific API key). You can do this:
43
46
 
44
47
  ```ruby
45
48
  StripeEvent.event_retriever = Proc.new do |params|
@@ -54,6 +57,14 @@ During development it may be useful to skip retrieving the event from Stripe, an
54
57
  StripeEvent.event_retriever = Proc.new { |params| params }
55
58
  ```
56
59
 
57
- ## Register webhook url with Stripe
60
+ ### Register webhook url with Stripe
58
61
 
59
62
  ![Setup webhook url](https://raw.github.com/integrallis/stripe_event/master/screenshots/dashboard-webhook.png "webhook setup")
63
+
64
+ ### Examples
65
+
66
+ The [RailsApps](https://github.com/RailsApps) project by Daniel Kehoe has released an [example Rails 3.2 app](https://github.com/RailsApps/rails-stripe-membership-saas) with recurring billing using Stripe. The application uses stripe_event to handle `customer.subscription.deleted` events.
67
+
68
+ ### Note: 'Test Webhooks' Button on Stripe Dashboard
69
+
70
+ This button sends an example event to your webhook urls, including an `id` of `evt_00000000000000`. To confirm that Stripe sent the webhook, stripe_event attempts to retrieve the event details from Stripe using the given `id`. In this case the event does not exist and stripe_event responds with `401 Unauthorized`. Instead of using the 'Test Webhooks' button, trigger webhooks by using the Stripe Dashboard to create test payments, customers, etc.
data/Rakefile CHANGED
@@ -1,27 +1,4 @@
1
- #!/usr/bin/env rake
2
- begin
3
- require 'bundler/setup'
4
- rescue LoadError
5
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
- end
7
- # begin
8
- # require 'rdoc/task'
9
- # rescue LoadError
10
- # require 'rdoc/rdoc'
11
- # require 'rake/rdoctask'
12
- # RDoc::Task = Rake::RDocTask
13
- # end
14
- #
15
- # RDoc::Task.new(:rdoc) do |rdoc|
16
- # rdoc.rdoc_dir = 'rdoc'
17
- # rdoc.title = 'StripeEvent'
18
- # rdoc.options << '--line-numbers'
19
- # rdoc.rdoc_files.include('lib/**/*.rb')
20
- # end
21
-
22
- Bundler::GemHelper.install_tasks
23
-
24
- require 'rake'
25
1
  require 'rspec/core/rake_task'
26
- RSpec::Core::RakeTask.new(:spec)
27
- task :default => :spec
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task :default => :spec
data/lib/stripe_event.rb CHANGED
@@ -1,23 +1,62 @@
1
+ require "set"
1
2
  require "stripe"
2
3
  require "stripe_event/engine"
3
4
  require "stripe_event/subscriber"
4
5
  require "stripe_event/publisher"
5
- require "stripe_event/types"
6
- require "stripe_event/errors"
7
6
 
8
7
  module StripeEvent
9
8
  mattr_accessor :event_retriever
10
9
  self.event_retriever = Proc.new { |params| Stripe::Event.retrieve(params[:id]) }
11
10
 
12
- class << self
13
- alias_method :setup, :instance_eval
11
+ def self.setup(&block)
12
+ instance_eval(&block)
14
13
  end
15
-
14
+
16
15
  def self.publish(event)
17
16
  Publisher.new(event).instrument
18
17
  end
19
-
18
+
20
19
  def self.subscribe(*names, &block)
21
20
  Subscriber.new(*names).register(&block)
22
21
  end
22
+
23
+ class StripeEventError < StandardError; end
24
+ class InvalidEventTypeError < StripeEventError; end
25
+
26
+ TYPE_LIST = Set[
27
+ 'account.updated',
28
+ 'account.application.deauthorized',
29
+ 'charge.succeeded',
30
+ 'charge.failed',
31
+ 'charge.refunded',
32
+ 'charge.dispute.created',
33
+ 'charge.dispute.updated',
34
+ 'charge.dispute.closed',
35
+ 'customer.created',
36
+ 'customer.updated',
37
+ 'customer.deleted',
38
+ 'customer.subscription.created',
39
+ 'customer.subscription.updated',
40
+ 'customer.subscription.deleted',
41
+ 'customer.subscription.trial_will_end',
42
+ 'customer.discount.created',
43
+ 'customer.discount.updated',
44
+ 'customer.discount.deleted',
45
+ 'invoice.created',
46
+ 'invoice.updated',
47
+ 'invoice.payment_succeeded',
48
+ 'invoice.payment_failed',
49
+ 'invoiceitem.created',
50
+ 'invoiceitem.updated',
51
+ 'invoiceitem.deleted',
52
+ 'plan.created',
53
+ 'plan.updated',
54
+ 'plan.deleted',
55
+ 'coupon.created',
56
+ 'coupon.deleted',
57
+ 'transfer.created',
58
+ 'transfer.updated',
59
+ 'transfer.failed',
60
+ 'ping'
61
+ ].freeze
23
62
  end
@@ -1,13 +1,12 @@
1
1
  module StripeEvent
2
2
  class Publisher < Struct.new(:event)
3
3
  def instrument
4
- ActiveSupport::Notifications.instrument(type, :event => event)
4
+ ActiveSupport::Notifications.instrument(type, event)
5
5
  end
6
-
6
+
7
7
  def type
8
- event[:type].tap { |type|
9
- raise InvalidEventTypeError.new("Event type was not present for: #{event}") if !type.present?
10
- }
8
+ return event[:type] if event[:type].present?
9
+ raise InvalidEventTypeError.new("Event type was not present for: #{event}")
11
10
  end
12
11
  end
13
12
  end
@@ -2,22 +2,23 @@ module StripeEvent
2
2
  class Subscriber
3
3
  def initialize(*names)
4
4
  @names = names
5
- ensure_valid_types!
5
+ assert_valid_types!
6
6
  end
7
-
7
+
8
8
  def register(&block)
9
- ActiveSupport::Notifications.subscribe(pattern) do |*_, payload|
10
- block.call(payload[:event])
9
+ ActiveSupport::Notifications.subscribe(pattern) do |*args|
10
+ event = ActiveSupport::Notifications::Event.new(*args)
11
+ block.call(event.payload)
11
12
  end
12
13
  end
13
-
14
+
14
15
  def pattern
15
- Regexp.union(@names.empty? ? TYPE_LIST : @names)
16
+ Regexp.union(@names.empty? ? TYPE_LIST.to_a : @names)
16
17
  end
17
-
18
+
18
19
  private
19
-
20
- def ensure_valid_types!
20
+
21
+ def assert_valid_types!
21
22
  invalid_names = @names.select { |name| !TYPE_LIST.include?(name) }
22
23
  raise InvalidEventTypeError.new(invalid_names) if invalid_names.any?
23
24
  end
@@ -1,3 +1,3 @@
1
1
  module StripeEvent
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe_event
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-25 00:00:00.000000000 Z
12
+ date: 2012-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: '2.10'
53
+ version: '2.12'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '2.10'
61
+ version: '2.12'
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: webmock
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +66,7 @@ dependencies:
66
66
  requirements:
67
67
  - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: '1.8'
69
+ version: '1.9'
70
70
  type: :development
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +74,7 @@ dependencies:
74
74
  requirements:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: '1.8'
77
+ version: '1.9'
78
78
  description: Stripe webhook integration for Rails applications.
79
79
  email:
80
80
  - dwhalen@integrallis.com
@@ -85,17 +85,16 @@ files:
85
85
  - app/controllers/stripe_event/webhook_controller.rb
86
86
  - config/routes.rb
87
87
  - lib/stripe_event/engine.rb
88
- - lib/stripe_event/errors.rb
89
88
  - lib/stripe_event/publisher.rb
90
89
  - lib/stripe_event/subscriber.rb
91
- - lib/stripe_event/types.rb
92
90
  - lib/stripe_event/version.rb
93
91
  - lib/stripe_event.rb
94
- - MIT-LICENSE
92
+ - LICENSE.md
95
93
  - Rakefile
96
94
  - README.md
97
95
  homepage: https://github.com/integrallis/stripe_event
98
- licenses: []
96
+ licenses:
97
+ - MIT
99
98
  post_install_message:
100
99
  rdoc_options: []
101
100
  require_paths:
@@ -114,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
113
  version: '0'
115
114
  requirements: []
116
115
  rubyforge_project:
117
- rubygems_version: 1.8.21
116
+ rubygems_version: 1.8.24
118
117
  signing_key:
119
118
  specification_version: 3
120
119
  summary: Stripe webhook integration for Rails applications.
@@ -1,4 +0,0 @@
1
- module StripeEvent
2
- class StripeEventError < StandardError; end
3
- class InvalidEventTypeError < StripeEventError; end
4
- end
@@ -1,37 +0,0 @@
1
- module StripeEvent
2
- TYPE_LIST = [
3
- 'account.updated',
4
- 'account.application.deauthorized',
5
- 'charge.succeeded',
6
- 'charge.failed',
7
- 'charge.refunded',
8
- 'charge.disputed',
9
- 'customer.created',
10
- 'customer.updated',
11
- 'customer.deleted',
12
- 'customer.subscription.created',
13
- 'customer.subscription.updated',
14
- 'customer.subscription.deleted',
15
- 'customer.subscription.trial_will_end',
16
- 'customer.discount.created',
17
- 'customer.discount.updated',
18
- 'customer.discount.deleted',
19
- 'invoice.created',
20
- 'invoice.updated',
21
- 'invoice.payment_succeeded',
22
- 'invoice.payment_failed',
23
- 'invoiceitem.created',
24
- 'invoiceitem.updated',
25
- 'invoiceitem.deleted',
26
- 'plan.created',
27
- 'plan.updated',
28
- 'plan.deleted',
29
- 'coupon.created',
30
- 'coupon.updated',
31
- 'coupon.deleted',
32
- 'transfer.created',
33
- 'transfer.updated',
34
- 'transfer.failed',
35
- 'ping'
36
- ]
37
- end