From ed375a1e2c880c9673a278de6ee1e5c0e71b6d7e Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 12 Oct 2017 16:31:17 +1100 Subject: [PATCH] Build Event object in controller instead of service object --- app/controllers/stripe/webhooks_controller.rb | 3 ++- lib/stripe/webhook_handler.rb | 4 ++-- spec/lib/stripe/webhook_handler_spec.rb | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/controllers/stripe/webhooks_controller.rb b/app/controllers/stripe/webhooks_controller.rb index 7ad7c1e507..c80dc500ae 100644 --- a/app/controllers/stripe/webhooks_controller.rb +++ b/app/controllers/stripe/webhooks_controller.rb @@ -7,7 +7,8 @@ module Stripe # POST /stripe/webhook def create # TODO is there a sensible way to confirm this webhook call is actually from Stripe? - handler = WebhookHandler.new(params) + event = Event.construct_from(params) + handler = WebhookHandler.new(event) result = handler.handle render nothing: true, status: status_mappings[result] diff --git a/lib/stripe/webhook_handler.rb b/lib/stripe/webhook_handler.rb index 7a5332bacd..da5482d723 100644 --- a/lib/stripe/webhook_handler.rb +++ b/lib/stripe/webhook_handler.rb @@ -1,7 +1,7 @@ module Stripe class WebhookHandler - def initialize(params) - @event = Event.construct_from(params) + def initialize(event) + @event = event end def handle diff --git a/spec/lib/stripe/webhook_handler_spec.rb b/spec/lib/stripe/webhook_handler_spec.rb index fea36f21da..1795430b9f 100644 --- a/spec/lib/stripe/webhook_handler_spec.rb +++ b/spec/lib/stripe/webhook_handler_spec.rb @@ -3,8 +3,8 @@ require 'stripe/webhook_handler' module Stripe describe WebhookHandler do - let(:params) { { type: 'some.event' } } - let(:handler) { WebhookHandler.new(params) } + let(:event) { double(:event, type: 'some.event') } + let(:handler) { WebhookHandler.new(event) } describe "event_mappings" do it { expect(handler.send(:event_mappings)).to be_a Hash } @@ -62,7 +62,7 @@ module Stripe context "when the event has an 'account' attribute" do before do - params[:account] = 'some.account' + allow(event).to receive(:account) { 'some.account' } end context "when some stripe accounts are destroyed" do