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.
This commit is contained in:
Blake Erickson 2024-08-28 11:27:55 -06:00 committed by GitHub
parent 71643a023d
commit a2c70deeff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 6 deletions

View File

@ -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)

View File

@ -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