From a2c70deeffc4aa394c3b15ac760793053b921df7 Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Wed, 28 Aug 2024 11:27:55 -0600 Subject: [PATCH] FIX: Create customer for one off pricing table purchases (#232) When using the Stripe Pricing Table for one-off purchases (non-subscription) stripe doesn't automatically create a customer record. This was causing the pricing table webhook endpoint to end prematurely and not add users to their appropriate group after purchase. Now if we detect that there is no customer record we will make an api call to Stripe to create one. --- .../discourse_subscriptions/hooks_controller.rb | 10 +++++++--- spec/requests/hooks_controller_spec.rb | 9 ++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/controllers/discourse_subscriptions/hooks_controller.rb b/app/controllers/discourse_subscriptions/hooks_controller.rb index f847254..cc5e143 100644 --- a/app/controllers/discourse_subscriptions/hooks_controller.rb +++ b/app/controllers/discourse_subscriptions/hooks_controller.rb @@ -37,10 +37,14 @@ module DiscourseSubscriptions email = checkout_session[:customer_email] return head 200 if checkout_session[:status] != "complete" - return render_json_error "customer not found" if checkout_session[:customer].nil? return render_json_error "email not found" if !email - customer_id = checkout_session[:customer] + if checkout_session[:customer].nil? + customer = ::Stripe::Customer.create({ email: email }) + customer_id = customer[:id] + else + customer_id = checkout_session[:customer] + end if SiteSetting.discourse_subscriptions_enable_verbose_logging Rails.logger.warn("Looking up user with email: #{email}") @@ -48,7 +52,7 @@ module DiscourseSubscriptions user = ::User.find_by_username_or_email(email) - return render_json_error "customer not found" if !user + return render_json_error "user not found" if !user discourse_customer = Customer.create(user_id: user.id, customer_id: customer_id) diff --git a/spec/requests/hooks_controller_spec.rb b/spec/requests/hooks_controller_spec.rb index ccb2ca2..9641036 100644 --- a/spec/requests/hooks_controller_spec.rb +++ b/spec/requests/hooks_controller_spec.rb @@ -88,7 +88,7 @@ RSpec.describe DiscourseSubscriptions::HooksController do object: { id: "cs_test_a1ENei5A9TGOaEketyV5qweiQR5CyJWHT5j8T3HheQY3uah3RxzKttVUKZ", object: "checkout.session", - customer: customer.customer_id, + customer: nil, customer_email: user.email, invoice: nil, metadata: { @@ -110,7 +110,7 @@ RSpec.describe DiscourseSubscriptions::HooksController do id: "cs_test_a1ENei5A9TGOaEketyV5qweiQR5CyJWHT5j8T3HheQY3uah3RxzKttVUKZ", object: "checkout.session", customer: nil, - customer_email: user.email, + customer_email: nil, invoice: "in_1P9b7iEYXaQnncSh81AQtuHD", metadata: { }, @@ -118,7 +118,7 @@ RSpec.describe DiscourseSubscriptions::HooksController do payment_status: "paid", status: "complete", submit_type: nil, - subscription: "sub_1P9b7iEYXaQnncSh3H3G9d2Y", + subscription: nil, success_url: "http://localhost:4200/my/billing/subscriptions", url: nil, }, @@ -197,6 +197,7 @@ RSpec.describe DiscourseSubscriptions::HooksController do .returns(list_line_items_data) ::Stripe::Webhook.stubs(:construct_event).returns(event) + ::Stripe::Customer.stubs(:create).returns(id: "cus_1234") end it "is returns 422" do @@ -217,6 +218,7 @@ RSpec.describe DiscourseSubscriptions::HooksController do .returns(list_line_items_data) ::Stripe::Webhook.stubs(:construct_event).returns(event) + ::Stripe::Customer.stubs(:create).returns(id: "cus_1234") end it "is returns 200" do @@ -236,6 +238,7 @@ RSpec.describe DiscourseSubscriptions::HooksController do .returns(list_line_items_data) ::Stripe::Webhook.stubs(:construct_event).returns(event) + ::Stripe::Customer.stubs(:create).returns(id: "cus_1234") end it "is returns 422" do