簡體   English   中英

Rails-條紋訂閱(設置時間段)

[英]Rails - Stripe Subscription (Set Time Period)

我在rails應用程序上運行的條紋很好,還有2個其他問題來總結一些功能。

  1. 有沒有一種方法可以創建僅6個月或任何給定設置時間的訂閱,以便在上述時間段后不向客戶收費?
  2. 如果用戶完全取消了他們的計划,我需要刪除其訂閱。 我敢肯定這將包括dependent: :destroy但我不確定在哪里放置它。 謝謝!!

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),您可以做的是創建一個可由調度程序https://github.com/javan/觸發的過程,然后檢查給定訂閱是否已超過6個月並將該帳戶移至另一個訂閱

對於問題2),請查看https://stripe.com/docs/api#cancel_subscription 您不希望刪除或銷毀數據,因為您丟失了記帳的歷史記錄。

您想要將訂閱更新為取消狀態

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM