mirror of
https://github.com/discourse/discourse.git
synced 2026-08-08 17:53:55 +08:00
Subscriptions rendered its own nav inside the page (legacy) and hid it entirely until Stripe was configured, so from the sidebar, a fresh install landed on a dead-end "Stripe is not configured" message. This change moves the plugin onto the newer admin show route, and an unconfigured Stripe lands on the settings tab with the other tabs showing an empty state that links back to settings. Also removed some dead code Before: <img width="2762" height="818" alt="image" src="https://github.com/user-attachments/assets/0896d0eb-f183-4e29-a724-07f6cc0ef847" /> After: <img width="2272" height="1244" alt="image" src="https://github.com/user-attachments/assets/607d396f-3beb-4275-967e-c1d6f3169955" /> --------- Co-authored-by: Martin Brennan <martin@discourse.org>
107 lines
4.1 KiB
Ruby
Vendored
107 lines
4.1 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
# name: discourse-subscriptions
|
|
# about: Allows admins to sell subscriptions to site content.
|
|
# meta_topic_id: 140818
|
|
# version: 2.8.1
|
|
# authors: Rimian Perkins, Justin DiRose
|
|
# url: https://github.com/discourse/discourse/tree/main/plugins/discourse-subscriptions
|
|
|
|
enabled_site_setting :discourse_subscriptions_enabled
|
|
|
|
require "stripe"
|
|
|
|
register_asset "stylesheets/common/main.scss"
|
|
register_asset "stylesheets/common/layout.scss"
|
|
register_asset "stylesheets/common/subscribe.scss"
|
|
register_asset "stylesheets/common/campaign.scss"
|
|
register_asset "stylesheets/mobile/main.scss"
|
|
register_svg_icon "far-credit-card"
|
|
|
|
register_html_builder("server:before-head-close") do |controller|
|
|
"<script src='https://js.stripe.com/v3/' nonce='#{controller.helpers.csp_nonce_placeholder}'></script>"
|
|
end
|
|
|
|
register_html_builder("server:before-head-close") do |controller|
|
|
"<script async src='https://js.stripe.com/v3/pricing-table.js' nonce='#{controller.helpers.csp_nonce_placeholder}'></script>"
|
|
end
|
|
|
|
extend_content_security_policy(script_src: %w[https://js.stripe.com/v3/ https://hooks.stripe.com])
|
|
|
|
add_admin_route "discourse_subscriptions.admin_navigation",
|
|
"discourse-subscriptions",
|
|
use_new_show_route: true
|
|
|
|
Discourse::Application.routes.append do
|
|
get "/admin/plugins/discourse-subscriptions/products" => "admin/plugins#index",
|
|
:constraints => AdminConstraint.new
|
|
get "/admin/plugins/discourse-subscriptions/products/:product_id" => "admin/plugins#index",
|
|
:constraints => AdminConstraint.new
|
|
get "/admin/plugins/discourse-subscriptions/products/:product_id/plans" => "admin/plugins#index",
|
|
:constraints => AdminConstraint.new
|
|
get "/admin/plugins/discourse-subscriptions/products/:product_id/plans/:plan_id" =>
|
|
"admin/plugins#index",
|
|
:constraints => AdminConstraint.new
|
|
get "/admin/plugins/discourse-subscriptions/subscriptions" => "admin/plugins#index",
|
|
:constraints => AdminConstraint.new
|
|
get "/admin/plugins/discourse-subscriptions/coupons" => "admin/plugins#index",
|
|
:constraints => AdminConstraint.new
|
|
get "u/:username/billing" => "users#show", :constraints => { username: RouteFormat.username }
|
|
get "u/:username/billing/:id" => "users#show", :constraints => { username: RouteFormat.username }
|
|
get "u/:username/billing/subscriptions/card/:subscription_id" => "users#show",
|
|
:constraints => {
|
|
username: RouteFormat.username,
|
|
}
|
|
end
|
|
|
|
module ::DiscourseSubscriptions
|
|
PLUGIN_NAME = "discourse-subscriptions"
|
|
CHECKOUT_SESSION_USER_REFERENCE_PURPOSE = "checkout_user"
|
|
CHECKOUT_SESSION_USER_REFERENCE_EXPIRES_IN = 30.days
|
|
end
|
|
|
|
require_relative "lib/discourse_subscriptions/engine"
|
|
require_relative "app/controllers/concerns/stripe"
|
|
require_relative "app/controllers/concerns/group"
|
|
|
|
after_initialize do
|
|
::Stripe.api_version = "2024-04-10"
|
|
|
|
::Stripe.set_app_info(
|
|
"Discourse Subscriptions",
|
|
version: "2.8.2",
|
|
url: "https://github.com/discourse/discourse-subscriptions",
|
|
)
|
|
|
|
Discourse::Application.routes.append { mount DiscourseSubscriptions::Engine, at: "s" }
|
|
|
|
add_to_serializer(
|
|
:current_user,
|
|
:discourse_subscriptions_checkout_session_user_reference,
|
|
include_condition: -> do
|
|
SiteSetting.discourse_subscriptions_enabled &&
|
|
SiteSetting.discourse_subscriptions_pricing_table_enabled
|
|
end,
|
|
) do
|
|
object.signed_id(
|
|
expires_in: DiscourseSubscriptions::CHECKOUT_SESSION_USER_REFERENCE_EXPIRES_IN,
|
|
purpose: DiscourseSubscriptions::CHECKOUT_SESSION_USER_REFERENCE_PURPOSE,
|
|
)
|
|
end
|
|
|
|
add_to_serializer(
|
|
:site,
|
|
:discourse_subscriptions_stripe_configured,
|
|
include_condition: -> { scope.is_admin? },
|
|
) { DiscourseSubscriptions::Stripe.configured? }
|
|
|
|
add_to_serializer(:site, :show_campaign_banner) do
|
|
enabled = SiteSetting.discourse_subscriptions_enabled
|
|
campaign_enabled = SiteSetting.discourse_subscriptions_campaign_enabled
|
|
goal_met = Discourse.redis.get("subscriptions_goal_met_date")
|
|
|
|
enabled && campaign_enabled && (!goal_met || 7.days.ago <= Date.parse(goal_met))
|
|
rescue StandardError
|
|
false
|
|
end
|
|
end
|