Skip to content

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

SECURITY: Fixes for discourse-subscriptions

Alan Guo Xiang Tanc34f2aac8dcd
diff --git a/plugins/discourse-subscriptions/app/controllers/concerns/stripe.rb b/plugins/discourse-subscriptions/app/controllers/concerns/stripe.rb
index bbe8bbecfd2..bd4a18dbfe2 100644
--- a/plugins/discourse-subscriptions/app/controllers/concerns/stripe.rb
+++ b/plugins/discourse-subscriptions/app/controllers/concerns/stripe.rb
@@ -4,8 +4,12 @@ module DiscourseSubscriptions
module Stripe
extend ActiveSupport::Concern
- def set_api_key
- ::Stripe.api_key = SiteSetting.discourse_subscriptions_secret_key
+ def self.request_opts
+ { api_key: SiteSetting.discourse_subscriptions_secret_key }
+ end
+
+ def stripe_request_opts
+ DiscourseSubscriptions::Stripe.request_opts
end
def is_stripe_configured?
diff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/coupons_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/coupons_controller.rb
index 72adf183e36..10481cb2a07 100644
--- a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/coupons_controller.rb
+++ b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/coupons_controller.rb
@@ -8,11 +8,9 @@ module DiscourseSubscriptions
requires_plugin PLUGIN_NAME
- before_action :set_api_key
-
def index
begin
- promo_codes = ::Stripe::PromotionCode.list({ limit: 100 })[:data]
+ promo_codes = ::Stripe::PromotionCode.list({ limit: 100 }, stripe_request_opts)[:data]
promo_codes = promo_codes.select { |code| code[:coupon][:valid] == true }
render_json_dump promo_codes
rescue ::Stripe::InvalidRequestError => e
@@ -33,11 +31,12 @@ module DiscourseSubscriptions
coupon_params[:percent_off] = params[:discount]
end
- coupon = ::Stripe::Coupon.create(coupon_params)
+ coupon = ::Stripe::Coupon.create(coupon_params, stripe_request_opts)
promo_code =
::Stripe::PromotionCode.create(
{ coupon: coupon[:id], code: params[:promo] },
+ stripe_request_opts,
) if coupon.present?
render_json_dump promo_code
@@ -49,7 +48,12 @@ module DiscourseSubscriptions
def update
params.require(%i[id active])
begin
- promo_code = ::Stripe::PromotionCode.update(params[:id], { active: params[:active] })
+ promo_code =
+ ::Stripe::PromotionCode.update(
+ params[:id],
+ { active: params[:active] },
+ stripe_request_opts,
+ )
render_json_dump promo_code
rescue ::Stripe::InvalidRequestError => e
@@ -60,7 +64,7 @@ module DiscourseSubscriptions
def destroy
params.require(:coupon_id)
begin
- coupon = ::Stripe::Coupon.delete(params[:coupon_id])
+ coupon = ::Stripe::Coupon.delete(params[:coupon_id], {}, stripe_request_opts)
render_json_dump coupon
rescue ::Stripe::InvalidRequestError => e
render_json_error e.message
diff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/plans_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/plans_controller.rb
index fb5f8ea6471..b7a023b987a 100644
--- a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/plans_controller.rb
+++ b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/plans_controller.rb
@@ -7,11 +7,9 @@ module DiscourseSubscriptions
requires_plugin PLUGIN_NAME
- before_action :set_api_key
-
def index
begin
- plans = ::Stripe::Price.list(product_params)
+ plans = ::Stripe::Price.list(product_params, stripe_request_opts)
render_json_dump plans.data
rescue ::Stripe::InvalidRequestError => e
@@ -35,7 +33,7 @@ module DiscourseSubscriptions
price_object[:recurring] = { interval: params[:interval] } if params[:type] == "recurring"
- plan = ::Stripe::Price.create(price_object)
+ plan = ::Stripe::Price.create(price_object, stripe_request_opts)
render_json_dump plan
rescue ::Stripe::InvalidRequestError => e
@@ -45,7 +43,7 @@ module DiscourseSubscriptions
def show
begin
- plan = ::Stripe::Price.retrieve(params[:id])
+ plan = ::Stripe::Price.retrieve(params[:id], stripe_request_opts)
if plan[:metadata] && plan[:metadata][:trial_period_days]
trial_days = plan[:metadata][:trial_period_days]
@@ -74,12 +72,15 @@ module DiscourseSubscriptions
plan =
::Stripe::Price.update(
params[:id],
- nickname: params[:nickname],
- active: params[:active],
- metadata: {
- group_name: params[:metadata][:group_name],
- trial_period_days: params[:trial_period_days],
+ {
+ nickname: params[:nickname],
+ active: params[:active],
+ metadata: {
+ group_name: params[:metadata][:group_name],
+ trial_period_days: params[:trial_period_days],
+ },
},
+ stripe_request_opts,
)
render_json_dump plan
diff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/products_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/products_controller.rb
index 2c644c5d9c1..c6c37b78859 100644
--- a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/products_controller.rb
+++ b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/products_controller.rb
@@ -7,15 +7,13 @@ module DiscourseSubscriptions
requires_plugin PLUGIN_NAME
- before_action :set_api_key
-
def index
begin
product_ids = Product.all.pluck(:external_id)
products = []
if product_ids.present? && is_stripe_configured?
- products = ::Stripe::Product.list({ ids: product_ids, limit: 100 })
+ products = ::Stripe::Product.list({ ids: product_ids, limit: 100 }, stripe_request_opts)
products = products[:data]
elsif !is_stripe_configured?
products = nil
@@ -33,7 +31,7 @@ module DiscourseSubscriptions
create_params.except!(:statement_descriptor) if params[:statement_descriptor].blank?
- product = ::Stripe::Product.create(create_params)
+ product = ::Stripe::Product.create(create_params, stripe_request_opts)
Product.create(external_id: product[:id])
@@ -45,7 +43,7 @@ module DiscourseSubscriptions
def show
begin
- product = ::Stripe::Product.retrieve(params[:id])
+ product = ::Stripe::Product.retrieve(params[:id], stripe_request_opts)
render_json_dump product
rescue ::Stripe::InvalidRequestError => e
@@ -55,7 +53,7 @@ module DiscourseSubscriptions
def update
begin
- product = ::Stripe::Product.update(params[:id], product_params)
+ product = ::Stripe::Product.update(params[:id], product_params, stripe_request_opts)
render_json_dump product
rescue ::Stripe::InvalidRequestError => e
@@ -65,7 +63,7 @@ module DiscourseSubscriptions
def destroy
begin
- product = ::Stripe::Product.delete(params[:id])
+ product = ::Stripe::Product.delete(params[:id], {}, stripe_request_opts)
Product.delete_by(external_id: params[:id])
diff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/subscriptions_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/subscriptions_controller.rb
index 18984c36e12..5132944c4e4 100644
--- a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/subscriptions_controller.rb
+++ b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/subscriptions_controller.rb
@@ -7,7 +7,6 @@ module DiscourseSubscriptions
include DiscourseSubscriptions::Stripe
include DiscourseSubscriptions::Group
- before_action :set_api_key
PAGE_LIMIT = 10
@@ -53,7 +52,7 @@ module DiscourseSubscriptions
params.require(:id)
begin
refund_subscription(params[:id]) if ActiveModel::Type::Boolean.new.cast(params[:refund])
- subscription = ::Stripe::Subscription.cancel(params[:id])
+ subscription = ::Stripe::Subscription.cancel(params[:id], {}, stripe_request_opts)
customer =
Customer.find_by(
@@ -77,10 +76,13 @@ module DiscourseSubscriptions
def get_subscriptions(start)
::Stripe::Subscription.list(
- expand: ["data.plan.product"],
- limit: PAGE_LIMIT,
- starting_after: start,
- status: "all",
+ {
+ expand: ["data.plan.product"],
+ limit: PAGE_LIMIT,
+ starting_after: start,
+ status: "all",
+ },
+ stripe_request_opts,
)
end
@@ -91,12 +93,14 @@ module DiscourseSubscriptions
# this will only refund the most recent subscription payment
def refund_subscription(subscription_id)
- subscription = ::Stripe::Subscription.retrieve(subscription_id)
- invoice = ::Stripe::Invoice.retrieve(subscription[:latest_invoice]) if subscription[
- :latest_invoice
- ]
+ subscription = ::Stripe::Subscription.retrieve(subscription_id, stripe_request_opts)
+ invoice =
+ ::Stripe::Invoice.retrieve(
+ subscription[:latest_invoice],
+ stripe_request_opts,
+ ) if subscription[:latest_invoice]
payment_intent = invoice[:payment_intent] if invoice[:payment_intent]
- refund = ::Stripe::Refund.create({ payment_intent: payment_intent })
+ refund = ::Stripe::Refund.create({ payment_intent: payment_intent }, stripe_request_opts)
end
end
end
diff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/hooks_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/hooks_controller.rb
index 3025e26030b..b8631906915 100644
--- a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/hooks_controller.rb
+++ b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/hooks_controller.rb
@@ -9,7 +9,6 @@ module DiscourseSubscriptions
layout false
- before_action :set_api_key
skip_before_action :check_xhr
skip_before_action :redirect_to_login_if_required
skip_before_action :verify_authenticity_token, only: [:create]
@@ -42,7 +41,7 @@ module DiscourseSubscriptions
return render_json_error "email not found" if !email
if checkout_session[:customer].nil?
- customer = ::Stripe::Customer.create({ email: email })
+ customer = ::Stripe::Customer.create({ email: email }, stripe_request_opts)
customer_id = customer[:id]
else
customer_id = checkout_session[:customer]
@@ -65,7 +64,11 @@ module DiscourseSubscriptions
end
line_items =
- ::Stripe::Checkout::Session.list_line_items(checkout_session[:id], { limit: 1 })
+ ::Stripe::Checkout::Session.list_line_items(
+ checkout_session[:id],
+ { limit: 1 },
+ stripe_request_opts,
+ )
item = line_items[:data].first
group = plan_group(item[:price])
@@ -87,6 +90,7 @@ module DiscourseSubscriptions
::Stripe::Subscription.update(
subscription,
{ metadata: { user_id: user.id, username: user.username } },
+ stripe_request_opts,
)
end
when "customer.subscription.created"
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 2991bfee38b..57fdecf230f 100644
--- a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/subscribe_controller.rb
+++ b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/subscribe_controller.rb
@@ -7,7 +7,6 @@ module DiscourseSubscriptions
requires_plugin PLUGIN_NAME
- before_action :set_api_key
requires_login except: %i[index contributors show]
def index
@@ -16,7 +15,7 @@ module DiscourseSubscriptions
products = []
if product_ids.present? && is_stripe_configured?
- response = ::Stripe::Product.list({ ids: product_ids, active: true })
+ response = ::Stripe::Product.list({ ids: product_ids, active: true }, stripe_request_opts)
products = response[:data].map { |p| serialize_product(p) }
end
@@ -46,8 +45,8 @@ module DiscourseSubscriptions
def show
params.require(:id)
begin
- product = ::Stripe::Product.retrieve(params[:id])
- plans = ::Stripe::Price.list(active: true, product: params[:id])
+ product = ::Stripe::Product.retrieve(params[:id], stripe_request_opts)
+ plans = ::Stripe::Price.list({ active: true, product: params[:id] }, stripe_request_opts)
response = { product: serialize_product(product), plans: serialize_plans(plans) }
@@ -66,10 +65,10 @@ module DiscourseSubscriptions
params[:cardholder_name],
params[:cardholder_address],
)
- plan = ::Stripe::Price.retrieve(params[:plan])
+ plan = ::Stripe::Price.retrieve(params[:plan], stripe_request_opts)
if params[:promo].present?
- promo_code = ::Stripe::PromotionCode.list({ code: params[:promo] })
+ promo_code = ::Stripe::PromotionCode.list({ code: params[:promo] }, stripe_request_opts)
promo_code = promo_code[:data][0] # we assume promo codes have a unique name
if promo_code.blank?
@@ -97,7 +96,7 @@ module DiscourseSubscriptions
subscription_params[:automatic_tax] = { enabled: true }
end
- transaction = ::Stripe::Subscription.create(subscription_params)
+ transaction = ::Stripe::Subscription.create(subscription_params, stripe_request_opts)
payment_intent = retrieve_payment_intent(transaction[:latest_invoice]) if transaction[
:status
@@ -110,16 +109,19 @@ module DiscourseSubscriptions
if SiteSetting.discourse_subscriptions_enable_automatic_tax
invoice_params[:automatic_tax] = { enabled: true }
end
- invoice = ::Stripe::Invoice.create(invoice_params)
+ invoice = ::Stripe::Invoice.create(invoice_params, stripe_request_opts)
invoice_item =
::Stripe::InvoiceItem.create(
- customer: customer[:id],
- price: params[:plan],
- discounts: [{ coupon: coupon_id }],
- invoice: invoice[:id],
+ {
+ customer: customer[:id],
+ price: params[:plan],
+ discounts: [{ coupon: coupon_id }],
+ invoice: invoice[:id],
+ },
+ stripe_request_opts,
)
- transaction = ::Stripe::Invoice.finalize_invoice(invoice[:id])
+ transaction = ::Stripe::Invoice.finalize_invoice(invoice[:id], {}, stripe_request_opts)
payment_intent = retrieve_payment_intent(transaction[:id]) if transaction[:status] ==
"open"
if payment_intent.nil?
@@ -127,11 +129,20 @@ module DiscourseSubscriptions
render_json_error I18n.t("js.discourse_subscriptions.subscribe.transaction_error")
)
end
- transaction = ::Stripe::Invoice.pay(invoice[:id]) if payment_intent[:status] ==
- "successful"
+ transaction =
+ ::Stripe::Invoice.pay(invoice[:id], {}, stripe_request_opts) if payment_intent[
+ :status
+ ] == "successful"
end
- finalize_transaction(transaction, plan) if transaction_ok(transaction)
+ if transaction_ok(transaction)
+ finalize_transaction(transaction, plan)
+ else
+ server_session["pending_subscription"] = {
+ transaction_id: transaction[:id],
+ plan_id: plan[:id],
+ }
+ end
transaction = transaction.to_h.merge(transaction, payment_intent: payment_intent)
@@ -142,13 +153,19 @@ module DiscourseSubscriptions
end
def finalize
- params.require(%i[plan transaction])
+ pending = server_session["pending_subscription"]
+ raise Discourse::InvalidAccess if pending.blank?
+
begin
- price = ::Stripe::Price.retrieve(params[:plan])
- transaction = retrieve_transaction(params[:transaction])
- finalize_transaction(transaction, price) if transaction_ok(transaction)
+ transaction = retrieve_transaction(pending[:transaction_id])
+ raise Discourse::InvalidAccess unless transaction_ok(transaction)
+
+ plan = ::Stripe::Price.retrieve(pending[:plan_id], stripe_request_opts)
+ finalize_transaction(transaction, plan)
+
+ server_session.delete("pending_subscription")
- render_json_dump params[:transaction]
+ render_json_dump pending[:transaction_id]
rescue ::Stripe::InvalidRequestError => e
render_json_error e.message
end
@@ -227,29 +244,32 @@ module DiscourseSubscriptions
)
if customer.present?
- ::Stripe::Customer.retrieve(customer.customer_id)
+ ::Stripe::Customer.retrieve(customer.customer_id, stripe_request_opts)
else
::Stripe::Customer.create(
- email: current_user.email,
- source: source,
- name: cardholder_name,
- address: cardholder_address,
+ {
+ email: current_user.email,
+ source: source,
+ name: cardholder_name,
+ address: cardholder_address,
+ },
+ stripe_request_opts,
)
end
end
def retrieve_payment_intent(invoice_id)
- invoice = ::Stripe::Invoice.retrieve(invoice_id)
- ::Stripe::PaymentIntent.retrieve(invoice[:payment_intent])
+ invoice = ::Stripe::Invoice.retrieve(invoice_id, stripe_request_opts)
+ ::Stripe::PaymentIntent.retrieve(invoice[:payment_intent], stripe_request_opts)
end
def retrieve_transaction(transaction)
begin
case transaction
when /^sub_/
- ::Stripe::Subscription.retrieve(transaction)
+ ::Stripe::Subscription.retrieve(transaction, stripe_request_opts)
when /^in_/
- ::Stripe::Invoice.retrieve(transaction)
+ ::Stripe::Invoice.retrieve(transaction, stripe_request_opts)
end
rescue ::Stripe::InvalidRequestError => e
e.message
diff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/payments_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/payments_controller.rb
index 55325ceeee7..6cbfa726031 100644
--- a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/payments_controller.rb
+++ b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/payments_controller.rb
@@ -7,7 +7,6 @@ module DiscourseSubscriptions
requires_plugin PLUGIN_NAME
- before_action :set_api_key
requires_login
def index
@@ -21,10 +20,11 @@ module DiscourseSubscriptions
if customer_ids.present? && product_ids.present?
customer_ids.each do |customer_id|
# lots of matching because the Stripe API doesn't make it easy to match products => payments except from invoices
- all_invoices = ::Stripe::Invoice.list(customer: customer_id)
+ all_invoices = ::Stripe::Invoice.list({ customer: customer_id }, stripe_request_opts)
invoices_with_products = parse_invoices(all_invoices, product_ids)
invoice_ids = invoices_with_products.map { |invoice| invoice[:id] }
- payments = ::Stripe::PaymentIntent.list(customer: customer_id)
+ payments =
+ ::Stripe::PaymentIntent.list({ customer: customer_id }, stripe_request_opts)
payments_from_invoices =
payments[:data].select { |payment| invoice_ids.include?(payment[:invoice]) }
@@ -89,9 +89,8 @@ module DiscourseSubscriptions
# Fetch charges in batches of 100, using pagination with starting_after
charges =
::Stripe::Charge.list(
- limit: 100,
- starting_after: starting_after,
- expand: ["data.payment_intent"],
+ { limit: 100, starting_after: starting_after, expand: ["data.payment_intent"] },
+ stripe_request_opts,
)
charges[:data].each do |charge|
diff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/subscriptions_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/subscriptions_controller.rb
index 733f4a8e82b..0983b79412f 100644
--- a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/subscriptions_controller.rb
+++ b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/subscriptions_controller.rb
@@ -8,7 +8,6 @@ module DiscourseSubscriptions
requires_plugin PLUGIN_NAME
- before_action :set_api_key
before_action :find_subscription, only: %i[update destroy]
requires_login
@@ -28,7 +27,7 @@ module DiscourseSubscriptions
prices = []
price_params = { limit: 100, expand: ["data.product"] }
loop do
- response = ::Stripe::Price.list(price_params)
+ response = ::Stripe::Price.list(price_params, stripe_request_opts)
prices.concat(response[:data])
break unless response[:has_more]
price_params[:starting_after] = response[:data].last.id
@@ -37,7 +36,10 @@ module DiscourseSubscriptions
stripe_customer_ids.each do |stripe_customer_id|
customer_subscriptions =
- ::Stripe::Subscription.list(customer: stripe_customer_id, status: "all")
+ ::Stripe::Subscription.list(
+ { customer: stripe_customer_id, status: "all" },
+ stripe_request_opts,
+ )
all_subscriptions.concat(customer_subscriptions[:data])
end
@@ -59,7 +61,12 @@ module DiscourseSubscriptions
# we cancel but don't remove until the end of the period
# full removal is done via webhooks
begin
- subscription = ::Stripe::Subscription.update(params[:id], { cancel_at_period_end: true })
+ subscription =
+ ::Stripe::Subscription.update(
+ params[:id],
+ { cancel_at_period_end: true },
+ stripe_request_opts,
+ )
if subscription
render_json_dump subscription
@@ -79,6 +86,7 @@ module DiscourseSubscriptions
::Stripe::Subscription.update(
params[:id],
{ default_payment_method: params[:payment_method] },
+ stripe_request_opts,
)
render json: success_json
rescue ::Stripe::InvalidRequestError
@@ -90,7 +98,11 @@ module DiscourseSubscriptions
def attach_method_to_customer(customer_id, method)
customer = Customer.find(customer_id)
- ::Stripe::PaymentMethod.attach(method, { customer: customer.customer_id })
+ ::Stripe::PaymentMethod.attach(
+ method,
+ { customer: customer.customer_id },
+ stripe_request_opts,
+ )
end
def find_subscription
diff --git a/plugins/discourse-subscriptions/app/services/discourse_subscriptions/campaign.rb b/plugins/discourse-subscriptions/app/services/discourse_subscriptions/campaign.rb
index c37410ebcae..68fd63f723f 100644
--- a/plugins/discourse-subscriptions/app/services/discourse_subscriptions/campaign.rb
+++ b/plugins/discourse-subscriptions/app/services/discourse_subscriptions/campaign.rb
@@ -3,9 +3,6 @@
module DiscourseSubscriptions
class Campaign
include DiscourseSubscriptions::Stripe
- def initialize
- set_api_key # instantiates Stripe API
- end
def refresh_data
product_ids = Set.new(Product.all.pluck(:external_id))
@@ -109,7 +106,7 @@ module DiscourseSubscriptions
},
}
- product = ::Stripe::Product.create(product_params)
+ product = ::Stripe::Product.create(product_params, stripe_request_opts)
Product.create(external_id: product[:id])
@@ -141,7 +138,7 @@ module DiscourseSubscriptions
},
}
- plan = ::Stripe::Price.create(price_object)
+ plan = ::Stripe::Price.create(price_object, stripe_request_opts)
end
def get_one_time_payments(product_ids)
@@ -152,7 +149,10 @@ module DiscourseSubscriptions
# lots of matching because the Stripe API doesn't make it easy to match products => payments except from invoices
until current_set[:has_more] == false
all_invoices =
- ::Stripe::Invoice.list(limit: 100, starting_after: current_set[:last_record])
+ ::Stripe::Invoice.list(
+ { limit: 100, starting_after: current_set[:last_record] },
+ stripe_request_opts,
+ )
if all_invoices[:data].present?
current_set[:last_record] = all_invoices[:data].last[:id]
@@ -192,9 +192,12 @@ module DiscourseSubscriptions
until current_set[:has_more] == false
current_set =
::Stripe::Subscription.list(
- expand: ["data.plan.product"],
- limit: 100,
- starting_after: current_set[:last_record],
+ {
+ expand: ["data.plan.product"],
+ limit: 100,
+ starting_after: current_set[:last_record],
+ },
+ stripe_request_opts,
)
current_set[:last_record] = current_set[:data].last[:id] if current_set[:data].present?
diff --git a/plugins/discourse-subscriptions/assets/javascripts/discourse/controllers/subscribe/show.js b/plugins/discourse-subscriptions/assets/javascripts/discourse/controllers/subscribe/show.js
index 92ab2a9632d..985a014c934 100644
--- a/plugins/discourse-subscriptions/assets/javascripts/discourse/controllers/subscribe/show.js
+++ b/plugins/discourse-subscriptions/assets/javascripts/discourse/controllers/subscribe/show.js
@@ -172,12 +172,10 @@ export default class SubscribeShowController extends Controller {
if (result.error) {
this.dialog.alert(result.error.message || result.error);
} else if (result.status === "incomplete" || result.status === "open") {
- const transactionId = result.id;
- const planId = this.selectedPlan;
this.handleAuthentication(plan, result).then(
(authenticationResult) => {
if (authenticationResult && !authenticationResult.error) {
- return Transaction.finalize(transactionId, planId).then(() => {
+ return Transaction.finalize().then(() => {
this._advanceSuccessfulTransaction(plan);
});
}
diff --git a/plugins/discourse-subscriptions/assets/javascripts/discourse/models/transaction.js b/plugins/discourse-subscriptions/assets/javascripts/discourse/models/transaction.js
index 08fe6a70d81..938ad9d44a0 100644
--- a/plugins/discourse-subscriptions/assets/javascripts/discourse/models/transaction.js
+++ b/plugins/discourse-subscriptions/assets/javascripts/discourse/models/transaction.js
@@ -1,12 +1,7 @@
import { ajax } from "discourse/lib/ajax";
export default {
- finalize(transaction, plan) {
- const data = {
- transaction,
- plan,
- };
-
- return ajax("/s/finalize", { method: "post", data });
+ finalize() {
+ return ajax("/s/finalize", { method: "post" });
},
};
diff --git a/plugins/discourse-subscriptions/spec/requests/admin/coupons_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/admin/coupons_controller_spec.rb
index 74c4982b74f..8644b6f7681 100644
--- a/plugins/discourse-subscriptions/spec/requests/admin/coupons_controller_spec.rb
+++ b/plugins/discourse-subscriptions/spec/requests/admin/coupons_controller_spec.rb
@@ -18,13 +18,16 @@ RSpec.describe DiscourseSubscriptions::Admin::CouponsController do
context "when authenticated" do
let(:admin) { Fabricate(:admin) }
- before { sign_in(admin) }
+ before do
+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"
+ sign_in(admin)
+ end
describe "#index" do
it "returns a list of promo codes" do
::Stripe::PromotionCode
.expects(:list)
- .with({ limit: 100 })
+ .with({ limit: 100 }, DiscourseSubscriptions::Stripe.request_opts)
.returns({ data: [{ id: "promo_123", coupon: { valid: true } }] })
get "/s/admin/coupons.json"
@@ -35,7 +38,7 @@ RSpec.describe DiscourseSubscriptions::Admin::CouponsController do
it "only returns valid promo codes" do
::Stripe::PromotionCode
.expects(:list)
- .with({ limit: 100 })
+ .with({ limit: 100 }, DiscourseSubscriptions::Stripe.request_opts)
.returns({ data: [{ id: "promo_123", coupon: { valid: false } }] })
get "/s/admin/coupons.json"
@@ -46,10 +49,14 @@ RSpec.describe DiscourseSubscriptions::Admin::CouponsController do
describe "#create" do
it "creates a coupon with an amount off" do
- ::Stripe::Coupon.expects(:create).returns(id: "coup_123")
- ::Stripe::PromotionCode.expects(:create).returns(
- { code: "p123", coupon: { amount_off: 2000 } },
- )
+ ::Stripe::Coupon
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "coup_123")
+ ::Stripe::PromotionCode
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns({ code: "p123", coupon: { amount_off: 2000 } })
post "/s/admin/coupons.json",
params: {
@@ -64,10 +71,14 @@ RSpec.describe DiscourseSubscriptions::Admin::CouponsController do
end
it "creates a coupon with a percent off" do
- ::Stripe::Coupon.expects(:create).returns(id: "coup_123")
- ::Stripe::PromotionCode.expects(:create).returns(
- { code: "p123", coupon: { percent_off: 20 } },
- )
+ ::Stripe::Coupon
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "coup_123")
+ ::Stripe::PromotionCode
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns({ code: "p123", coupon: { percent_off: 20 } })
post "/s/admin/coupons.json",
params: {
diff --git a/plugins/discourse-subscriptions/spec/requests/admin/plans_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/admin/plans_controller_spec.rb
index 384355f3b49..f8bbd3cd9f3 100644
--- a/plugins/discourse-subscriptions/spec/requests/admin/plans_controller_spec.rb
+++ b/plugins/discourse-subscriptions/spec/requests/admin/plans_controller_spec.rb
@@ -55,23 +55,32 @@ RSpec.describe DiscourseSubscriptions::Admin::PlansController do
context "when authenticated" do
let(:admin) { Fabricate(:admin) }
- before { sign_in(admin) }
+ before do
+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"
+ sign_in(admin)
+ end
describe "index" do
it "lists the plans" do
- ::Stripe::Price.expects(:list).with(nil)
+ ::Stripe::Price.expects(:list).with(nil, DiscourseSubscriptions::Stripe.request_opts)
get "/s/admin/plans.json"
end
it "lists the plans for the product" do
- ::Stripe::Price.expects(:list).with({ product: "prod_id123" })
+ ::Stripe::Price.expects(:list).with(
+ { product: "prod_id123" },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
get "/s/admin/plans.json", params: { product_id: "prod_id123" }
end
end
describe "show" do
it "shows a plan" do
- ::Stripe::Price.expects(:retrieve).with("plan_12345").returns(currency: "aud")
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with("plan_12345", DiscourseSubscriptions::Stripe.request_opts)
+ .returns(currency: "aud")
get "/s/admin/plans/plan_12345.json"
expect(response.status).to eq 200
end
@@ -79,7 +88,7 @@ RSpec.describe DiscourseSubscriptions::Admin::PlansController do
it "upcases the currency" do
::Stripe::Price
.expects(:retrieve)
- .with("plan_12345")
+ .with("plan_12345", DiscourseSubscriptions::Stripe.request_opts)
.returns(currency: "aud", recurring: { interval: "year" })
get "/s/admin/plans/plan_12345.json"
@@ -91,17 +100,26 @@ RSpec.describe DiscourseSubscriptions::Admin::PlansController do
describe "create" do
it "creates a plan with a nickname" do
- ::Stripe::Price.expects(:create).with(has_entry(:nickname, "Veg"))
+ ::Stripe::Price.expects(:create).with(
+ has_entry(:nickname, "Veg"),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
post "/s/admin/plans.json", params: { nickname: "Veg", metadata: { group_name: "" } }
end
it "creates a plan with a currency" do
- ::Stripe::Price.expects(:create).with(has_entry(:currency, "AUD"))
+ ::Stripe::Price.expects(:create).with(
+ has_entry(:currency, "AUD"),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
post "/s/admin/plans.json", params: { currency: "AUD", metadata: { group_name: "" } }
end
it "creates a plan with an interval" do
- ::Stripe::Price.expects(:create).with(has_entry(recurring: { interval: "week" }))
+ ::Stripe::Price.expects(:create).with(
+ has_entry(recurring: { interval: "week" }),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
post "/s/admin/plans.json",
params: {
type: "recurring",
@@ -113,17 +131,26 @@ RSpec.describe DiscourseSubscriptions::Admin::PlansController do
end
it "creates a plan as a one-time purchase" do
- ::Stripe::Price.expects(:create).with(Not(has_key(:recurring)))
+ ::Stripe::Price.expects(:create).with(
+ Not(has_key(:recurring)),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
post "/s/admin/plans.json", params: { metadata: { group_name: "" } }
end
it "creates a plan with an amount" do
- ::Stripe::Price.expects(:create).with(has_entry(:unit_amount, "102"))
+ ::Stripe::Price.expects(:create).with(
+ has_entry(:unit_amount, "102"),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
post "/s/admin/plans.json", params: { amount: "102", metadata: { group_name: "" } }
end
it "creates a plan with a product" do
- ::Stripe::Price.expects(:create).with(has_entry(product: "prod_walterwhite"))
+ ::Stripe::Price.expects(:create).with(
+ has_entry(product: "prod_walterwhite"),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
post "/s/admin/plans.json",
params: {
product: "prod_walterwhite",
@@ -134,7 +161,10 @@ RSpec.describe DiscourseSubscriptions::Admin::PlansController do
end
it "creates a plan with an active status" do
- ::Stripe::Price.expects(:create).with(has_entry(:active, "false"))
+ ::Stripe::Price.expects(:create).with(
+ has_entry(:active, "false"),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
post "/s/admin/plans.json", params: { active: "false", metadata: { group_name: "" } }
end
@@ -154,7 +184,11 @@ RSpec.describe DiscourseSubscriptions::Admin::PlansController do
describe "update" do
it "updates a plan" do
- ::Stripe::Price.expects(:update)
+ ::Stripe::Price.expects(:update).with(
+ "plan_12345",
+ anything,
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
patch "/s/admin/plans/plan_12345.json",
params: {
trial_period_days: "14",
diff --git a/plugins/discourse-subscriptions/spec/requests/admin/products_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/admin/products_controller_spec.rb
index e43e77275db..7b5ff86c1a6 100644
--- a/plugins/discourse-subscriptions/spec/requests/admin/products_controller_spec.rb
+++ b/plugins/discourse-subscriptions/spec/requests/admin/products_controller_spec.rb
@@ -42,12 +42,14 @@ RSpec.describe DiscourseSubscriptions::Admin::ProductsController do
context "when authenticated" do
let(:admin) { Fabricate(:admin) }
- before { sign_in(admin) }
+ before do
+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"
+ sign_in(admin)
+ end
describe "index" do
it "gets the empty products" do
SiteSetting.discourse_subscriptions_public_key = "public-key"
- SiteSetting.discourse_subscriptions_secret_key = "secret-key"
get "/s/admin/products.json"
expect(response.parsed_body).to be_empty
end
@@ -55,23 +57,33 @@ RSpec.describe DiscourseSubscriptions::Admin::ProductsController do
describe "create" do
it "is of product type service" do
- ::Stripe::Product.expects(:create).with(has_entry(:type, "service"))
+ ::Stripe::Product.expects(:create).with(
+ has_entry(:type, "service"),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
post "/s/admin/products.json", params: {}
end
it "has a name" do
- ::Stripe::Product.expects(:create).with(has_entry(:name, "Jesse Pinkman"))
+ ::Stripe::Product.expects(:create).with(
+ has_entry(:name, "Jesse Pinkman"),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
post "/s/admin/products.json", params: { name: "Jesse Pinkman" }
end
it "has an active attribute" do
- ::Stripe::Product.expects(:create).with(has_entry(active: "false"))
+ ::Stripe::Product.expects(:create).with(
+ has_entry(active: "false"),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
post "/s/admin/products.json", params: { active: "false" }
end
it "has a statement descriptor" do
::Stripe::Product.expects(:create).with(
has_entry(statement_descriptor: "Blessed are the cheesemakers"),
+ DiscourseSubscriptions::Stripe.request_opts,
)
post "/s/admin/products.json",
params: {
@@ -80,7 +92,10 @@ RSpec.describe DiscourseSubscriptions::Admin::ProductsController do
end
it "has no statement descriptor if empty" do
- ::Stripe::Product.expects(:create).with(has_key(:statement_descriptor)).never
+ ::Stripe::Product
+ .expects(:create)
+ .with(has_key(:statement_descriptor), DiscourseSubscriptions::Stripe.request_opts)
+ .never
post "/s/admin/products.json", params: { statement_descriptor: "" }
end
@@ -92,6 +107,7 @@ RSpec.describe DiscourseSubscriptions::Admin::ProductsController do
repurchaseable: "false",
},
),
+ DiscourseSubscriptions::Stripe.request_opts,
)
post "/s/admin/products.json",
@@ -106,21 +122,32 @@ RSpec.describe DiscourseSubscriptions::Admin::ProductsController do
describe "show" do
it "retrieves the product" do
- ::Stripe::Product.expects(:retrieve).with("prod_walterwhite")
+ ::Stripe::Product.expects(:retrieve).with(
+ "prod_walterwhite",
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
get "/s/admin/products/prod_walterwhite.json"
end
end
describe "update" do
it "updates the product" do
- ::Stripe::Product.expects(:update)
+ ::Stripe::Product.expects(:update).with(
+ "prod_walterwhite",
+ anything,
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
patch "/s/admin/products/prod_walterwhite.json", params: {}
end
end
describe "delete" do
it "deletes the product" do
- ::Stripe::Product.expects(:delete).with("prod_walterwhite")
+ ::Stripe::Product.expects(:delete).with(
+ "prod_walterwhite",
+ anything,
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
delete "/s/admin/products/prod_walterwhite.json"
end
end
diff --git a/plugins/discourse-subscriptions/spec/requests/admin/subscriptions_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/admin/subscriptions_controller_spec.rb
index d0be1ec9cb3..5021b88ba6a 100644
--- a/plugins/discourse-subscriptions/spec/requests/admin/subscriptions_controller_spec.rb
+++ b/plugins/discourse-subscriptions/spec/requests/admin/subscriptions_controller_spec.rb
@@ -35,18 +35,21 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
context "when authenticated" do
let(:admin) { Fabricate(:admin) }
- before { sign_in(admin) }
+ before do
+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"
+ sign_in(admin)
+ end
describe "index" do
- before do
- SiteSetting.discourse_subscriptions_public_key = "public-key"
- SiteSetting.discourse_subscriptions_secret_key = "secret-key"
- end
+ before { SiteSetting.discourse_subscriptions_public_key = "public-key" }
it "gets the subscriptions and products" do
::Stripe::Subscription
.expects(:list)
- .with(expand: ["data.plan.product"], limit: 10, starting_after: nil, status: "all")
+ .with(
+ { expand: ["data.plan.product"], limit: 10, starting_after: nil, status: "all" },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns(has_more: false, data: [{ id: "sub_12345" }, { id: "sub_nope" }])
get "/s/admin/subscriptions.json"
subscriptions = response.parsed_body["data"][0]["id"]
@@ -58,7 +61,10 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "handles starting at a different point in the set" do
::Stripe::Subscription
.expects(:list)
- .with(expand: ["data.plan.product"], limit: 10, starting_after: "sub_nope", status: "all")
+ .with(
+ { expand: ["data.plan.product"], limit: 10, starting_after: "sub_nope", status: "all" },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns(has_more: false, data: [{ id: "sub_77777" }, { id: "sub_yepnoep" }])
get "/s/admin/subscriptions.json", params: { last_record: "sub_nope" }
subscriptions = response.parsed_body["data"][0]["id"]
@@ -76,7 +82,7 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "deletes a customer" do
::Stripe::Subscription
.expects(:cancel)
- .with("sub_12345")
+ .with("sub_12345", anything, DiscourseSubscriptions::Stripe.request_opts)
.returns(plan: { product: "pr_34578" }, customer: "c_123")
# We don't want to delete the customer record. The webhook hook will update the status instead.
@@ -88,7 +94,7 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "removes the user from the group" do
::Stripe::Subscription
.expects(:cancel)
- .with("sub_12345")
+ .with("sub_12345", anything, DiscourseSubscriptions::Stripe.request_opts)
.returns(
plan: {
product: "pr_34578",
@@ -107,7 +113,7 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "does not remove the user from the group" do
::Stripe::Subscription
.expects(:cancel)
- .with("sub_12345")
+ .with("sub_12345", anything, DiscourseSubscriptions::Stripe.request_opts)
.returns(
plan: {
product: "pr_34578",
@@ -126,9 +132,12 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "does not refund when refund param is the string 'false'" do
::Stripe::Subscription
.expects(:cancel)
- .with("sub_12345")
+ .with("sub_12345", anything, DiscourseSubscriptions::Stripe.request_opts)
.returns(plan: { product: "pr_34578" }, customer: "c_123")
- ::Stripe::Subscription.expects(:retrieve).with("sub_12345").never
+ ::Stripe::Subscription
+ .expects(:retrieve)
+ .with("sub_12345", DiscourseSubscriptions::Stripe.request_opts)
+ .never
::Stripe::Refund.expects(:create).never
delete "/s/admin/subscriptions/sub_12345.json", params: { refund: "false" }
@@ -137,14 +146,20 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "refunds if params[:refund] present" do
::Stripe::Subscription
.expects(:cancel)
- .with("sub_12345")
+ .with("sub_12345", anything, DiscourseSubscriptions::Stripe.request_opts)
.returns(plan: { product: "pr_34578" }, customer: "c_123")
::Stripe::Subscription
.expects(:retrieve)
- .with("sub_12345")
+ .with("sub_12345", DiscourseSubscriptions::Stripe.request_opts)
.returns(latest_invoice: "in_123")
- ::Stripe::Invoice.expects(:retrieve).with("in_123").returns(payment_intent: "pi_123")
- ::Stripe::Refund.expects(:create).with({ payment_intent: "pi_123" })
+ ::Stripe::Invoice
+ .expects(:retrieve)
+ .with("in_123", DiscourseSubscriptions::Stripe.request_opts)
+ .returns(payment_intent: "pi_123")
+ ::Stripe::Refund.expects(:create).with(
+ { payment_intent: "pi_123" },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
delete "/s/admin/subscriptions/sub_12345.json", params: { refund: true }
end
diff --git a/plugins/discourse-subscriptions/spec/requests/hooks_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/hooks_controller_spec.rb
index da21da58e42..d48a74076c0 100644
--- a/plugins/discourse-subscriptions/spec/requests/hooks_controller_spec.rb
+++ b/plugins/discourse-subscriptions/spec/requests/hooks_controller_spec.rb
@@ -3,6 +3,7 @@
RSpec.describe DiscourseSubscriptions::HooksController do
before do
SiteSetting.discourse_subscriptions_webhook_secret = "zascharoo"
+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"
SiteSetting.discourse_subscriptions_enabled = true
end
@@ -156,7 +157,11 @@ RSpec.describe DiscourseSubscriptions::HooksController do
event = { type: "checkout.session.completed", data: checkout_session_completed_data }
::Stripe::Checkout::Session
.stubs(:list_line_items)
- .with(checkout_session_completed_data[:object][:id], { limit: 1 })
+ .with(
+ checkout_session_completed_data[:object][:id],
+ { limit: 1 },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns(list_line_items_data)
::Stripe::Subscription
@@ -164,6 +169,7 @@ RSpec.describe DiscourseSubscriptions::HooksController do
.with(
checkout_session_completed_data[:object][:subscription],
{ metadata: { user_id: user.id, username: user.username } },
+ DiscourseSubscriptions::Stripe.request_opts,
)
.returns(
{
@@ -198,11 +204,18 @@ RSpec.describe DiscourseSubscriptions::HooksController do
event = { type: "checkout.session.completed", data: checkout_session_completed_bad_data }
::Stripe::Checkout::Session
.stubs(:list_line_items)
- .with(checkout_session_completed_data[:object][:id], { limit: 1 })
+ .with(
+ checkout_session_completed_data[:object][:id],
+ { limit: 1 },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns(list_line_items_data)
::Stripe::Webhook.stubs(:construct_event).returns(event)
- ::Stripe::Customer.stubs(:create).returns(id: "cus_1234")
+ ::Stripe::Customer
+ .stubs(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "cus_1234")
end
it "is returns 422" do
@@ -219,11 +232,18 @@ RSpec.describe DiscourseSubscriptions::HooksController do
}
::Stripe::Checkout::Session
.stubs(:list_line_items)
- .with(checkout_session_completed_data[:object][:id], { limit: 1 })
+ .with(
+ checkout_session_completed_data[:object][:id],
+ { limit: 1 },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns(list_line_items_data)
::Stripe::Webhook.stubs(:construct_event).returns(event)
- ::Stripe::Customer.stubs(:create).returns(id: "cus_1234")
+ ::Stripe::Customer
+ .stubs(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "cus_1234")
end
it "is returns 200" do
@@ -239,11 +259,18 @@ RSpec.describe DiscourseSubscriptions::HooksController do
event = { type: "checkout.session.completed", data: data }
::Stripe::Checkout::Session
.stubs(:list_line_items)
- .with(checkout_session_completed_data[:object][:id], { limit: 1 })
+ .with(
+ checkout_session_completed_data[:object][:id],
+ { limit: 1 },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns(list_line_items_data)
::Stripe::Webhook.stubs(:construct_event).returns(event)
- ::Stripe::Customer.stubs(:create).returns(id: "cus_1234")
+ ::Stripe::Customer
+ .stubs(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "cus_1234")
end
it "is returns 422" do
@@ -259,7 +286,11 @@ RSpec.describe DiscourseSubscriptions::HooksController do
event = { type: "checkout.session.completed", data: data }
::Stripe::Checkout::Session
.stubs(:list_line_items)
- .with(checkout_session_completed_data[:object][:id], { limit: 1 })
+ .with(
+ checkout_session_completed_data[:object][:id],
+ { limit: 1 },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns(list_line_items_data)
::Stripe::Webhook.stubs(:construct_event).returns(event)
diff --git a/plugins/discourse-subscriptions/spec/requests/subscribe_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/subscribe_controller_spec.rb
index c3710a2107c..cbc814d2dfb 100644
--- a/plugins/discourse-subscriptions/spec/requests/subscribe_controller_spec.rb
+++ b/plugins/discourse-subscriptions/spec/requests/subscribe_controller_spec.rb
@@ -4,7 +4,10 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
let(:user) { Fabricate(:user) }
let(:campaign_user) { Fabricate(:user) }
- before { SiteSetting.discourse_subscriptions_enabled = true }
+ before do
+ SiteSetting.discourse_subscriptions_enabled = true
+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"
+ end
context "when showing products" do
let(:product) do
@@ -63,7 +66,6 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
sign_in(user)
Fabricate(:product, external_id: "prodct_23456")
SiteSetting.discourse_subscriptions_public_key = "public-key"
- SiteSetting.discourse_subscriptions_secret_key = "secret-key"
end
describe "#index" do
@@ -74,7 +76,7 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
it "gets products" do
::Stripe::Product
.expects(:list)
- .with({ ids: product_ids, active: true })
+ .with({ ids: product_ids, active: true }, DiscourseSubscriptions::Stripe.request_opts)
.returns(data: [product])
get "/s.json"
@@ -100,7 +102,7 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
::Stripe::Product
.expects(:list)
- .with({ ids: product_ids, active: true })
+ .with({ ids: product_ids, active: true }, DiscourseSubscriptions::Stripe.request_opts)
.returns(data: [product])
get "/s.json"
@@ -112,7 +114,7 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
DiscourseSubscriptions::Customer.delete_all
::Stripe::Product
.expects(:list)
- .with({ ids: product_ids, active: true })
+ .with({ ids: product_ids, active: true }, DiscourseSubscriptions::Stripe.request_opts)
.returns(data: [product])
get "/s.json"
@@ -168,10 +170,16 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
describe "#show" do
it "retrieves the product" do
- ::Stripe::Product.expects(:retrieve).with("prod_walterwhite").returns(product)
+ ::Stripe::Product
+ .expects(:retrieve)
+ .with("prod_walterwhite", DiscourseSubscriptions::Stripe.request_opts)
+ .returns(product)
::Stripe::Price
.expects(:list)
- .with(active: true, product: "prod_walterwhite")
+ .with(
+ { active: true, product: "prod_walterwhite" },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns(prices)
get "/s/prod_walterwhite.json"
@@ -233,18 +241,26 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
before { sign_in(user) }
describe "#create" do
- before { ::Stripe::Customer.expects(:create).returns(id: "cus_1234") }
+ before do
+ ::Stripe::Customer
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "cus_1234")
+ end
it "creates a subscription without automatic_tax param" do
SiteSetting.discourse_subscriptions_enable_automatic_tax = false
- ::Stripe::Price.expects(:retrieve).returns(
- type: "recurring",
- product: "product_12345",
- metadata: {
- group_name: "awesome",
- trial_period_days: 0,
- },
- )
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(
+ type: "recurring",
+ product: "product_12345",
+ metadata: {
+ group_name: "awesome",
+ trial_period_days: 0,
+ },
+ )
expected_subscription_params = {
customer: "cus_1234",
@@ -259,8 +275,9 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
::Stripe::Subscription
.expects(:create)
- .with do |params|
- params == expected_subscription_params && !params.key?(:automatic_tax)
+ .with do |params, opts|
+ params == expected_subscription_params && !params.key?(:automatic_tax) &&
+ opts == DiscourseSubscriptions::Stripe.request_opts
end
.returns(status: "active", customer: "cus_1234")
@@ -271,14 +288,17 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
it "creates a subscription with automatic tax" do
SiteSetting.discourse_subscriptions_enable_automatic_tax = true
- ::Stripe::Price.expects(:retrieve).returns(
- type: "recurring",
- product: "product_12345",
- metadata: {
- group_name: "awesome",
- trial_period_days: 0,
- },
- )
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(
+ type: "recurring",
+ product: "product_12345",
+ metadata: {
+ group_name: "awesome",
+ trial_period_days: 0,
+ },
+ )
expected_subscription_params = {
customer: "cus_1234",
@@ -296,7 +316,7 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
::Stripe::Subscription
.expects(:create)
- .with(expected_subscription_params)
+ .with(expected_subscription_params, DiscourseSubscriptions::Stripe.request_opts)
.returns(status: "active", customer: "cus_1234")
expect {
@@ -308,23 +328,31 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
# It's possible that the invoice item doesn't get attached
# to the invoice. This means the invoice is paid, but for $0.00 with
# a pending invoice item.
- ::Stripe::Price.expects(:retrieve).returns(
- type: "one_time",
- product: "product_12345",
- metadata: {
- 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::InvoiceItem.expects(:create)
+ ::Stripe::InvoiceItem.expects(:create).with(
+ anything,
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
- ::Stripe::Invoice.expects(:create).returns(status: "open", id: "in_123")
+ ::Stripe::Invoice
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "open", id: "in_123")
- ::Stripe::Invoice.expects(:finalize_invoice).returns(
- id: "in_123",
- status: "paid",
- payment_intent: "pi_123",
- )
+ ::Stripe::Invoice
+ .expects(:finalize_invoice)
+ .with(anything, anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "in_123", status: "paid", payment_intent: "pi_123")
expect {
post "/s/create.json", params: { plan: "plan_1234", source: "tok_1234" }
@@ -335,38 +363,51 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
it "creates a one time payment subscription without automatic tax" do
SiteSetting.discourse_subscriptions_enable_automatic_tax = false
- ::Stripe::Price.expects(:retrieve).returns(
- type: "one_time",
- product: "product_12345",
- metadata: {
- 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::InvoiceItem.expects(:create)
+ ::Stripe::InvoiceItem.expects(:create).with(
+ anything,
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
expected_one_time_params = { customer: "cus_1234" }
::Stripe::Invoice
.expects(:create)
- .with { |params| params == expected_one_time_params && !params.key?(:automatic_tax) }
+ .with do |params, opts|
+ params == expected_one_time_params && !params.key?(:automatic_tax) &&
+ opts == DiscourseSubscriptions::Stripe.request_opts
+ end
.returns(status: "open", id: "in_123")
- ::Stripe::Invoice.expects(:finalize_invoice).returns(
- id: "in_123",
- status: "open",
- payment_intent: "pi_123",
- )
+ ::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).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).returns(status: "successful")
+ ::Stripe::PaymentIntent
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "successful")
- ::Stripe::Invoice.expects(:pay).returns(status: "paid", customer: "cus_1234")
+ ::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" }
@@ -375,38 +416,48 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
it "creates a one time payment subscription" do
SiteSetting.discourse_subscriptions_enable_automatic_tax = true
- ::Stripe::Price.expects(:retrieve).returns(
- type: "one_time",
- product: "product_12345",
- metadata: {
- 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::InvoiceItem.expects(:create)
+ ::Stripe::InvoiceItem.expects(:create).with(
+ anything,
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
expected_one_time_params = { customer: "cus_1234", automatic_tax: { enabled: true } }
::Stripe::Invoice
.expects(:create)
- .with(expected_one_time_params)
+ .with(expected_one_time_params, DiscourseSubscriptions::Stripe.request_opts)
.returns(status: "open", id: "in_123")
- ::Stripe::Invoice.expects(:finalize_invoice).returns(
- id: "in_123",
- status: "open",
- payment_intent: "pi_123",
- )
+ ::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).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).returns(status: "successful")
+ ::Stripe::PaymentIntent
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "successful")
- ::Stripe::Invoice.expects(:pay).returns(status: "paid", customer: "cus_1234")
+ ::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" }
@@ -414,14 +465,24 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
end
it "creates a customer model" do
- ::Stripe::Price.expects(:retrieve).returns(type: "recurring", metadata: {}).twice
- ::Stripe::Subscription.expects(:create).returns(status: "active", customer: "cus_1234")
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(type: "recurring", metadata: {})
+ .twice
+ ::Stripe::Subscription
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "active", customer: "cus_1234")
expect {
post "/s/create.json", params: { plan: "plan_1234", source: "tok_1234" }
}.to change { DiscourseSubscriptions::Customer.count }
- ::Stripe::Customer.expects(:retrieve).with("cus_1234")
+ ::Stripe::Customer.expects(:retrieve).with(
+ "cus_1234",
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
expect {
post "/s/create.json", params: { plan: "plan_5678", source: "tok_5678" }
@@ -430,8 +491,14 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
context "with customer name & address" do
it "creates a customer & subscription when a customer address is provided" do
- ::Stripe::Price.expects(:retrieve).returns(type: "recurring", metadata: {})
- ::Stripe::Subscription.expects(:create).returns(status: "active", customer: "cus_1234")
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(type: "recurring", metadata: {})
+ ::Stripe::Subscription
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "active", customer: "cus_1234")
expect {
post "/s/create.json",
params: {
@@ -453,16 +520,22 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
context "with promo code" do
context "with invalid code" do
it "prevents use of invalid coupon codes" do
- ::Stripe::Price.expects(:retrieve).returns(
- type: "recurring",
- product: "product_12345",
- metadata: {
- group_name: "awesome",
- trial_period_days: 0,
- },
- )
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(
+ type: "recurring",
+ product: "product_12345",
+ metadata: {
+ group_name: "awesome",
+ trial_period_days: 0,
+ },
+ )
- ::Stripe::PromotionCode.expects(:list).with({ code: "invalid" }).returns(data: [])
+ ::Stripe::PromotionCode
+ .expects(:list)
+ .with({ code: "invalid" }, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [])
post "/s/create.json",
params: {
@@ -480,19 +553,22 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
before do
::Stripe::PromotionCode
.expects(:list)
- .with({ code: "123" })
+ .with({ code: "123" }, DiscourseSubscriptions::Stripe.request_opts)
.returns(data: [{ id: "promo123", coupon: { id: "c123" } }])
end
it "applies promo code to recurring subscription" do
- ::Stripe::Price.expects(:retrieve).returns(
- type: "recurring",
- product: "product_12345",
- metadata: {
- group_name: "awesome",
- trial_period_days: 0,
- },
- )
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(
+ type: "recurring",
+ product: "product_12345",
+ metadata: {
+ group_name: "awesome",
+ trial_period_days: 0,
+ },
+ )
expected_subscription_params = {
customer: "cus_1234",
@@ -507,7 +583,7 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
::Stripe::Subscription
.expects(:create)
- .with(expected_subscription_params)
+ .with(expected_subscription_params, DiscourseSubscriptions::Stripe.request_opts)
.returns(status: "active", customer: "cus_1234")
expect {
@@ -521,38 +597,51 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
end
it "applies promo code to one time purchase" do
- ::Stripe::Price.expects(:retrieve).returns(
- type: "one_time",
- product: "product_12345",
- metadata: {
- group_name: "awesome",
- },
- )
-
- ::Stripe::Invoice.expects(:create).returns(status: "open", id: "in_123")
+ ::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: [{ coupon: "c123" }],
- invoice: "in_123",
+ {
+ customer: "cus_1234",
+ price: "plan_1234",
+ discounts: [{ coupon: "c123" }],
+ invoice: "in_123",
+ },
+ DiscourseSubscriptions::Stripe.request_opts,
)
- ::Stripe::Invoice.expects(:finalize_invoice).returns(
- id: "in_123",
- status: "open",
- payment_intent: "pi_123",
- )
+ ::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).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).returns(status: "successful")
+ ::Stripe::PaymentIntent
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "successful")
- ::Stripe::Invoice.expects(:pay).returns(status: "paid", customer: "cus_1234")
+ ::Stripe::Invoice
+ .expects(:pay)
+ .with(anything, anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "paid", customer: "cus_1234")
expect {
post "/s/create.json",
@@ -570,43 +659,67 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
describe "#finalize strong customer authenticated transaction" do
context "with subscription" do
it "finalizes the subscription" do
- ::Stripe::Price.expects(:retrieve).returns(
- id: "plan_1234",
- product: "prod_1234",
- metadata: {
- },
- )
- ::Stripe::Subscription.expects(:retrieve).returns(
- id: "sub_123",
- customer: "cus_1234",
- status: "active",
- )
-
- expect {
- post "/s/finalize.json", params: { plan: "plan_1234", transaction: "sub_1234" }
- }.to change { DiscourseSubscriptions::Customer.count }
+ server_session["pending_subscription"] = {
+ transaction_id: "sub_1234",
+ plan_id: "plan_1234",
+ }
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "plan_1234", product: "prod_1234", metadata: {})
+ ::Stripe::Subscription
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "sub_123", customer: "cus_1234", status: "active")
+
+ expect { post "/s/finalize.json" }.to change { DiscourseSubscriptions::Customer.count }
end
end
context "with one-time payment" do
it "finalizes the one-time payment" do
- ::Stripe::Price.expects(:retrieve).returns(
- id: "plan_1234",
- product: "prod_1234",
- metadata: {
- },
- )
- ::Stripe::Invoice.expects(:retrieve).returns(
- id: "in_123",
- customer: "cus_1234",
- status: "paid",
- )
-
- expect {
- post "/s/finalize.json", params: { plan: "plan_1234", transaction: "in_1234" }
- }.to change { DiscourseSubscriptions::Customer.count }
+ server_session["pending_subscription"] = {
+ transaction_id: "in_1234",
+ plan_id: "plan_1234",
+ }
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "plan_1234", product: "prod_1234", metadata: {})
+ ::Stripe::Invoice
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "in_123", customer: "cus_1234", status: "paid")
+
+ expect { post "/s/finalize.json" }.to change { DiscourseSubscriptions::Customer.count }
end
end
+
+ it "returns 403 with no pending server session" do
+ post "/s/finalize.json"
+ expect(response.status).to eq(403)
+ end
+
+ it "prevents replay by rejecting a second finalize" do
+ server_session["pending_subscription"] = {
+ transaction_id: "sub_123",
+ plan_id: "plan_1234",
+ }
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "plan_1234", product: "prod_1234", metadata: {})
+ ::Stripe::Subscription
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "sub_123", customer: "cus_1234", status: "active")
+
+ post "/s/finalize.json"
+ expect(response.status).to eq(200)
+
+ post "/s/finalize.json"
+ expect(response.status).to eq(403)
+ end
end
describe "user groups" do
@@ -615,28 +728,30 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
context "with unauthorized group" do
before do
- ::Stripe::Customer.expects(:create).returns(id: "cus_1234")
- ::Stripe::Subscription.expects(:create).returns(status: "active")
+ ::Stripe::Customer
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "cus_1234")
+ ::Stripe::Subscription
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "active")
end
it "does not add the user to the admins group" do
- ::Stripe::Price.expects(:retrieve).returns(
- type: "recurring",
- metadata: {
- group_name: "admins",
- },
- )
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(type: "recurring", metadata: { group_name: "admins" })
post "/s/create.json", params: { plan: "plan_1234", source: "tok_1234" }
expect(user.admin).to eq false
end
it "does not add the user to other group" do
- ::Stripe::Price.expects(:retrieve).returns(
- type: "recurring",
- metadata: {
- group_name: "other",
- },
- )
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(type: "recurring", metadata: { group_name: "other" })
post "/s/create.json", params: { plan: "plan_1234", source: "tok_1234" }
expect(user.groups).to be_empty
end
@@ -644,17 +759,21 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
context "when plan has group in metadata" do
before do
- ::Stripe::Customer.expects(:create).returns(id: "cus_1234")
- ::Stripe::Price.expects(:retrieve).returns(
- type: "recurring",
- metadata: {
- group_name: group_name,
- },
- )
+ ::Stripe::Customer
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "cus_1234")
+ ::Stripe::Price
+ .expects(:retrieve)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(type: "recurring", metadata: { group_name: group_name })
end
it "does not add the user to the group when subscription fails" do
- ::Stripe::Subscription.expects(:create).returns(status: "failed")
+ ::Stripe::Subscription
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "failed")
expect {
post "/s/create.json", params: { plan: "plan_1234", source: "tok_1234" }
@@ -664,7 +783,10 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
end
it "adds the user to the group when the subscription is active" do
- ::Stripe::Subscription.expects(:create).returns(status: "active")
+ ::Stripe::Subscription
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "active")
expect {
post "/s/create.json", params: { plan: "plan_1234", source: "tok_1234" }
@@ -674,7 +796,10 @@ RSpec.describe DiscourseSubscriptions::SubscribeController do
end
it "adds the user to the group when the subscription is trialing" do
- ::Stripe::Subscription.expects(:create).returns(status: "trialing")
+ ::Stripe::Subscription
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(status: "trialing")
expect {
post "/s/create.json", params: { plan: "plan_1234", source: "tok_1234" }
diff --git a/plugins/discourse-subscriptions/spec/requests/user/payments_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/user/payments_controller_spec.rb
index 09851a1ab8d..12c6e39d446 100644
--- a/plugins/discourse-subscriptions/spec/requests/user/payments_controller_spec.rb
+++ b/plugins/discourse-subscriptions/spec/requests/user/payments_controller_spec.rb
@@ -1,7 +1,10 @@
# frozen_string_literal: true
RSpec.describe DiscourseSubscriptions::User::PaymentsController do
- before { SiteSetting.discourse_subscriptions_enabled = true }
+ before do
+ SiteSetting.discourse_subscriptions_enabled = true
+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"
+ end
it "is a subclass of ApplicationController" do
expect(DiscourseSubscriptions::User::PaymentsController < ::ApplicationController).to eq(true)
@@ -29,7 +32,7 @@ RSpec.describe DiscourseSubscriptions::User::PaymentsController do
created_time = Time.now
::Stripe::Invoice
.expects(:list)
- .with(customer: "c_345678")
+ .with({ customer: "c_345678" }, DiscourseSubscriptions::Stripe.request_opts)
.returns(
data: [
{ id: "inv_900007", lines: { data: [plan: { product: "prod_8675309" }] } },
@@ -40,7 +43,7 @@ RSpec.describe DiscourseSubscriptions::User::PaymentsController do
::Stripe::PaymentIntent
.expects(:list)
- .with(customer: "c_345678")
+ .with({ customer: "c_345678" }, DiscourseSubscriptions::Stripe.request_opts)
.returns(
data: [
{ id: "pi_900008", invoice: "inv_900008", created: created_time },
@@ -60,11 +63,14 @@ RSpec.describe DiscourseSubscriptions::User::PaymentsController do
end
it "gets pricing table one-off purchases" do
- ::Stripe::Invoice.expects(:list).with(customer: "c_345678").returns(data: [])
+ ::Stripe::Invoice
+ .expects(:list)
+ .with({ customer: "c_345678" }, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [])
::Stripe::PaymentIntent
.expects(:list)
- .with(customer: "c_345678")
+ .with({ customer: "c_345678" }, DiscourseSubscriptions::Stripe.request_opts)
.returns(data: [{ id: "pi_900010", invoice: nil, created: Time.now }])
get "/s/user/payments.json"
@@ -76,13 +82,22 @@ RSpec.describe DiscourseSubscriptions::User::PaymentsController do
it "gets pricing table one-off purchases that show up as related guest payments" do
SiteSetting.discourse_subscriptions_pricing_table_enabled = true
- ::Stripe::Invoice.expects(:list).with(customer: "c_345678").returns(data: [])
+ ::Stripe::Invoice
+ .expects(:list)
+ .with({ customer: "c_345678" }, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [])
- ::Stripe::PaymentIntent.expects(:list).with(customer: "c_345678").returns(data: [])
+ ::Stripe::PaymentIntent
+ .expects(:list)
+ .with({ customer: "c_345678" }, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [])
::Stripe::Charge
.expects(:list)
- .with(limit: 100, starting_after: nil, expand: ["data.payment_intent"])
+ .with(
+ { limit: 100, starting_after: nil, expand: ["data.payment_intent"] },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns(
data: [
{
diff --git a/plugins/discourse-subscriptions/spec/requests/user/subscriptions_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/user/subscriptions_controller_spec.rb
index dbbcdba1633..445cd2645e6 100644
--- a/plugins/discourse-subscriptions/spec/requests/user/subscriptions_controller_spec.rb
+++ b/plugins/discourse-subscriptions/spec/requests/user/subscriptions_controller_spec.rb
@@ -1,7 +1,10 @@
# frozen_string_literal: true
RSpec.describe DiscourseSubscriptions::User::SubscriptionsController do
- before { SiteSetting.discourse_subscriptions_enabled = true }
+ before do
+ SiteSetting.discourse_subscriptions_enabled = true
+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"
+ end
def create_price(id, product)
price = { id: id, product: product }
@@ -60,7 +63,10 @@ RSpec.describe DiscourseSubscriptions::User::SubscriptionsController do
)
it "gets subscriptions" do
- ::Stripe::Price.stubs(:list).returns(JSON.parse(plans_json, symbolize_names: true))
+ ::Stripe::Price
+ .stubs(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(JSON.parse(plans_json, symbolize_names: true))
subscriptions_json =
File.read(
@@ -74,9 +80,10 @@ RSpec.describe DiscourseSubscriptions::User::SubscriptionsController do
),
)
- ::Stripe::Subscription.stubs(:list).returns(
- JSON.parse(subscriptions_json, symbolize_names: true),
- )
+ ::Stripe::Subscription
+ .stubs(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(JSON.parse(subscriptions_json, symbolize_names: true))
get "/s/user/subscriptions.json"
@@ -91,7 +98,10 @@ RSpec.describe DiscourseSubscriptions::User::SubscriptionsController do
subscription_data = { id: "sub_10z", items: { data: [{ price: { id: "price_200" } }] } }
::Stripe::Subscription
.stubs(:list)
- .with(customer: "cus_23456", status: "all")
+ .with(
+ { customer: "cus_23456", status: "all" },
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns({ data: [subscription_data] })
# Build the first page of 100 prices that do NOT include the desired price.
@@ -105,12 +115,18 @@ RSpec.describe DiscourseSubscriptions::User::SubscriptionsController do
::Stripe::Price
.expects(:list)
- .with(has_entries(limit: 100, expand: ["data.product"]))
+ .with(
+ has_entries(limit: 100, expand: ["data.product"]),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns({ data: prices_page_1, has_more: true })
::Stripe::Price
.expects(:list)
- .with(has_entries(limit: 100, expand: ["data.product"], starting_after: "price_100"))
+ .with(
+ has_entries(limit: 100, expand: ["data.product"], starting_after: "price_100"),
+ DiscourseSubscriptions::Stripe.request_opts,
+ )
.returns({ data: prices_page_2, has_more: false })
get "/s/user/subscriptions.json"
@@ -126,8 +142,14 @@ RSpec.describe DiscourseSubscriptions::User::SubscriptionsController do
describe "update" do
it "updates the payment method for subscription" do
- ::Stripe::Subscription.expects(:update).once
- ::Stripe::PaymentMethod.expects(:attach).once
+ ::Stripe::Subscription
+ .expects(:update)
+ .with(anything, anything, DiscourseSubscriptions::Stripe.request_opts)
+ .once
+ ::Stripe::PaymentMethod
+ .expects(:attach)
+ .with(anything, anything, DiscourseSubscriptions::Stripe.request_opts)
+ .once
put "/s/user/subscriptions/sub_10z.json", params: { payment_method: "pm_abc123abc" }
end
end
diff --git a/plugins/discourse-subscriptions/spec/services/campaign_spec.rb b/plugins/discourse-subscriptions/spec/services/campaign_spec.rb
index 33534a3bec6..17d2ec6c5a3 100644
--- a/plugins/discourse-subscriptions/spec/services/campaign_spec.rb
+++ b/plugins/discourse-subscriptions/spec/services/campaign_spec.rb
@@ -79,8 +79,14 @@ describe DiscourseSubscriptions::Campaign do
describe "refresh_data" do
context "for all subscription purchases" do
it "refreshes the campaign data properly" do
- ::Stripe::Subscription.expects(:list).returns(data: [subscription], has_more: false)
- ::Stripe::Invoice.expects(:list).returns(data: [invoice, invoice2], has_more: false)
+ ::Stripe::Subscription
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [subscription], has_more: false)
+ ::Stripe::Invoice
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [invoice, invoice2], has_more: false)
DiscourseSubscriptions::Campaign.new.refresh_data
@@ -90,8 +96,14 @@ describe DiscourseSubscriptions::Campaign do
it "checks if the goal is completed or not" do
SiteSetting.discourse_subscriptions_campaign_goal = 5
- ::Stripe::Subscription.expects(:list).returns(data: [subscription], has_more: false)
- ::Stripe::Invoice.expects(:list).returns(data: [invoice], has_more: false)
+ ::Stripe::Subscription
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [subscription], has_more: false)
+ ::Stripe::Invoice
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [invoice], has_more: false)
DiscourseSubscriptions::Campaign.new.refresh_data
expect(Discourse.redis.get("subscriptions_goal_met_date")).to be_present
@@ -100,23 +112,41 @@ describe DiscourseSubscriptions::Campaign do
it "checks if goal is < 90% met after being met" do
SiteSetting.discourse_subscriptions_campaign_goal = 25
Discourse.redis.set("subscriptions_goal_met_date", 10.days.ago)
- ::Stripe::Subscription.expects(:list).returns(data: [subscription], has_more: false)
- ::Stripe::Invoice.expects(:list).returns(data: [invoice], has_more: false)
+ ::Stripe::Subscription
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [subscription], has_more: false)
+ ::Stripe::Invoice
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [invoice], has_more: false)
DiscourseSubscriptions::Campaign.new.refresh_data
expect(Discourse.redis.get("subscriptions_goal_met_date")).to be_blank
end
it "handles invoices with nil lines gracefully" do
- ::Stripe::Subscription.expects(:list).returns(data: [subscription], has_more: false)
- ::Stripe::Invoice.expects(:list).returns(data: [invoice_with_nil_lines], has_more: false)
+ ::Stripe::Subscription
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [subscription], has_more: false)
+ ::Stripe::Invoice
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [invoice_with_nil_lines], has_more: false)
expect { DiscourseSubscriptions::Campaign.new.refresh_data }.not_to raise_error
end
it "handles invoices with nil price gracefully" do
- ::Stripe::Subscription.expects(:list).returns(data: [subscription], has_more: false)
- ::Stripe::Invoice.expects(:list).returns(data: [invoice_with_nil_price], has_more: false)
+ ::Stripe::Subscription
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [subscription], has_more: false)
+ ::Stripe::Invoice
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [invoice_with_nil_price], has_more: false)
expect { DiscourseSubscriptions::Campaign.new.refresh_data }.not_to raise_error
end
@@ -150,11 +180,14 @@ describe DiscourseSubscriptions::Campaign do
end
it "refreshes campaign data with only the campaign product/subscriptions" do
- ::Stripe::Subscription.expects(:list).returns(
- data: [subscription, campaign_subscription],
- has_more: false,
- )
- ::Stripe::Invoice.expects(:list).returns(data: [invoice], has_more: false)
+ ::Stripe::Subscription
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [subscription, campaign_subscription], has_more: false)
+ ::Stripe::Invoice
+ .expects(:list)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(data: [invoice], has_more: false)
DiscourseSubscriptions::Campaign.new.refresh_data
@@ -166,15 +199,18 @@ describe DiscourseSubscriptions::Campaign do
end
describe "campaign is automatically created" do
+ before { SiteSetting.discourse_subscriptions_secret_key = "secret-key" }
+
describe "create_campaign" do
it "successfully creates the campaign group, product, and prices" do
- ::Stripe::Product.expects(:create).returns(id: "prod_campaign")
- ::Stripe::Price.expects(:create)
- ::Stripe::Price.expects(:create)
- ::Stripe::Price.expects(:create)
- ::Stripe::Price.expects(:create)
- ::Stripe::Price.expects(:create)
- ::Stripe::Price.expects(:create)
+ ::Stripe::Product
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .returns(id: "prod_campaign")
+ ::Stripe::Price
+ .expects(:create)
+ .with(anything, DiscourseSubscriptions::Stripe.request_opts)
+ .times(6)
DiscourseSubscriptions::Campaign.new.create_campaign