stripe_webhooks 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +43 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/javascripts/stripe_webhooks/application.js +13 -0
  6. data/app/assets/stylesheets/stripe_webhooks/application.css +15 -0
  7. data/app/controllers/stripe_webhooks/application_controller.rb +4 -0
  8. data/app/controllers/stripe_webhooks/events_controller.rb +26 -0
  9. data/app/helpers/stripe_webhooks/application_helper.rb +4 -0
  10. data/app/helpers/stripe_webhooks/events_helper.rb +4 -0
  11. data/app/jobs/stripe_webhooks/processor_job.rb +14 -0
  12. data/app/models/stripe_webhooks/callback.rb +62 -0
  13. data/app/models/stripe_webhooks/event.rb +37 -0
  14. data/app/models/stripe_webhooks/performed_callback.rb +5 -0
  15. data/app/views/layouts/stripe_webhooks/application.html.erb +14 -0
  16. data/config/routes.rb +3 -0
  17. data/db/migrate/20150715152905_create_stripe_webhooks_events.rb +13 -0
  18. data/db/migrate/20150716195741_create_stripe_webhooks_performed_callbacks.rb +10 -0
  19. data/lib/generators/stripe_webhooks.rb +7 -0
  20. data/lib/generators/stripe_webhooks/callback_generator.rb +28 -0
  21. data/lib/generators/stripe_webhooks/templates/application_callback.rb.erb +3 -0
  22. data/lib/generators/stripe_webhooks/templates/callback.rb.erb +8 -0
  23. data/lib/stripe_webhooks.rb +6 -0
  24. data/lib/stripe_webhooks/callbacks.rb +24 -0
  25. data/lib/stripe_webhooks/engine.rb +14 -0
  26. data/lib/stripe_webhooks/version.rb +3 -0
  27. data/lib/tasks/stripe_webhooks_tasks.rake +4 -0
  28. data/spec/controllers/stripe_webhooks/events_controller_spec.rb +14 -0
  29. data/spec/dummy/README.rdoc +28 -0
  30. data/spec/dummy/Rakefile +6 -0
  31. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  32. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  33. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  34. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  35. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  36. data/spec/dummy/bin/bundle +3 -0
  37. data/spec/dummy/bin/rails +4 -0
  38. data/spec/dummy/bin/rake +4 -0
  39. data/spec/dummy/bin/setup +29 -0
  40. data/spec/dummy/config.ru +4 -0
  41. data/spec/dummy/config/application.rb +26 -0
  42. data/spec/dummy/config/boot.rb +5 -0
  43. data/spec/dummy/config/database.yml +13 -0
  44. data/spec/dummy/config/environment.rb +5 -0
  45. data/spec/dummy/config/environments/development.rb +41 -0
  46. data/spec/dummy/config/environments/production.rb +79 -0
  47. data/spec/dummy/config/environments/test.rb +42 -0
  48. data/spec/dummy/config/initializers/assets.rb +11 -0
  49. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  50. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  51. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  52. data/spec/dummy/config/initializers/inflections.rb +16 -0
  53. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  54. data/spec/dummy/config/initializers/session_store.rb +3 -0
  55. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  56. data/spec/dummy/config/locales/en.yml +23 -0
  57. data/spec/dummy/config/routes.rb +4 -0
  58. data/spec/dummy/config/secrets.yml +22 -0
  59. data/spec/dummy/public/404.html +67 -0
  60. data/spec/dummy/public/422.html +67 -0
  61. data/spec/dummy/public/500.html +66 -0
  62. data/spec/dummy/public/favicon.ico +0 -0
  63. data/spec/factories/stripe_webhooks_events.rb +10 -0
  64. data/spec/factories/stripe_webhooks_performed_callbacks.rb +7 -0
  65. data/spec/helpers/stripe_webhooks/events_helper_spec.rb +17 -0
  66. data/spec/jobs/stripe_webhooks/processor_job_spec.rb +7 -0
  67. data/spec/models/stripe_webhooks/event_spec.rb +7 -0
  68. data/spec/models/stripe_webhooks/performed_callback_spec.rb +7 -0
  69. data/spec/rails_helper.rb +65 -0
  70. data/spec/spec_helper.rb +87 -0
  71. data/spec/views/stripe_webhooks/events/create.html.erb_spec.rb +5 -0
  72. metadata +257 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd1f08430b1ef364a86da41ffb13052b5f817413
4
+ data.tar.gz: a7d9e947785bcc9267cbade2ab1141a23f3a5e8b
5
+ SHA512:
6
+ metadata.gz: 9affb286ac0b56570d85ba2b30c30eb45e2c24229b7de964cd1af12246aba77d81f5e3424d8b6a51a18da11cf9370464a7743de8f7058d8d77f9d450e44c2eec
7
+ data.tar.gz: 5d26da0418c121b1c124cdc507a8290886133adf48b59c2bc2eafdd3baffa8182bf159625684a33b290bba3bb390578015686c346e8229b26543c31aeb05d10a
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Greg Woods
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # StripeWebhooks
2
+
3
+ **NOTICE**: This project is in early development. As such, I don't recommend using it for anything serious just yet. You have been warned!
4
+
5
+ StripeWebhooks is a Rails engine for dealing with data posted from [Stripe via Webhooks](https://stripe.com/docs/webhooks).
6
+
7
+ ## Goals
8
+
9
+ 1. Provide an endpoint for capturing POST data from Stripe
10
+ 2. Verify authenticity of data by checking against the Stripe API
11
+ 3. Run callbacks in response to desired events
12
+ 4. Enable an app to "catch up" with missed events in the case of a server outage
13
+ 5. Store as little Stripe data locally as possible
14
+
15
+ ## Requirements
16
+
17
+ - [Stripe](https://rubygems.org/gems/stripe) 1.23 or higher
18
+ - Rails 4.2
19
+ - Ruby 2.2
20
+
21
+ ## Installation
22
+
23
+ First, install and configure the [stripe](https://rubygems.org/gems/stripe) gem according to their instructions. Then:
24
+
25
+ 1. Add the gem to your Gemfile
26
+
27
+ gem 'stripe_webhooks'
28
+
29
+ 2. Run bundle install
30
+ 3. Copy the database migrations to your rails project
31
+
32
+ bundle exec rake railties:install:migrations
33
+ rake db:migrate
34
+
35
+ 4. Mount the engine in your routes.rb file
36
+
37
+ mount StripeWebhooks::Engine => "/stripe_webhooks"
38
+
39
+ 5. Restart your application
40
+
41
+ ## Usage
42
+
43
+ TODO: This project is in the early development phase. This section will be updated as soon as the dust starts to settle.
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'StripeWebhooks'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module StripeWebhooks
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,26 @@
1
+ require_dependency "stripe_webhooks/application_controller"
2
+
3
+ module StripeWebhooks
4
+ class EventsController < ApplicationController
5
+ protect_from_forgery :except => :create
6
+
7
+ def create
8
+ begin
9
+ raw_data = JSON.parse(request.body.read)
10
+ rescue JSON::ParserError => e
11
+ render :text => e.message, :status => 400
12
+ return
13
+ end
14
+
15
+ @event = StripeWebhooks::Event.find_or_initialize_by(:stripe_event_id => raw_data['id'])
16
+
17
+ if @event.save()
18
+ StripeWebhooks::ProcessorJob.perform_later(@event)
19
+ render :nothing => true, :status => 200
20
+ else
21
+ render :text => @event.errors.full_messages.first, :status => 422
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ module StripeWebhooks
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StripeWebhooks
2
+ module EventsHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ module StripeWebhooks
2
+ class ProcessorJob < ActiveJob::Base
3
+ queue_as :default
4
+
5
+ def perform(event)
6
+ puts "running!"
7
+ event.validate!
8
+ if event.is_authentic?
9
+ event.run_callbacks!
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,62 @@
1
+ module StripeWebhooks
2
+ class Callback
3
+
4
+ module ClassMethods
5
+ attr_reader :event_types
6
+
7
+ # Run callbacks for a given event type
8
+ #
9
+ # === Example
10
+ #
11
+ # StripeWebhooks::Callback.run_callbacks_for('customer.subscription.deleted', stripe_event)
12
+ #
13
+ def run_callbacks_for(event_type, event)
14
+ StripeWebhooks.callbacks.each do |label|
15
+ class_name = "#{label.classify}Callback"
16
+ callback = class_name.constantize.new
17
+ if callback.handles?(event_type)
18
+ callback.run_once(event)
19
+ end
20
+ end
21
+ end
22
+
23
+ # Declares which event types a callback will handle
24
+ #
25
+ # === Example
26
+ #
27
+ # class MyCallback < StripeWebhooks::Callback
28
+ # handles_events 'customer.subscription.deleted', 'customer.subscription.testing'
29
+ # def run(event)
30
+ # puts "Do useful stuff"
31
+ # end
32
+ # end
33
+ #
34
+ def handles_events(*event_types)
35
+ @event_types = event_types
36
+ end
37
+ end
38
+ extend ClassMethods
39
+
40
+ # Return the label for the callback. Label is based on the class name.
41
+ #
42
+ def label
43
+ return self.class.to_s.underscore.gsub('_callback', '')
44
+ end
45
+
46
+ # Run the callback only if we have not run it once before
47
+ #
48
+ def run_once(event)
49
+ if !StripeWebhooks::PerformedCallback.exists?(:stripe_event_id => event.id, :label => label)
50
+ run(event)
51
+ StripeWebhooks::PerformedCallback.create(:stripe_event_id => event.id, :label => label)
52
+ end
53
+ end
54
+
55
+ # Return true if the Callback handles this type of event
56
+ #
57
+ def handles?(event_type)
58
+ return self.class.event_types.include?(event_type)
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,37 @@
1
+ module StripeWebhooks
2
+ class Event < ActiveRecord::Base
3
+ validates :stripe_event_id, :presence => true, :uniqueness => true
4
+ has_many :performed_callbacks, :primary_key => :stripe_event_id, :foreign_key => :stripe_event_id
5
+
6
+ def stripe_event
7
+ if created_at < 30.days.ago
8
+ Rails.logger.warn('The event you requested was created over 30 days ago, which means it may no longer be available via the Stripe Events API.')
9
+ end
10
+ @_stripe_event ||= Stripe::Event.retrieve(stripe_event_id)
11
+ return @_stripe_event
12
+ end
13
+
14
+ def validate!
15
+ return true if is_authentic
16
+ begin
17
+ event = stripe_event()
18
+ update_attributes({
19
+ :is_processed => true,
20
+ :is_authentic => true,
21
+ :stripe_event_type => event.type,
22
+ :stripe_created_at => Time.at(event.created).to_datetime
23
+ })
24
+ return true
25
+ rescue Stripe::InvalidRequestError
26
+ update_attributes(:is_processed => true, :is_authentic => false)
27
+ return false
28
+ end
29
+ end
30
+
31
+ def run_callbacks!
32
+ StripeWebhooks::Callback.run_callbacks_for(self.stripe_event_type, self.stripe_event)
33
+ return true
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module StripeWebhooks
2
+ class PerformedCallback < ActiveRecord::Base
3
+ belongs_to :event, :primary_key => :stripe_event_id, :foreign_key => :stripe_event_id
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>StripeWebhooks</title>
5
+ <%= stylesheet_link_tag "stripe_webhooks/application", media: "all" %>
6
+ <%= javascript_include_tag "stripe_webhooks/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ StripeWebhooks::Engine.routes.draw do
2
+ resources :events, :only => :create
3
+ end
@@ -0,0 +1,13 @@
1
+ class CreateStripeWebhooksEvents < ActiveRecord::Migration
2
+ def change
3
+ create_table :stripe_webhooks_events do |t|
4
+ t.string :stripe_event_id
5
+ t.index :stripe_event_id, :unique => true
6
+ t.string :stripe_event_type
7
+ t.datetime :stripe_created_at
8
+ t.boolean :is_processed, :default => false
9
+ t.boolean :is_authentic, :default => false
10
+ t.timestamps null: false
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ class CreateStripeWebhooksPerformedCallbacks < ActiveRecord::Migration
2
+ def change
3
+ create_table :stripe_webhooks_performed_callbacks do |t|
4
+ t.string :stripe_event_id, :null => false
5
+ t.string :label, :null => false
6
+ t.index [:stripe_event_id, :label], :name => 'index_stripe_webhooks_performed_callbacks_event_id_label'
7
+ t.timestamps null: false
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module StripeWebhooks
2
+ module Generators
3
+ class Base < Rails::Generators::NamedBase
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ require 'generators/stripe_webhooks'
2
+
3
+ module StripeWebhooks
4
+ module Generators
5
+ class CallbackGenerator < Base
6
+ desc "Creates a stripe webhook callback object"
7
+ argument :event_types, :type => :array, :default => [], :banner => 'event.type.a event.type.b ...'
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def create_callback
12
+ application_callback = 'app/callbacks/application_callback.rb'
13
+ if !File.exist?(application_callback)
14
+ template 'application_callback.rb.erb', application_callback
15
+ end
16
+
17
+ template 'callback.rb.erb', "app/callbacks/#{name.underscore}_callback.rb"
18
+
19
+ initializer = 'config/initializers/stripe_webhook_callbacks.rb'
20
+ if !File.exist?(initializer)
21
+ create_file(initializer)
22
+ end
23
+ append_to_file initializer, "StripeWebhooks.register_callback('#{name.underscore}')\n"
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationCallback < StripeWebhooks::Callback
2
+
3
+ end
@@ -0,0 +1,8 @@
1
+ class <%= name %>Callback < ApplicationCallback
2
+ handles_events <%= event_types.collect{ |event_type| "'#{event_type}'" }.join(', ') %>
3
+
4
+ def run(event)
5
+
6
+ end
7
+
8
+ end
@@ -0,0 +1,6 @@
1
+ require "stripe_webhooks/engine"
2
+ require "stripe_webhooks/callbacks"
3
+
4
+ module StripeWebhooks
5
+ include Callbacks
6
+ end
@@ -0,0 +1,24 @@
1
+ module StripeWebhooks
2
+ module Callbacks
3
+
4
+ def self.included(base)
5
+ base.instance_variable_set(:@callbacks, [])
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ attr_reader :callbacks
11
+
12
+ # Add a callback to be ran whenever a new event is recorded
13
+ #
14
+ # === Example
15
+ #
16
+ # StripeWebhooks.register_callback('my_callback')
17
+ #
18
+ def register_callback(label)
19
+ @callbacks << label
20
+ end
21
+ end
22
+
23
+ end
24
+ end