REFACTOR: Use the Prices API in place of Plans (#17)
Stripe has a newer API called Prices where you can create a price for any product and it can either be recurring or one-time. The easy part is existing Plans work with the Prices API by passing a Plan ID, but objects are returned in the slightly-different Prices API object format. This commit is a refactor to the new API to handle the data in its new form, and lays the foundation for a one time payment plan to be added to any subscriptions product.
This commit is contained in:
@@ -9,7 +9,7 @@ module DiscourseSubscriptions
|
||||
|
||||
def index
|
||||
begin
|
||||
plans = ::Stripe::Plan.list(product_params)
|
||||
plans = ::Stripe::Price.list(product_params)
|
||||
|
||||
render_json_dump plans.data
|
||||
|
||||
@@ -20,15 +20,19 @@ module DiscourseSubscriptions
|
||||
|
||||
def create
|
||||
begin
|
||||
plan = ::Stripe::Plan.create(
|
||||
plan = ::Stripe::Price.create(
|
||||
nickname: params[:nickname],
|
||||
amount: params[:amount],
|
||||
interval: params[:interval],
|
||||
unit_amount: params[:amount],
|
||||
recurring: {
|
||||
interval: params[:interval],
|
||||
},
|
||||
product: params[:product],
|
||||
trial_period_days: params[:trial_period_days],
|
||||
currency: params[:currency],
|
||||
active: params[:active],
|
||||
metadata: { group_name: params[:metadata][:group_name] }
|
||||
metadata: {
|
||||
group_name: params[:metadata][:group_name],
|
||||
trial_period_days: params[:trial_period_days]
|
||||
}
|
||||
)
|
||||
|
||||
render_json_dump plan
|
||||
@@ -40,9 +44,15 @@ module DiscourseSubscriptions
|
||||
|
||||
def show
|
||||
begin
|
||||
plan = ::Stripe::Plan.retrieve(params[:id])
|
||||
plan = ::Stripe::Price.retrieve(params[:id])
|
||||
|
||||
serialized = plan.to_h.merge(currency: plan[:currency].upcase)
|
||||
if plan[:metadata] && plan[:metadata][:trial_period_days]
|
||||
trial_days = plan[:metadata][:trial_period_days]
|
||||
elsif plan[:recurring] && plan[:recurring][:trial_period_days]
|
||||
trial_days = plan[:recurring][:trial_period_days]
|
||||
end
|
||||
|
||||
serialized = plan.to_h.merge(trial_period_days: trial_days, currency: plan[:currency].upcase)
|
||||
|
||||
render_json_dump serialized
|
||||
|
||||
@@ -53,12 +63,14 @@ module DiscourseSubscriptions
|
||||
|
||||
def update
|
||||
begin
|
||||
plan = ::Stripe::Plan.update(
|
||||
plan = ::Stripe::Price.update(
|
||||
params[:id],
|
||||
nickname: params[:nickname],
|
||||
trial_period_days: params[:trial_period_days],
|
||||
active: params[:active],
|
||||
metadata: { group_name: params[:metadata][:group_name] }
|
||||
metadata: {
|
||||
group_name: params[:metadata][:group_name],
|
||||
trial_period_days: params[:trial_period_days]
|
||||
}
|
||||
)
|
||||
|
||||
render_json_dump plan
|
||||
@@ -68,17 +80,6 @@ module DiscourseSubscriptions
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
begin
|
||||
plan = ::Stripe::Plan.delete(params[:id])
|
||||
|
||||
render_json_dump plan
|
||||
|
||||
rescue ::Stripe::InvalidRequestError => e
|
||||
render_json_error e.message
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def product_params
|
||||
|
||||
@@ -9,13 +9,13 @@ module DiscourseSubscriptions
|
||||
def index
|
||||
begin
|
||||
if params[:product_id].present?
|
||||
plans = ::Stripe::Plan.list(active: true, product: params[:product_id])
|
||||
plans = ::Stripe::Price.list(active: true, product: params[:product_id])
|
||||
else
|
||||
plans = ::Stripe::Plan.list(active: true)
|
||||
plans = ::Stripe::Price.list(active: true)
|
||||
end
|
||||
|
||||
serialized = plans[:data].map do |plan|
|
||||
plan.to_h.slice(:id, :amount, :currency, :interval)
|
||||
plan.to_h.slice(:id, :unit_amount, :currency, :recurring)
|
||||
end.sort_by { |plan| plan[:amount] }
|
||||
|
||||
render_json_dump serialized
|
||||
|
||||
@@ -27,12 +27,17 @@ module DiscourseSubscriptions
|
||||
|
||||
def create
|
||||
begin
|
||||
plan = ::Stripe::Plan.retrieve(params[:plan])
|
||||
plan = ::Stripe::Price.retrieve(params[:plan])
|
||||
|
||||
if plan[:metadata] && plan[:metadata][:trial_period_days]
|
||||
trial_days = plan[:metadata][:trial_period_days]
|
||||
end
|
||||
|
||||
@subscription = ::Stripe::Subscription.create(
|
||||
customer: params[:customer],
|
||||
items: [ { plan: params[:plan] } ],
|
||||
items: [ { price: params[:plan] } ],
|
||||
metadata: metadata_user,
|
||||
trial_period_days: trial_days
|
||||
)
|
||||
|
||||
group = plan_group(plan)
|
||||
|
||||
@@ -17,7 +17,7 @@ module DiscourseSubscriptions
|
||||
subscriptions = []
|
||||
|
||||
if subscription_ids
|
||||
plans = ::Stripe::Plan.list(
|
||||
plans = ::Stripe::Price.list(
|
||||
expand: ['data.product'],
|
||||
limit: 100
|
||||
)
|
||||
@@ -34,8 +34,9 @@ module DiscourseSubscriptions
|
||||
subscriptions = subscriptions.select { |sub| subscription_ids.include?(sub[:id]) }
|
||||
|
||||
subscriptions.map! do |subscription|
|
||||
plan = plans[:data].find { |p| p[:id] == subscription[:plan][:id] }
|
||||
subscription.to_h.merge(product: plan[:product].to_h.slice(:id, :name))
|
||||
plan = plans[:data].find { |p| p[:id] == subscription[:items][:data][0][:price][:id] }
|
||||
subscription.to_h.except!(:plan)
|
||||
subscription.to_h.merge(plan: plan, product: plan[:product].to_h.slice(:id, :name))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user