【问题标题】:Stripe Cancel User Subscription条纹取消用户订阅
【发布时间】:2019-04-22 09:18:01
【问题描述】:

我完全不知道如何取消用户订阅。我一直在通过 StackOverflow,似乎找不到任何帮助。因为我对 RoR 还很陌生,所以 Stripe API 只会让我更加困惑。我明白在取消之前我需要以某种方式捕获并保存用户 ID。我无法弄清楚这一点......因此为什么我不能取消订阅。请帮忙

订阅控制器.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

用户控制器.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

【问题讨论】:

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


    【解决方案1】:

    我相信你在stripe cancel subscription 之前见过这个

    首先你必须让客户在那里你可以找到订阅,这样你就可以删除它

    # 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
    

    【讨论】:

    • 感谢您的意见...是的,这就是我遇到麻烦的地方。什么可以代替 CUSTOMER_ID 和 SUBSCRIPTION_ID?另外我假设这是完全正确的放置在它自己的方法中?我完全理解这个概念,但不知道在这两个区域或 .retrieve("cus_...") 或 .retrieve("sub_...") 中放置什么
    • 如果您登录到您的 stripe acc,您会看到 CUSTOMER_ID 它看起来像 cus_5LXt66ikj5nYz5 当我创建一个我喜欢保存的客户时在我的数据库中
    • 是的。我以为我正在使用更新方法下的 '''current_user.subscribed = true current_user.stripe_id = customer.id current_user.save''' 调用。那部分不正确吗?因为我似乎无法将它们保存在我的数据库中
    • 是的。你能在中部时间上午 10 点之前给我吗?
    • 知道了 感谢您的帮助。快说吧。
    【解决方案2】:

    终极答案

    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::Subscription 的?
    【解决方案3】:

    为了使这更容易,您需要将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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 2021-07-15
      • 2023-04-10
      • 2012-01-26
      • 1970-01-01
      • 2019-03-31
      相关资源
      最近更新 更多