From b1f303a1c8eb70b8652e7210812ad05422a25ccd Mon Sep 17 00:00:00 2001 From: Nat Date: Thu, 4 Jan 2024 15:52:24 +0800 Subject: [PATCH] UX: Surface stripe AuthenticationError on the Admin Dashboard --- app/controllers/concerns/stripe.rb | 2 + .../discourse_subscriptions/campaign.rb | 45 ++++++++++++------- config/locales/server.en.yml | 2 + plugin.rb | 4 ++ spec/services/campaign_spec.rb | 25 +++++++++++ 5 files changed, 62 insertions(+), 16 deletions(-) diff --git a/app/controllers/concerns/stripe.rb b/app/controllers/concerns/stripe.rb index bbe8bbe..8810546 100644 --- a/app/controllers/concerns/stripe.rb +++ b/app/controllers/concerns/stripe.rb @@ -4,6 +4,8 @@ module DiscourseSubscriptions module Stripe extend ActiveSupport::Concern + AUTH_PROBLEM_IDENTIFIER = "#{DiscourseSubscriptions::PLUGIN_NAME}.stripe_auth_error" + def set_api_key ::Stripe.api_key = SiteSetting.discourse_subscriptions_secret_key end diff --git a/app/services/discourse_subscriptions/campaign.rb b/app/services/discourse_subscriptions/campaign.rb index 16aec62..eace93d 100644 --- a/app/services/discourse_subscriptions/campaign.rb +++ b/app/services/discourse_subscriptions/campaign.rb @@ -17,26 +17,39 @@ module DiscourseSubscriptions product_ids = product_ids.include?(campaign_product) ? [campaign_product] : [] end - amount = 0.00 - subscriptions = get_subscription_data - subscriptions = filter_to_subscriptions_products(subscriptions, product_ids) + begin + amount = 0.00 + subscriptions = get_subscription_data + subscriptions = filter_to_subscriptions_products(subscriptions, product_ids) - # Fetch product purchases - one_time_payments = get_one_time_payments(product_ids) - one_time_payments.each { |c| amount += c[:price].to_f / 100.00 } + # Fetch product purchases + one_time_payments = get_one_time_payments(product_ids) + one_time_payments.each { |c| amount += c[:price].to_f / 100.00 } - # get number of subscribers - SiteSetting.discourse_subscriptions_campaign_subscribers = subscriptions&.length.to_i + # get number of subscribers + SiteSetting.discourse_subscriptions_campaign_subscribers = subscriptions&.length.to_i - # calculate amount raised - subscriptions&.each do |sub| - sub_amount = calculate_monthly_amount(sub) - amount += sub_amount / 100.00 + # calculate amount raised + subscriptions&.each do |sub| + sub_amount = calculate_monthly_amount(sub) + amount += sub_amount / 100.00 + end + + SiteSetting.discourse_subscriptions_campaign_amount_raised = amount.round(2) + + check_goal_status + rescue ::Stripe::AuthenticationError + message = + I18n.t( + "discourse_subscriptions.auth.invalid", + settingsUrl: "admin/site_settings/category/discourse_subscriptions", + ) + AdminDashboardData.add_found_scheduled_check_problem( + AdminDashboardData::Problem.new(message, identifier: AUTH_PROBLEM_IDENTIFIER), + ) + else + AdminDashboardData.clear_found_problem(AUTH_PROBLEM_IDENTIFIER) end - - SiteSetting.discourse_subscriptions_campaign_amount_raised = amount.round(2) - - check_goal_status end def create_campaign diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index ff7edbf..59ebc07 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -4,3 +4,5 @@ en: card: declined: Card Declined invalid: Card Invalid + auth: + invalid: Invalid Credentials for Stripe. Please check your discourse-subscriptions settings. diff --git a/plugin.rb b/plugin.rb index c745f0f..34ec012 100644 --- a/plugin.rb +++ b/plugin.rb @@ -27,6 +27,10 @@ extend_content_security_policy(script_src: %w[https://js.stripe.com/v3/ https:// add_admin_route "discourse_subscriptions.admin_navigation", "discourse-subscriptions.products" +module ::DiscourseSubscriptions + PLUGIN_NAME = "discourse-subscriptions" +end + Discourse::Application.routes.append do get "/admin/plugins/discourse-subscriptions" => "admin/plugins#index", :constraints => AdminConstraint.new diff --git a/spec/services/campaign_spec.rb b/spec/services/campaign_spec.rb index 583420b..efbfd1e 100644 --- a/spec/services/campaign_spec.rb +++ b/spec/services/campaign_spec.rb @@ -150,6 +150,31 @@ describe DiscourseSubscriptions::Campaign do expect(SiteSetting.discourse_subscriptions_campaign_amount_raised).to eq 8.33 end end + + context "with ::Stripe::AuthenticationError" do + it "adds a problem to the admin dashboard" do + ::Stripe::Subscription.expects(:list).raises(::Stripe::AuthenticationError) + + DiscourseSubscriptions::Campaign.new.refresh_data + + expect(AdminDashboardData.load_found_scheduled_check_problems.count).to eq(1) + end + + it "clears the problem when no errors" do + ::Stripe::Subscription.expects(:list).returns(data: [subscription], has_more: false) + ::Stripe::Invoice.expects(:list).returns(data: [invoice, invoice2], has_more: false) + AdminDashboardData.add_found_scheduled_check_problem( + AdminDashboardData::Problem.new( + "x", + identifier: DiscourseSubscriptions::Stripe::AUTH_PROBLEM_IDENTIFIER, + ), + ) + + DiscourseSubscriptions::Campaign.new.refresh_data + + expect(AdminDashboardData.load_found_scheduled_check_problems.count).to eq(0) + end + end end end