簡體   English   中英

條帶取消用戶訂閱

[英]Stripe Cancel User Subscription

完全敲打我的頭,試圖弄清楚如何取消用戶訂閱。 我已經經歷了StackOverflow,似乎找不到任何幫助。 因為我對RoR還很陌生,所以Stripe API僅使我感到困惑。 我知道我需要以某種方式捕獲並保存用戶ID,然后才能取消。 我無法弄清楚這一點...因此,為什么我不能取消訂閱。 請幫助

Subscribe_controller.rb

class SubscribeController < ApplicationController
  before_filter :authenticate_user!

  def new
    unless (params[:plan_id] == '1' || params[:plan_id] == '2' || params[:plan_id] == '3')
      flash[:notice] = "Please select a plan to sign up."
      redirect_to new_subscribe_path
    end
  end

  def update
  # Amount in cents
  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.save

  redirect_to demo_path, notice: "Your Plan was created. Enjoy the demo!"
  end

  def cancel_plan
    @user = current_user
    if @user.cancel_user_plan(params[:customer_id])
      @user.update_attributes(customer_id: nil, plan_id: 1)
      flash[:notice] = "Canceled subscription."
      redirect_to pricing_path
    else
      flash[:error] = "There was an error canceling your subscription. Please notify us."
      redirect_to edit_user_registration_path
    end
  end

  def update_plan
    @user = current_user
    if (params[:user][:stripe_id] != nil) && (params[:plan] == "2")
      @user.update_attributes(plan_id: params[:plan], email: params[:email], stripe_id: params[:user][:stripe_id])
      @user.save_with_payment
      redirect_to edit_user_registration_path, notice: "Updated to premium!"
    else
      flash[:error] = "Unable to update plan."
      redirect_to :back
    end
  end
end

User_controller.rb

class UsersController < ApplicationController
   before_action :authenticate_user!

   def update
     if current_user.update_attributes(user_params)
       flash[:notice] = "User information updated"
       redirect_to edit_user_registration_path
     else
       flash[:error] = "Invalid user information"
       redirect_to edit_user_registration_path
     end
   end

   private

   def user_params
     params.require(:user).permit(:name)
   end
 end

編輯用戶信息頁面

<div class="col-md-9 text-center">
    <h2>Manage Plan</h2>
  </div>
  <div class="text-center">
    <%= button_to "Cancel my account", cancel_plan_path, :data => { :confirm => "Are you sure?" }, :method => :delete, class: "btn btn-danger" %>
    <button class="btn-lg btn btn-primary" disabled="disabled">Update Plan</button>
  </div>

的routes.rb

get 'cancel_plan' => 'subscribe#cancel_plan'
resources :subscribe
devise_for :users

我確定您在條帶取消訂閱之前已經看到了這一點

首先,您必須先獲得客戶,然后才能在其中找到訂閱,因此可以將其刪除

# Definition 
customer = Stripe::Customer.retrieve({CUSTOMER_ID}) 
customer.subscriptions.retrieve({SUBSCRIPTION_ID}).delete 

# Example
require "stripe" 
Stripe.api_key = "sk_test_fSaKtH5qZMmhyXiF7YXup2wz" 
customer = Stripe::Customer.retrieve("cus_5LXt66ikj5nYz5")
# customer.subscriptions returns a list of all subscriptions
customer.subscriptions.retrieve("sub_5TGMEBBALjNcD6").delete

終極答案

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.save

  redirect_to demo_path, notice: "Your Plan was created. Enjoy the demo!"

為了使此操作更容易,您需要將stripe_idsubscription_id存儲在數據庫中。 因此,在User模型中創建stripe_idsubscription_id列之后,您將必須保存以下內容:

customer = Stripe::Customer.create({
                                             email: params[:stripeEmail],
                                             source: params[:stripeToken],
                                             plan: params[:plan]
                                         })

subscription_id = customer.subscriptions['data'][0].id
user.update_attributes(stripe_id: customer.id, subscription_id: subscription_id) 

現在,由於您具有ID,因此您始終可以調用該用戶的訂閱:

user = current_user
stripe_subscription = Stripe::Subscription.retrieve(user.subscription_id)
stripe_subscription.delete

如果您的數據庫中僅包含customer_id ,則可以:

user = current_user
stripe_customer = Stripe::Customer.retrieve(user.stripe_id)
stripe_subscription = stripe_customer.['data'][0].delete

暫無
暫無

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

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