FIX: Login required for Stripe Checkout (#209)

If an anonymous user tries to subscribe we need to show them a log in
message first. We currently don't have support for anonymous
subscriptions.
This commit is contained in:
Blake Erickson 2024-05-03 17:01:51 -06:00 committed by GitHub
parent 5eba613f8f
commit e132913db4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 2 deletions

View File

@ -34,11 +34,14 @@ module DiscourseSubscriptions
return head 200 if checkout_session[:status] != "complete" return head 200 if checkout_session[:status] != "complete"
return render_json_error "customer not found" if checkout_session[:customer].nil? 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] customer_id = checkout_session[:customer]
user = ::User.find_by_username_or_email(email) user = ::User.find_by_username_or_email(email)
return render_json_error "customer not found" if !user
discourse_customer = Customer.create(user_id: user.id, customer_id: customer_id) discourse_customer = Customer.create(user_id: user.id, customer_id: customer_id)
Subscription.create( Subscription.create(

View File

@ -1,3 +1,7 @@
<div class="container"> <div class="container">
{{pricingTable}} {{#if this.currentUser}}
{{this.pricingTable}}
{{else}}
<LoginRequired />
{{/if}}
</div> </div>

View File

@ -184,6 +184,44 @@ RSpec.describe DiscourseSubscriptions::HooksController do
end end
end end
describe "checkout.session.completed with anonymous user" do
before do
checkout_session_completed_bad_data[:object][:customer_email] = "anonymous@example.com"
data = checkout_session_completed_bad_data
event = { type: "checkout.session.completed", data: data }
::Stripe::Checkout::Session
.stubs(:list_line_items)
.with(checkout_session_completed_data[:object][:id], { limit: 1 })
.returns(list_line_items_data)
::Stripe::Webhook.stubs(:construct_event).returns(event)
end
it "is returns 422" do
post "/s/hooks.json"
expect(response.status).to eq 422
end
end
describe "checkout.session.completed with no customer email" do
before do
checkout_session_completed_bad_data[:object][:customer_email] = nil
data = checkout_session_completed_bad_data
event = { type: "checkout.session.completed", data: data }
::Stripe::Checkout::Session
.stubs(:list_line_items)
.with(checkout_session_completed_data[:object][:id], { limit: 1 })
.returns(list_line_items_data)
::Stripe::Webhook.stubs(:construct_event).returns(event)
end
it "is returns 422" do
post "/s/hooks.json"
expect(response.status).to eq 422
end
end
describe "customer.subscription.updated" do describe "customer.subscription.updated" do
before do before do
event = { type: "customer.subscription.updated", data: event_data } event = { type: "customer.subscription.updated", data: event_data }

View File

@ -7,7 +7,6 @@ RSpec.describe "Pricing Table", type: :system, js: true do
let(:product_subscriptions_page) { PageObjects::Pages::AdminSubscriptionProduct.new } let(:product_subscriptions_page) { PageObjects::Pages::AdminSubscriptionProduct.new }
before do before do
sign_in(admin)
SiteSetting.discourse_subscriptions_enabled = true SiteSetting.discourse_subscriptions_enabled = true
SiteSetting.discourse_subscriptions_extra_nav_subscribe = true SiteSetting.discourse_subscriptions_extra_nav_subscribe = true
@ -33,6 +32,7 @@ RSpec.describe "Pricing Table", type: :system, js: true do
end end
it "Links to the pricing table page" do it "Links to the pricing table page" do
sign_in(admin)
visit("/") visit("/")
link = find("li.nav-item_subscribe a") link = find("li.nav-item_subscribe a")
@ -41,6 +41,7 @@ RSpec.describe "Pricing Table", type: :system, js: true do
end end
it "Links to the old page when disabled" do it "Links to the old page when disabled" do
sign_in(admin)
SiteSetting.discourse_subscriptions_pricing_table_enabled = false SiteSetting.discourse_subscriptions_pricing_table_enabled = false
visit("/") visit("/")
@ -50,6 +51,7 @@ RSpec.describe "Pricing Table", type: :system, js: true do
end end
it "Old subscribe page still works when disabled" do it "Old subscribe page still works when disabled" do
sign_in(admin)
SiteSetting.discourse_subscriptions_pricing_table_enabled = false SiteSetting.discourse_subscriptions_pricing_table_enabled = false
visit("/") visit("/")
@ -58,6 +60,7 @@ RSpec.describe "Pricing Table", type: :system, js: true do
end end
it "Shows a message when not setup yet" do it "Shows a message when not setup yet" do
sign_in(admin)
visit("/") visit("/")
find("li.nav-item_subscribe a").click find("li.nav-item_subscribe a").click
@ -67,4 +70,15 @@ RSpec.describe "Pricing Table", type: :system, js: true do
text: "There are currently no products available.", text: "There are currently no products available.",
) )
end end
it "Shows a log in message if not signed in" do
visit("/")
find("li.nav-item_subscribe a").click
expect(page).to have_selector(
"div.container",
text: "Log in or create an account to subscribe.",
)
end
end end