From ebe891c09b6b84734f4479304420fe69d6d44cec Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Sat, 25 Jan 2020 15:06:05 +1100 Subject: [PATCH] fix event hash for subscription --- README.md | 6 ++- app/controllers/hooks_controller.rb | 7 +-- spec/requests/hooks_controller_spec.rb | 73 ++++++++++++++++++-------- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index e999c23..6f36b20 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,14 @@ When you get a moment, take a look at Stripe's documentation. But for now, you c ### Enable Webhooks in your Stripe account You'll need to tell Stripe where your end points are. You can enter this in your Stripe dashboard. +Also: Add the webhook secret in settings (above). The address for webhooks is: `[your server address]/s/hooks` -Also: Add the webhook secret in settings (above). +Discourse Subscriptions responds to the following events: + +* `customer.subscription.deleted` +* `customer.subscription.updated` ### Set up your User Groups in Discourse diff --git a/app/controllers/hooks_controller.rb b/app/controllers/hooks_controller.rb index babfa31..c9a9ad2 100644 --- a/app/controllers/hooks_controller.rb +++ b/app/controllers/hooks_controller.rb @@ -22,18 +22,19 @@ module DiscourseSubscriptions end case event[:type] + when 'customer.subscription.updated' when 'customer.subscription.deleted' customer = Customer.find_by( - customer_id: event[:customer], - product_id: event[:plan][:product] + customer_id: event[:data][:object][:customer], + product_id: event[:data][:object][:plan][:product] ) if customer customer.delete user = ::User.find(customer.user_id) - group = plan_group(event[:plan]) + group = plan_group(event[:data][:object][:plan]) group.remove(user) if group end end diff --git a/spec/requests/hooks_controller_spec.rb b/spec/requests/hooks_controller_spec.rb index 50d27a6..c0c25ed 100644 --- a/spec/requests/hooks_controller_spec.rb +++ b/spec/requests/hooks_controller_spec.rb @@ -22,39 +22,66 @@ module DiscourseSubscriptions expect(response.status).to eq 200 end - describe "canceling a subscription" do + describe "event types" do let(:user) { Fabricate(:user) } - let(:group) { Fabricate(:group, name: 'subscribers-group') } let(:customer) { Fabricate(:customer, customer_id: 'c_575768', product_id: 'p_8654', user_id: user.id) } - before do - event = { - type: 'customer.subscription.deleted', - customer: customer.customer_id, - plan: { product: customer.product_id, metadata: { group_name: group.name } } - } + describe "customer.subscription.updated" do + before do + event = { + type: 'customer.subscription.updated', + data: { + object: {} + } + } - ::Stripe::Webhook - .stubs(:construct_event) - .returns(event) + ::Stripe::Webhook + .stubs(:construct_event) + .returns(event) + end - group.add(user) + it 'is successfull' do + post "/s/hooks.json" + expect(response.status).to eq 200 + end end - it "deletes the customer" do - expect { - post "/s/hooks.json" - }.to change { DiscourseSubscriptions::Customer.count }.by(-1) + describe "customer.subscription.deleted" do + let(:group) { Fabricate(:group, name: 'subscribers-group') } - expect(response.status).to eq 200 - end + before do + event = { + type: 'customer.subscription.deleted', + data: { + object: { + customer: customer.customer_id, + plan: { product: customer.product_id, metadata: { group_name: group.name } } + } + } + } - it "removes the user from the group" do - expect { - post "/s/hooks.json" - }.to change { user.groups.count }.by(-1) + ::Stripe::Webhook + .stubs(:construct_event) + .returns(event) - expect(response.status).to eq 200 + group.add(user) + end + + it "deletes the customer" do + expect { + post "/s/hooks.json" + }.to change { DiscourseSubscriptions::Customer.count }.by(-1) + + expect(response.status).to eq 200 + end + + it "removes the user from the group" do + expect { + post "/s/hooks.json" + }.to change { user.groups.count }.by(-1) + + expect(response.status).to eq 200 + end end end end