Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 条带取消用户订阅_Ruby On Rails_Ruby On Rails 4_Stripe Payments - Fatal编程技术网

Ruby on rails 条带取消用户订阅

Ruby on rails 条带取消用户订阅,ruby-on-rails,ruby-on-rails-4,stripe-payments,Ruby On Rails,Ruby On Rails 4,Stripe Payments,我的头撞在墙上,想弄清楚如何取消用户订阅。我已经忙得不可开交了,似乎找不到任何帮助。因为我对RoR还比较陌生,所以StripeAPI只会让我更加困惑。我明白,在取消之前,我需要捕获并保存用户ID。我还没有弄明白…所以我不能取消订阅。请帮忙 订阅\u controller.rb class SubscribeController < ApplicationController before_filter :authenticate_user! def new unless

我的头撞在墙上,想弄清楚如何取消用户订阅。我已经忙得不可开交了,似乎找不到任何帮助。因为我对RoR还比较陌生,所以StripeAPI只会让我更加困惑。我明白,在取消之前,我需要捕获并保存用户ID。我还没有弄明白…所以我不能取消订阅。请帮忙

订阅\u 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
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

我相信你以前见过这个

首先,您必须获得客户,您可以在那里找到订阅,以便删除它

# 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!"

为了简化此操作,您需要在数据库中存储条带id和订阅id。因此,在用户模型中创建列stripe_id和subscription_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
如果数据库中只有客户id,则可以:

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

谢谢你的意见…是的,这就是我遇到的麻烦。什么代替了客户ID和订阅ID?我还假设这是放在它自己的方法完全正确?我完全理解这一概念,但不知道在这两个区域或.retrievecus\u中放置什么。。。或者。retrievesub_uz…如果您登录到您的stripe acc,您将看到客户ID。它就像cus_5LXt66ikj5nYz5一样。当我创建客户时,我希望将其保存在我的数据库yes中。我以为我是用update方法下的'current_user.subscribed=true current_user.stripe_id=customer.id current_user.save'调用来实现的。那部分不对吗?因为我似乎无法将它们保存在我的数据库中。你能给我到上午10点吗?明白了谢谢你的帮助。稍后再谈。这实际上是如何删除和/或取消条带::订阅的?
user = current_user
stripe_subscription = Stripe::Subscription.retrieve(user.subscription_id)
stripe_subscription.delete
user = current_user
stripe_customer = Stripe::Customer.retrieve(user.stripe_id)
stripe_subscription = stripe_customer.['data'][0].delete