条带取消用户订阅

时间:2015-01-07 03:36:27

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

完全撞到墙上试图弄清楚如何取消用户订阅。我一直都在通过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

3 个答案:

答案 0 :(得分:2)

我确信您在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

答案 1 :(得分: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!"

答案 2 :(得分:0)

为了简化操作,您需要将regex.varUser Designed Variables存储在数据库中。因此,在stripe_id模型中创建列subscription_idstripe_id之后,您将必须保存以下内容:

subscription_id

现在,由于您具有ID,因此您始终可以调用该用户的订阅:

User

如果您的数据库中只有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) ,则可以:

user = current_user
stripe_subscription = Stripe::Subscription.retrieve(user.subscription_id)
stripe_subscription.delete
相关问题