Skip to content

A platform for community discussion. Free, open, simple.

FIX: Preserve Stripe promotion-code policy in one-time checkout (#45239)

Isaac Janzen7457b259b4dd
diff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/subscribe_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/subscribe_controller.rb
index aa77157286e..3c0a1195b96 100644
--- a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/subscribe_controller.rb
+++ b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/subscribe_controller.rb
@@ -111,8 +111,7 @@ module DiscourseSubscriptions
:status
] == "incomplete"
else
- coupon_id = promo_code[:coupon][:id] if promo_code && promo_code[:coupon] &&
- promo_code[:coupon][:id]
+ promo_code_id = promo_code[:id] if promo_code
invoice_params = { customer: customer[:id] }
if SiteSetting.discourse_subscriptions_enable_automatic_tax
@@ -124,7 +123,7 @@ module DiscourseSubscriptions
{
customer: customer[:id],
price: params[:plan],
- discounts: [{ coupon: coupon_id }],
+ discounts: [{ promotion_code: promo_code_id }],
invoice: invoice[:id],
},
stripe_request_opts,
diff --git a/plugins/discourse-subscriptions/spec/requests/subscribe_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/subscribe_controller_spec.rb
index bc404f203b5..4e363f8e6b7 100644
--- a/plugins/discourse-subscriptions/spec/requests/subscribe_controller_spec.rb
+++ b/plugins/discourse-subscriptions/spec/requests/subscribe_controller_spec.rb
@@ -680,7 +680,7 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
}.to change { DiscourseSubscriptions::Customer.count }
end
- it "applies promo code to one time purchase" do
+ it "preserves promotion-code restrictions for a one-time purchase" do
::Stripe::Price
.expects(:retrieve)
.with(anything, DiscourseSubscriptions::Stripe.request_opts)
@@ -701,7 +701,7 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
{
customer: "cus_1234",
price: "plan_1234",
- discounts: [{ coupon: "c123" }],
+ discounts: [{ promotion_code: "promo123" }],
invoice: "in_123",
},
DiscourseSubscriptions::Stripe.request_opts,
@@ -735,6 +735,66 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
promo: "123",
}
}.to change { DiscourseSubscriptions::Customer.count }
+
+ expect(response).to have_http_status(:ok)
+ expect(response.parsed_body).to include("status" => "paid")
+ end
+
+ it "preserves promotion code restrictions for a one time paid group purchase" do
+ group = Fabricate(:group, name: "awesome")
+
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(
+ type: "one_time",
+ product: "product_12345",
+ metadata: {
+ group_name: "awesome",
+ },
+ )
+ ::Stripe::Invoice
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "open", id: "in_123")
+ ::Stripe::InvoiceItem.expects(:create).with(
+ {
+ customer: "cus_1234",
+ price: "plan_1234",
+ discounts: [{ promotion_code: "promo123" }],
+ invoice: "in_123",
+ },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
+ ::Stripe::Invoice
+ .expects(:finalize_invoice)
+ .with(anything, anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "in_123", status: "open", payment_intent: "pi_123")
+ ::Stripe::Invoice
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "in_123", status: "open", payment_intent: "pi_123")
+ ::Stripe::PaymentIntent
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "successful")
+ ::Stripe::Invoice
+ .expects(:pay)
+ .with(anything, anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "paid", customer: "cus_1234")
+ expect {
+ post "/s/create.json",
+ params: {
+ plan: "plan_1234",
+ source: "tok_1234",
+ promo: "123",
+ }
+ }.to change { DiscourseSubscriptions::Customer.count }.by(1).and change {
+ group.users.count
+ }.by(1)
+
+ expect(response).to have_http_status(:ok)
+ expect(response.parsed_body["status"]).to eq("paid")
end
end
end