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