繁体   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