From 6f54494a3b192716c434b7e66437afa3641f212f Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Mon, 29 Apr 2024 15:50:05 -0600 Subject: [PATCH] FIX: Allow active status for updated webhook (#203) Likely there was an API change at some point now that we are using the latest version of the stripe API. When the `customer.subscription.updated` webhook is called we should also accept the status of "active". --- .../discourse_subscriptions/hooks_controller.rb | 4 +++- spec/requests/hooks_controller_spec.rb | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/controllers/discourse_subscriptions/hooks_controller.rb b/app/controllers/discourse_subscriptions/hooks_controller.rb index cf235e8..7e3914e 100644 --- a/app/controllers/discourse_subscriptions/hooks_controller.rb +++ b/app/controllers/discourse_subscriptions/hooks_controller.rb @@ -64,6 +64,9 @@ module DiscourseSubscriptions ) when "customer.subscription.created" when "customer.subscription.updated" + status = event[:data][:object][:status] + return head 200 if !%w[complete active].include?(status) + customer = Customer.find_by( customer_id: event[:data][:object][:customer], @@ -71,7 +74,6 @@ module DiscourseSubscriptions ) return render_json_error "customer not found" if !customer - return head 200 if event[:data][:object][:status] != "complete" user = ::User.find_by(id: customer.user_id) return render_json_error "user not found" if !user diff --git a/spec/requests/hooks_controller_spec.rb b/spec/requests/hooks_controller_spec.rb index 8cc94b7..fd6a3b3 100644 --- a/spec/requests/hooks_controller_spec.rb +++ b/spec/requests/hooks_controller_spec.rb @@ -204,6 +204,14 @@ RSpec.describe DiscourseSubscriptions::HooksController do expect(response.status).to eq 200 end + + it "adds the user to the group when status is active" do + event_data[:object][:status] = "active" + + expect { post "/s/hooks.json" }.to change { user.groups.count }.by(1) + + expect(response.status).to eq 200 + end end end