Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2 from rrix/bm5k/webhooks

Bm5k/webhooks -- Create webhooks controller
  • Loading branch information...
commit eba6ae87572f4c289c780b8efdd77741062b7602 2 parents 1d845e7 + 3ea3b00
@BM5k authored
View
11 app/controllers/webhook_controller.rb
@@ -0,0 +1,11 @@
+class WebhookController < ApplicationController
+
+ def index
+ params.reject! { |k,v| k.to_s == 'stripe_webhook' }
+
+ Webhook.create_from_stripe! params
+ head :ok
+
+ end
+
+end
View
26 app/models/webhook.rb
@@ -2,14 +2,30 @@ class Webhook < ActiveRecord::Base
belongs_to :user
- validates :stripe_webhook_id, presence: true, uniqueness: true
- validates :stripe_webhook_type, presence: true
- validates :object, presence: true
- validates :livemode, presence: true
+ validates :stripe_webhook_id, presence: true, uniqueness: true
+ validates :stripe_webhook_type, presence: true
+ validates :object, presence: true
before_validation :set_user, :on => :create
after_create :send_notification
+ def self.initialize_from_stripe attrs
+ new stripe_webhook_id: attrs['id'],
+ livemode: attrs['livemode'],
+ stripe_webhook_type: attrs['type'],
+ data: attrs['data']
+ end
+
+ def self.create_from_stripe! attrs
+ hook = initialize_from_stripe attrs
+ hook.save!
+ hook
+ end
+
+ def data= hash
+ self.object = hash['object']
+ end
+
def stripe_customer_id
return nil if object.blank?
object['customer']
@@ -22,7 +38,7 @@ def set_user
end
def send_notification
- ActiveSupport::Notifications.instrument "stripe.#{ stripe_event_type }", { user_id: user_id, object: object }
+ ActiveSupport::Notifications.instrument "stripe.#{ stripe_webhook_type }", { user_id: user_id, object: object }
end
end
View
10 db/migrate/20120215185005_create_webhooks.rb
@@ -4,10 +4,10 @@ def change
create_table :webhooks do |t|
t.belongs_to :user
- t.string :stripe_webhook_id, null: false
- t.string :stripe_webhook_type, null: false
- t.text :object, null: false
- t.boolean :livemode, null: false, default: false
+ t.string :stripe_webhook_id, null: false
+ t.string :stripe_webhook_type, null: false
+ t.text :object, null: false
+ t.boolean :livemode, default: false
t.timestamps
end
@@ -16,4 +16,4 @@ def change
add_index :webhooks, :user_id
end
-end
+end
View
40 spec/controllers/webhook_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+describe WebhookController do
+
+ describe 'receiving webhooks from stripe' do
+
+ before do
+ @hash = load_webhook_json 'invoice.payment_failed'
+ post :index, @hash
+ @webhook = Webhook.last
+ end
+
+ it 'returns 200' do
+ response.status.should == 200
+ end
+
+ it 'an empty body' do
+ response.body.should == ' '
+ end
+
+ it 'sets the stripe_event_id' do
+ @webhook.stripe_webhook_id.should == @hash['id']
+ end
+
+ it 'sets the stripe_event_type' do
+ @webhook.stripe_webhook_type.should == @hash['type']
+ end
+
+ it 'sets "livemode" flag' do
+ @webhook.livemode.should == @hash['livemode']
+ end
+
+ it 'caches the data object' do
+ pending 'Serilization does creepy things with my datatypes: http://stackoverflow.com/questions/9087068/how-to-test-json-serialization-in-activerecord'
+ @webhook.object.should == @hash['data']['object']
+ end
+
+ end
+
+end
View
59 spec/data/stripe/charge.failed.json
@@ -0,0 +1,59 @@
+{
+ "action": "index",
+ "controller": "stripe_webhook",
+ "created": 1326853478,
+ "data": {
+ "object": {
+ "amount": 100,
+ "card": {
+ "country": null,
+ "exp_month": 1,
+ "exp_year": 2012,
+ "id": "cc_00000000000000",
+ "last4": "0002",
+ "object": "card",
+ "type": "Visa"
+ },
+ "created": 1327102285,
+ "currency": "usd",
+ "fee": null,
+ "id": "ch_00000000000000",
+ "livemode": false,
+ "object": "charge",
+ "paid": false,
+ "refunded": false
+ }
+ },
+ "format": "json",
+ "id": "note_00000000000000",
+ "livemode": false,
+ "stripe_webhook": {
+ "created": 1326853478,
+ "data": {
+ "object": {
+ "amount": 100,
+ "card": {
+ "country": null,
+ "exp_month": 1,
+ "exp_year": 2012,
+ "id": "cc_00000000000000",
+ "last4": "0002",
+ "object": "card",
+ "type": "Visa"
+ },
+ "created": 1327102285,
+ "currency": "usd",
+ "fee": null,
+ "id": "ch_00000000000000",
+ "livemode": false,
+ "object": "charge",
+ "paid": false,
+ "refunded": false
+ }
+ },
+ "id": "note_00000000000000",
+ "livemode": false,
+ "type": "charge.failed"
+ },
+ "type": "charge.failed"
+}
View
88 spec/data/stripe/invoice.payment_failed.json
@@ -0,0 +1,88 @@
+{
+ "action": "index",
+ "controller": "stripe_webhook",
+ "created": 1326853478,
+ "data": {
+ "object": {
+ "attempted": true,
+ "charge": "ch_00000000000000",
+ "closed": true,
+ "customer": "cus_00000000000000",
+ "date": 1327101120,
+ "id": "in_00000000000000",
+ "lines": {
+ "subscriptions": [
+ {
+ "amount": 999,
+ "period": {
+ "end": 1329779520,
+ "start": 1327101120
+ },
+ "plan": {
+ "amount": 999,
+ "currency": "usd",
+ "id": "plan",
+ "interval": "month",
+ "livemode": false,
+ "name": "New Plan",
+ "object": "plan"
+ }
+ }
+ ]
+ },
+ "livemode": false,
+ "object": "invoice",
+ "paid": false,
+ "period_end": 1327101120,
+ "period_start": 1327101120,
+ "subtotal": 999,
+ "total": 999
+ }
+ },
+ "id": "note_00000000000000",
+ "livemode": false,
+ "stripe_webhook": {
+ "created": 1326853478,
+ "data": {
+ "object": {
+ "attempted": true,
+ "charge": "ch_00000000000000",
+ "closed": true,
+ "customer": "cus_00000000000000",
+ "date": 1327101120,
+ "id": "in_00000000000000",
+ "lines": {
+ "subscriptions": [
+ {
+ "amount": 999,
+ "period": {
+ "end": 1329779520,
+ "start": 1327101120
+ },
+ "plan": {
+ "amount": 999,
+ "currency": "usd",
+ "id": "plan",
+ "interval": "month",
+ "livemode": false,
+ "name": "New Plan",
+ "object": "plan"
+ }
+ }
+ ]
+ },
+ "livemode": false,
+ "object": "invoice",
+ "paid": false,
+ "period_end": 1327101120,
+ "period_start": 1327101120,
+ "subtotal": 999,
+ "total": 999
+ }
+ },
+ "id": "note_00000000000000",
+ "livemode": false,
+ "type": "invoice.payment_failed"
+ },
+ "type": "invoice.payment_failed"
+}
View
1  spec/models/webhook_spec.rb
@@ -2,7 +2,6 @@
describe Webhook do
- its(:livemode) { should validate :presence }
its(:object) { should validate :presence }
its(:stripe_webhook_id) { should validate :presence }
its(:stripe_webhook_id) { should validate :uniqueness }
View
2  spec/requests/subscriptions_spec.rb
@@ -99,7 +99,7 @@
end
- context 'with simulated svalid data' do
+ context 'with simulated valid data' do
use_vcr_cassette 'stripe/billing-update-success'
View
6 spec/support/stripe_support.rb
@@ -8,3 +8,9 @@ def valid_card_data
exp_year: exp_date.year
}
end
+
+def load_webhook_json webhook_name
+ path = Rails.root.join *%W[ spec data stripe #{ webhook_name }.json ]
+ json = File.open(path).read
+ JSON.parse json
+end
Please sign in to comment.
Something went wrong with that request. Please try again.