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.rbindex 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 DiscourseSubscriptionsmodule Stripeextend 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_optsenddef 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.rbindex 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 DiscourseSubscriptionsrequires_plugin PLUGIN_NAME- before_action :set_api_key-def indexbegin- 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_codesrescue ::Stripe::InvalidRequestError => e@@ -33,11 +31,12 @@ module DiscourseSubscriptionscoupon_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 DiscourseSubscriptionsdef updateparams.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_coderescue ::Stripe::InvalidRequestError => e@@ -60,7 +64,7 @@ module DiscourseSubscriptionsdef destroyparams.require(:coupon_id)begin- coupon = ::Stripe::Coupon.delete(params[:coupon_id])+ coupon = ::Stripe::Coupon.delete(params[:coupon_id], {}, stripe_request_opts)render_json_dump couponrescue ::Stripe::InvalidRequestError => erender_json_error e.messagediff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/plans_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/plans_controller.rbindex 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 DiscourseSubscriptionsrequires_plugin PLUGIN_NAME- before_action :set_api_key-def indexbegin- plans = ::Stripe::Price.list(product_params)+ plans = ::Stripe::Price.list(product_params, stripe_request_opts)render_json_dump plans.datarescue ::Stripe::InvalidRequestError => e@@ -35,7 +33,7 @@ module DiscourseSubscriptionsprice_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 planrescue ::Stripe::InvalidRequestError => e@@ -45,7 +43,7 @@ module DiscourseSubscriptionsdef showbegin- 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 DiscourseSubscriptionsplan =::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 plandiff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/products_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/admin/products_controller.rbindex 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 DiscourseSubscriptionsrequires_plugin PLUGIN_NAME- before_action :set_api_key-def indexbeginproduct_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 DiscourseSubscriptionscreate_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 DiscourseSubscriptionsdef showbegin- product = ::Stripe::Product.retrieve(params[:id])+ product = ::Stripe::Product.retrieve(params[:id], stripe_request_opts)render_json_dump productrescue ::Stripe::InvalidRequestError => e@@ -55,7 +53,7 @@ module DiscourseSubscriptionsdef updatebegin- product = ::Stripe::Product.update(params[:id], product_params)+ product = ::Stripe::Product.update(params[:id], product_params, stripe_request_opts)render_json_dump productrescue ::Stripe::InvalidRequestError => e@@ -65,7 +63,7 @@ module DiscourseSubscriptionsdef destroybegin- 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.rbindex 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 DiscourseSubscriptionsinclude DiscourseSubscriptions::Stripeinclude DiscourseSubscriptions::Group- before_action :set_api_keyPAGE_LIMIT = 10@@ -53,7 +52,7 @@ module DiscourseSubscriptionsparams.require(:id)beginrefund_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 DiscourseSubscriptionsdef 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 paymentdef 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)endendenddiff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/hooks_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/hooks_controller.rbindex 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 DiscourseSubscriptionslayout false- before_action :set_api_keyskip_before_action :check_xhrskip_before_action :redirect_to_login_if_requiredskip_before_action :verify_authenticity_token, only: [:create]@@ -42,7 +41,7 @@ module DiscourseSubscriptionsreturn render_json_error "email not found" if !emailif checkout_session[:customer].nil?- customer = ::Stripe::Customer.create({ email: email })+ customer = ::Stripe::Customer.create({ email: email }, stripe_request_opts)customer_id = customer[:id]elsecustomer_id = checkout_session[:customer]@@ -65,7 +64,11 @@ module DiscourseSubscriptionsendline_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].firstgroup = 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,)endwhen "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.rbindex 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 DiscourseSubscriptionsrequires_plugin PLUGIN_NAME- before_action :set_api_keyrequires_login except: %i[index contributors show]def index@@ -16,7 +15,7 @@ module DiscourseSubscriptionsproducts = []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 DiscourseSubscriptionsdef showparams.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 DiscourseSubscriptionsparams[: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 nameif promo_code.blank?@@ -97,7 +96,7 @@ module DiscourseSubscriptionssubscription_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 DiscourseSubscriptionsif SiteSetting.discourse_subscriptions_enable_automatic_taxinvoice_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 DiscourseSubscriptionsrender_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],+ }+ endtransaction = transaction.to_h.merge(transaction, payment_intent: payment_intent)@@ -142,13 +153,19 @@ module DiscourseSubscriptionsenddef 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 => erender_json_error e.messageend@@ -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,)endenddef 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)enddef retrieve_transaction(transaction)begincase transactionwhen /^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)endrescue ::Stripe::InvalidRequestError => ee.messagediff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/payments_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/payments_controller.rbindex 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 DiscourseSubscriptionsrequires_plugin PLUGIN_NAME- before_action :set_api_keyrequires_logindef index@@ -21,10 +20,11 @@ module DiscourseSubscriptionsif 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_aftercharges =::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.rbindex 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 DiscourseSubscriptionsrequires_plugin PLUGIN_NAME- before_action :set_api_keybefore_action :find_subscription, only: %i[update destroy]requires_login@@ -28,7 +27,7 @@ module DiscourseSubscriptionsprices = []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 DiscourseSubscriptionsstripe_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 webhooksbegin- 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 subscriptionrender_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_jsonrescue ::Stripe::InvalidRequestError@@ -90,7 +98,11 @@ module DiscourseSubscriptionsdef 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,+ )enddef find_subscriptiondiff --git a/plugins/discourse-subscriptions/app/services/discourse_subscriptions/campaign.rb b/plugins/discourse-subscriptions/app/services/discourse_subscriptions/campaign.rbindex 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 DiscourseSubscriptionsclass Campaigninclude DiscourseSubscriptions::Stripe- def initialize- set_api_key # instantiates Stripe API- enddef refresh_dataproduct_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)enddef 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 invoicesuntil current_set[:has_more] == falseall_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 DiscourseSubscriptionsuntil current_set[:has_more] == falsecurrent_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.jsindex 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.jsindex 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.rbindex 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 docontext "when authenticated" dolet(:admin) { Fabricate(:admin) }- before { sign_in(admin) }+ before do+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"+ sign_in(admin)+ enddescribe "#index" doit "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 doit "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 dodescribe "#create" doit "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 doendit "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.rbindex 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 docontext "when authenticated" dolet(:admin) { Fabricate(:admin) }- before { sign_in(admin) }+ before do+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"+ sign_in(admin)+ enddescribe "index" doit "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"endit "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" }endenddescribe "show" doit "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 200end@@ -79,7 +88,7 @@ RSpec.describe DiscourseSubscriptions::Admin::PlansController doit "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 dodescribe "create" doit "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: "" } }endit "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: "" } }endit "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 doendit "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: "" } }endit "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: "" } }endit "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 doendit "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 dodescribe "update" doit "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.rbindex 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 docontext "when authenticated" dolet(:admin) { Fabricate(:admin) }- before { sign_in(admin) }+ before do+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"+ sign_in(admin)+ enddescribe "index" doit "gets the empty products" doSiteSetting.discourse_subscriptions_public_key = "public-key"- SiteSetting.discourse_subscriptions_secret_key = "secret-key"get "/s/admin/products.json"expect(response.parsed_body).to be_emptyend@@ -55,23 +57,33 @@ RSpec.describe DiscourseSubscriptions::Admin::ProductsController dodescribe "create" doit "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: {}endit "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" }endit "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" }endit "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 doendit "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)+ .neverpost "/s/admin/products.json", params: { statement_descriptor: "" }end@@ -92,6 +107,7 @@ RSpec.describe DiscourseSubscriptions::Admin::ProductsController dorepurchaseable: "false",},),+ DiscourseSubscriptions::Stripe.request_opts,)post "/s/admin/products.json",@@ -106,21 +122,32 @@ RSpec.describe DiscourseSubscriptions::Admin::ProductsController dodescribe "show" doit "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"endenddescribe "update" doit "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: {}endenddescribe "delete" doit "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"endenddiff --git a/plugins/discourse-subscriptions/spec/requests/admin/subscriptions_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/admin/subscriptions_controller_spec.rbindex 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 docontext "when authenticated" dolet(:admin) { Fabricate(:admin) }- before { sign_in(admin) }+ before do+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"+ sign_in(admin)+ enddescribe "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 doit "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 doit "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 doit "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 doit "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 doit "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).neverdelete "/s/admin/subscriptions/sub_12345.json", params: { refund: "false" }@@ -137,14 +146,20 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController doit "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 }enddiff --git a/plugins/discourse-subscriptions/spec/requests/hooks_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/hooks_controller_spec.rbindex 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 dobefore doSiteSetting.discourse_subscriptions_webhook_secret = "zascharoo"+ SiteSetting.discourse_subscriptions_secret_key = "secret-key"SiteSetting.discourse_subscriptions_enabled = trueend@@ -156,7 +157,11 @@ RSpec.describe DiscourseSubscriptions::HooksController doevent = { 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 doevent = { 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")endit "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")endit "is returns 200" do@@ -239,11 +259,18 @@ RSpec.describe DiscourseSubscriptions::HooksController doevent = { 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")endit "is returns 422" do@@ -259,7 +286,11 @@ RSpec.describe DiscourseSubscriptions::HooksController doevent = { 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.rbindex 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 dolet(: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"+ endcontext "when showing products" dolet(:product) do@@ -63,7 +66,6 @@ RSpec.describe DiscourseSubscriptions::SubscribeController dosign_in(user)Fabricate(:product, external_id: "prodct_23456")SiteSetting.discourse_subscriptions_public_key = "public-key"- SiteSetting.discourse_subscriptions_secret_key = "secret-key"enddescribe "#index" do@@ -74,7 +76,7 @@ RSpec.describe DiscourseSubscriptions::SubscribeController doit "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 doDiscourseSubscriptions::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 dodescribe "#show" doit "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 dobefore { 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")+ endit "creates a subscription without automatic_tax param" doSiteSetting.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_optsend.returns(status: "active", customer: "cus_1234")@@ -271,14 +288,17 @@ RSpec.describe DiscourseSubscriptions::SubscribeController doit "creates a subscription with automatic tax" doSiteSetting.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 doit "creates a one time payment subscription without automatic tax" doSiteSetting.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 doit "creates a one time payment subscription" doSiteSetting.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 doendit "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 docontext "with customer name & address" doit "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 docontext "with promo code" docontext "with invalid code" doit "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 dobefore do::Stripe::PromotionCode.expects(:list)- .with({ code: "123" })+ .with({ code: "123" }, DiscourseSubscriptions::Stripe.request_opts).returns(data: [{ id: "promo123", coupon: { id: "c123" } }])endit "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 doendit "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 dodescribe "#finalize strong customer authenticated transaction" docontext "with subscription" doit "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 }endendcontext "with one-time payment" doit "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 }endend++ 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)+ endenddescribe "user groups" do@@ -615,28 +728,30 @@ RSpec.describe DiscourseSubscriptions::SubscribeController docontext "with unauthorized group" dobefore 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")endit "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 falseendit "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_emptyend@@ -644,17 +759,21 @@ RSpec.describe DiscourseSubscriptions::SubscribeController docontext "when plan has group in metadata" dobefore 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 })endit "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 doendit "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 doendit "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.rbindex 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: trueRSpec.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"+ endit "is a subclass of ApplicationController" doexpect(DiscourseSubscriptions::User::PaymentsController < ::ApplicationController).to eq(true)@@ -29,7 +32,7 @@ RSpec.describe DiscourseSubscriptions::User::PaymentsController docreated_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 doendit "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 doit "gets pricing table one-off purchases that show up as related guest payments" doSiteSetting.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.rbindex 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: trueRSpec.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"+ enddef 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 dosubscription_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 dodescribe "update" doit "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)+ .onceput "/s/user/subscriptions/sub_10z.json", params: { payment_method: "pm_abc123abc" }endenddiff --git a/plugins/discourse-subscriptions/spec/services/campaign_spec.rb b/plugins/discourse-subscriptions/spec/services/campaign_spec.rbindex 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 dodescribe "refresh_data" docontext "for all subscription purchases" doit "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 doit "checks if the goal is completed or not" doSiteSetting.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_dataexpect(Discourse.redis.get("subscriptions_goal_met_date")).to be_present@@ -100,23 +112,41 @@ describe DiscourseSubscriptions::Campaign doit "checks if goal is < 90% met after being met" doSiteSetting.discourse_subscriptions_campaign_goal = 25Discourse.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_dataexpect(Discourse.redis.get("subscriptions_goal_met_date")).to be_blankendit "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_errorendit "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_errorend@@ -150,11 +180,14 @@ describe DiscourseSubscriptions::Campaign doendit "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 doenddescribe "campaign is automatically created" do+ before { SiteSetting.discourse_subscriptions_secret_key = "secret-key" }+describe "create_campaign" doit "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