UX: Surface stripe AuthenticationError on the Admin Dashboard

This commit is contained in:
Nat 2024-01-04 15:52:24 +08:00
parent 331c8630c3
commit b1f303a1c8
No known key found for this signature in database
GPG Key ID: 4938B35D927EC773
5 changed files with 62 additions and 16 deletions

View File

@ -4,6 +4,8 @@ module DiscourseSubscriptions
module Stripe module Stripe
extend ActiveSupport::Concern extend ActiveSupport::Concern
AUTH_PROBLEM_IDENTIFIER = "#{DiscourseSubscriptions::PLUGIN_NAME}.stripe_auth_error"
def set_api_key def set_api_key
::Stripe.api_key = SiteSetting.discourse_subscriptions_secret_key ::Stripe.api_key = SiteSetting.discourse_subscriptions_secret_key
end end

View File

@ -17,26 +17,39 @@ module DiscourseSubscriptions
product_ids = product_ids.include?(campaign_product) ? [campaign_product] : [] product_ids = product_ids.include?(campaign_product) ? [campaign_product] : []
end end
amount = 0.00 begin
subscriptions = get_subscription_data amount = 0.00
subscriptions = filter_to_subscriptions_products(subscriptions, product_ids) subscriptions = get_subscription_data
subscriptions = filter_to_subscriptions_products(subscriptions, product_ids)
# Fetch product purchases # Fetch product purchases
one_time_payments = get_one_time_payments(product_ids) one_time_payments = get_one_time_payments(product_ids)
one_time_payments.each { |c| amount += c[:price].to_f / 100.00 } one_time_payments.each { |c| amount += c[:price].to_f / 100.00 }
# get number of subscribers # get number of subscribers
SiteSetting.discourse_subscriptions_campaign_subscribers = subscriptions&.length.to_i SiteSetting.discourse_subscriptions_campaign_subscribers = subscriptions&.length.to_i
# calculate amount raised # calculate amount raised
subscriptions&.each do |sub| subscriptions&.each do |sub|
sub_amount = calculate_monthly_amount(sub) sub_amount = calculate_monthly_amount(sub)
amount += sub_amount / 100.00 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 end
SiteSetting.discourse_subscriptions_campaign_amount_raised = amount.round(2)
check_goal_status
end end
def create_campaign def create_campaign

View File

@ -4,3 +4,5 @@ en:
card: card:
declined: Card Declined declined: Card Declined
invalid: Card Invalid invalid: Card Invalid
auth:
invalid: Invalid Credentials for Stripe. Please check your <a href="%{settingsUrl}" target="_blank">discourse-subscriptions settings</a>.

View File

@ -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" add_admin_route "discourse_subscriptions.admin_navigation", "discourse-subscriptions.products"
module ::DiscourseSubscriptions
PLUGIN_NAME = "discourse-subscriptions"
end
Discourse::Application.routes.append do Discourse::Application.routes.append do
get "/admin/plugins/discourse-subscriptions" => "admin/plugins#index", get "/admin/plugins/discourse-subscriptions" => "admin/plugins#index",
:constraints => AdminConstraint.new :constraints => AdminConstraint.new

View File

@ -150,6 +150,31 @@ describe DiscourseSubscriptions::Campaign do
expect(SiteSetting.discourse_subscriptions_campaign_amount_raised).to eq 8.33 expect(SiteSetting.discourse_subscriptions_campaign_amount_raised).to eq 8.33
end end
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
end end