Rails - 条纹订阅(设置时间段)

时间:2015-01-11 17:26:31

标签: ruby-on-rails ruby-on-rails-4 stripe-payments

我在我的rails应用程序上运行了很好的条带,并且有2个附加问题来包含一些功能。

  1. 有没有办法在6个月或任何给定的时间内创建订阅,以便在上述时间段后不向客户收费?
  2. 如果用户完全取消他们的计划,我需要条带来删除他们的订阅。我确信这将包括dependent: :destroy,但我不确定在哪里放置它。谢谢!
  3. Subscribe_controller.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
    

1 个答案:

答案 0 :(得分:0)

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

对于问题#2),请查看https://stripe.com/docs/api#cancel_subscription。 你不想删除或破坏,因为你失去了会计的历史。

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