1
0
mirror of synced 2026-08-05 22:56:55 +00:00

plans and products

This commit is contained in:
Rimian Perkins
2019-10-17 12:07:06 +11:00
parent 01b78b31df
commit e2b915b905
13 changed files with 107 additions and 40 deletions
+3 -5
View File
@@ -20,13 +20,11 @@ module DiscoursePatrons
def create
begin
plan = ::Stripe::Plan.create(
amount: params[:amount],
interval: params[:interval],
product: { name: params[:name] },
product: product,
currency: SiteSetting.discourse_patrons_currency,
id: plan_id,
)
render_json_dump plan
@@ -49,8 +47,8 @@ module DiscoursePatrons
private
def plan_id
params[:name].parameterize.dasherize if params[:name]
def product
params[:product].slice(:id, :name).permit!.to_h.symbolize_keys if params[:product]
end
end
end
+4 -5
View File
@@ -47,11 +47,10 @@ module DiscoursePatrons
def update
begin
product = ::Stripe::Product.update(
params[:id], {
name: params[:name],
active: params[:active],
metadata: metadata
}
params[:id],
name: params[:name],
active: params[:active],
metadata: metadata
)
render_json_dump product
+8 -2
View File
@@ -7,8 +7,14 @@ module DiscoursePatrons
before_action :set_api_key
def index
plans = ::Stripe::Plan.list
render json: plans.data
begin
plans = ::Stripe::Plan.list
render_json_dump plans.data
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end
end
end
+24 -7
View File
@@ -7,14 +7,31 @@ module DiscoursePatrons
before_action :set_api_key
def create
subscription = ::Stripe::Subscription.create(
customer: params[:customer],
items: [
{ plan: params[:plan] },
]
)
begin
subscription = ::Stripe::Subscription.create(
customer: params[:customer],
items: [
{ plan: params[:plan] },
]
)
render_json_dump subscription
if subscription_ok(subscription)
# TODO: check group credentials
group = Group.find_by_name('group-123')
group.add(current_user)
end
render_json_dump subscription
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end
private
def subscription_ok(subscription)
['active', 'trialing'].include?(subscription[:status])
end
end
end