【发布时间】:2015-01-11 17:26:31
【问题描述】:
我的 rails 应用程序运行良好,并且有 2 个附加问题来总结一些功能。
- 有没有办法创建仅 6 个月或任何给定设定时间的订阅,以便在所述时间段后不向客户收费?
- 如果用户完全取消他们的计划,我需要删除他们的订阅。我确信这将包括
dependent: :destroy,但我不确定将它放在哪里。谢谢!!
订阅控制器.rb
def create
token = params[:stripeToken]
customer = Stripe::Customer.create(
:email => current_user.email,
:card => token,
plan: params[:id]
)
current_user.subscribed = true
current_user.stripe_id = customer.id
current_user.stripe_subscription_id = customer.subscriptions['data'][0].id
current_user.plan_name = customer.subscriptions['data'][0].plan.name
current_user.interval = customer.subscriptions['data'][0].plan.interval
current_user.amount = customer.subscriptions['data'][0].plan.amount
current_user.status = customer.subscriptions['data'][0].status
current_user.stripe_plan_id = customer.subscriptions['data'][0].plan.id
current_user.save
end
def destroy
customer = Stripe::Customer.retrieve(current_user.stripe_id)
free_plan = Stripe::Plan.retrieve('1')
if current_user.stripe_id = customer.id
customer.subscriptions.retrieve(current_user.stripe_subscription_id).delete
current_user.update_attributes(stripe_plan_id: free_plan.id , plan_name: free_plan.name, amount: free_plan.amount , status: "Inactive" )
current_user.save
flash[:notice] = "Your plan has been downgraded to #{current_user.plan_name}"
redirect_to edit_user_registration_path
else
customer.subscriptions.retrieve(current_user.stripe_subscription_id).delete
current_user.update_attributes(stripe_subscription_id: customer.subscriptions['data'][0].id)
flash[:notice] = "Blah"
redirect_to edit_user_registration_path
end
end
订阅模式
class Subscription < ActiveRecord::Base
belongs_to :user
end
用户模型
has_many :subscriptions, dependent: :destroy
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 stripe-payments