-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from paviliondev/merge_x_discourse_subscriptions
Add x-discourse-subscriptions into discourse-subscription-server
- Loading branch information
Showing
14 changed files
with
260 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
ast (2.4.2) | ||
json (2.6.3) | ||
language_server-protocol (3.17.0.3) | ||
parallel (1.23.0) | ||
parser (3.2.2.4) | ||
ast (~> 2.4.1) | ||
racc | ||
racc (1.7.3) | ||
rainbow (3.1.1) | ||
regexp_parser (2.8.2) | ||
rexml (3.2.6) | ||
rubocop (1.57.2) | ||
json (~> 2.3) | ||
language_server-protocol (>= 3.17.0) | ||
parallel (~> 1.10) | ||
parser (>= 3.2.2.4) | ||
rainbow (>= 2.2.2, < 4.0) | ||
regexp_parser (>= 1.8, < 3.0) | ||
rexml (>= 3.2.5, < 4.0) | ||
rubocop-ast (>= 1.28.1, < 2.0) | ||
ruby-progressbar (~> 1.7) | ||
unicode-display_width (>= 2.4.0, < 3.0) | ||
rubocop-ast (1.30.0) | ||
parser (>= 3.2.1.0) | ||
rubocop-capybara (2.19.0) | ||
rubocop (~> 1.41) | ||
rubocop-discourse (3.4.1) | ||
rubocop (>= 1.1.0) | ||
rubocop-rspec (>= 2.0.0) | ||
rubocop-factory_bot (2.24.0) | ||
rubocop (~> 1.33) | ||
rubocop-rspec (2.25.0) | ||
rubocop (~> 1.40) | ||
rubocop-capybara (~> 2.17) | ||
rubocop-factory_bot (~> 2.22) | ||
ruby-progressbar (1.13.0) | ||
unicode-display_width (2.5.0) | ||
|
||
PLATFORMS | ||
arm64-darwin-22 | ||
x86_64-linux | ||
|
||
DEPENDENCIES | ||
rubocop-discourse | ||
|
||
BUNDLED WITH | ||
2.4.13 |
11 changes: 11 additions & 0 deletions
11
app/controllers/subscription_server/user_authorizations_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class SubscriptionServer::UserAuthorizationsController < ApplicationController | ||
before_action :ensure_logged_in | ||
|
||
def destroy | ||
params.require(:domain) | ||
current_user.remove_subscription_domain(params[:domain]) | ||
render json: success_json | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
plugins: | ||
subscription_server_enabled: true | ||
subscription_server_enabled: | ||
client: true | ||
default: true | ||
subscription_server_supplier_name: '' | ||
subscription_server_subscriptions: | ||
type: list | ||
default: '' | ||
default: '' |
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
extensions/discourse_subscriptions_coupons_controller_extension.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
module DiscourseSubscriptionsCouponsControllerExtension | ||
def create | ||
params.require([:promo, :discount_type, :discount, :active, :applies_to_products]) | ||
begin | ||
coupon_params = { | ||
duration: 'forever', | ||
max_redemptions: params[:max_redemptions] || 1, | ||
applies_to: { | ||
products: params[:applies_to_products] | ||
} | ||
} | ||
|
||
case params[:discount_type] | ||
when 'amount' | ||
coupon_params[:amount_off] = params[:discount].to_i * 100 | ||
coupon_params[:currency] = SiteSetting.discourse_subscriptions_currency | ||
when 'percent' | ||
coupon_params[:percent_off] = params[:discount] | ||
end | ||
|
||
coupon = ::Stripe::Coupon.create(coupon_params) | ||
promo_code = ::Stripe::PromotionCode.create({ coupon: coupon[:id], code: params[:promo] }) if coupon.present? | ||
|
||
render_json_dump promo_code | ||
rescue ::Stripe::InvalidRequestError => e | ||
render_json_error e.message | ||
end | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
extensions/discourse_subscriptions_products_controller_extension.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
module DiscourseSubscriptionsProductsControllerExtension | ||
def show | ||
begin | ||
product = ::Stripe::Product.retrieve(params[:id]) | ||
|
||
if product[:metadata][:hidden].present? | ||
product[:metadata][:hidden] = ActiveRecord::Type::Boolean.new.cast(product[:metadata][:hidden]) | ||
end | ||
|
||
render_json_dump product | ||
|
||
rescue ::Stripe::InvalidRequestError => e | ||
render_json_error e.message | ||
end | ||
end | ||
|
||
private def product_params | ||
params.permit! | ||
|
||
{ | ||
name: params[:name], | ||
active: params[:active], | ||
statement_descriptor: params[:statement_descriptor], | ||
metadata: { | ||
description: params.dig(:metadata, :description), | ||
repurchaseable: params.dig(:metadata, :repurchaseable), | ||
hidden: params.dig(:metadata, :hidden) | ||
} | ||
} | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
extensions/discourse_subscriptions_subscriber_controller_extension.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
module DiscourseSubscriptionsSubscribeControllerExtension | ||
private def serialize_product(product) | ||
{ | ||
id: product[:id], | ||
name: product[:name], | ||
description: PrettyText.cook(product[:metadata][:description]), | ||
subscribed: current_user_products.include?(product[:id]), | ||
repurchaseable: product[:metadata][:repurchaseable], | ||
hidden: ActiveRecord::Type::Boolean.new.cast(product[:metadata][:hidden]) | ||
} | ||
end | ||
|
||
private def serialize_plans(plans) | ||
plans[:data].reduce([]) do |result, p| | ||
plan = p.to_h | ||
if plan[:nickname] != "hidden" | ||
result << plan.slice(:id, :unit_amount, :currency, :type, :recurring, :nickname) | ||
end | ||
result | ||
end.sort_by { |plan| plan[:amount] } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
spec/requests/subscription_server/user_authorizations_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
describe SubscriptionServer::UserAuthorizationsController do | ||
let(:user) { Fabricate(:user) } | ||
let(:provider) { "stripe" } | ||
let(:product_id) { "prod_CBTNpi3fqWWkq0" } | ||
let(:product_slug) { "business" } | ||
let(:resource) { "custom_wizard" } | ||
let(:domain) { "demo.pavilion.tech" } | ||
|
||
it "requires a user" do | ||
delete "/subscription-server/user-authorizations" | ||
expect(response.status).to eq(403) | ||
end | ||
|
||
context "with a user" do | ||
before do | ||
sign_in(user) | ||
end | ||
|
||
describe "#destroy" do | ||
|
||
it "requires a domain" do | ||
delete "/subscription-server/user-authorizations" | ||
expect(response.status).to eq(400) | ||
end | ||
|
||
it "removes the domain from all of the user's products" do | ||
user.add_subscription_product_domain(domain, resource, provider, product_id) | ||
user.add_subscription_product_domain(domain, resource, provider, "prod_12345") | ||
|
||
delete "/subscription-server/user-authorizations", params: { domain: domain } | ||
expect(response.status).to eq(200) | ||
|
||
user.reload | ||
expect(user.custom_fields[user.subscription_product_domain_key(resource, provider, product_id)]).to eq("") | ||
expect(user.custom_fields[user.subscription_product_domain_key(resource, provider, "prod_12345")]).to eq("") | ||
end | ||
end | ||
end | ||
end |