【问题标题】:Rails - Stripe Subscription (Set Time Period)Rails - Stripe 订阅(设置时间段)
【发布时间】:2015-01-11 17:26:31
【问题描述】:

我的 rails 应用程序运行良好,并且有 2 个附加问题来总结一些功能。

  1. 有没有办法创建仅 6 个月或任何给定设定时间的订阅,以便在所述时间段后不向客户收费?
  2. 如果用户完全取消他们的计划,我需要删除他们的订阅。我确信这将包括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


    【解决方案1】:

    对于问题 #1)您可以做的是创建一个可由调度程序 https://github.com/javan/whenever 触发的进程,然后检查给定订阅是否超过 6 个月并将帐户移至另一个订阅

    对于问题 #2)查看https://stripe.com/docs/api#cancel_subscription。 您不想删除或销毁,因为您丢失了会计历史。

    您想将订阅更新为取消状态

    【讨论】:

    • 哇!看起来很神奇,我试一试!谢谢!希望你的活动很有趣!
    猜你喜欢
    • 2014-09-27
    • 2021-01-01
    • 2016-10-18
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 2017-01-07
    • 2023-03-03
    • 2017-07-03
    相关资源
    最近更新 更多