2015-04-23 17 views
2

這是我第一次使用Stripe和Rails,現在我試圖讓高級用戶取消訂閱。如何解決這個錯誤「未定義的方法`編碼爲零:NilClassa」並取消訂閱計劃工作?

我可以使用我的代碼將用戶從標準級別升級到高級級別,但我在嘗試將高級用戶降級到標準級別時遇到問題。

我遵循的「取消訂閱」條紋紅寶石API參考:https://stripe.com/docs/api?lang=ruby#cancel_subscription,但是當我點擊「取消訂閱」按鈕,我得到這個錯誤:

NoMethodError - 未定義的方法encoding' for nil:NilClass: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/cgi/util.rb:7:in逃離」 條紋(1.21 .0)lib/stripe/list_object.rb:19:在retrieve' app/controllers/subscriptions_controller.rb:55:in降級'

我的rails版本是4.2.1。

我的代碼:

class SubscriptionsController < ApplicationController 

def create 
    subscription = Subscription.new 
     stripe_sub = nil 
    if current_user.stripe_customer_id.blank? 
     # Creates a Stripe Customer object, for associating with the charge 
     customer = Stripe::Customer.create(
     email: current_user.email, 
     card: params[:stripeToken], 
     plan: 'premium_plan' 
     ) 
     current_user.stripe_customer_id = customer.id 
     current_user.save! 
     stripe_sub = customer.subscriptions.first 
    else 
     customer = Stripe::Customer.retrieve(current_user.stripe_customer_id) 
     stripe_sub = customer.subscriptions.create(
     plan: 'premium_plan' 
     ) 
    end 

    current_user.subid = stripe_sub.id 

    current_user.subscription.save! 

    update_user_to_premium 
    flash[:success] = "Thank you for your subscription!" 

    redirect_to root_path 

    # Handle exceptions 
    rescue Stripe::CardError => e 
    flash[:error] = e.message 
    redirect_to new_subscriptions_path 
    end 


    def downgrade 

    customer = Stripe::Customer.retrieve(current_user.stripe_customer_id) 
    customer.subscriptions.retrieve(current_user.subid).delete 

    downgrade_user_to_standard 
    flash[:success] = "Sorry to see you go." 
    redirect_to user_path(current_user) 

    end 
end 

ApplitionController:

class ApplicationController < ActionController::Base 
def update_user_to_premium 
    current_user.update_attributes(role: "premium") 
    end 

    def downgrade_user_to_standard 
    current_user.update_attributes(role: "standard") 
    end 
end 

配置/初始化/ stripe.rb:

Rails.configuration.stripe = { 
    publishable_key: ENV['STRIPE_PUBLISHABLE_KEY'], 
    secret_key: ENV['STRIPE_SECRET_KEY'] 
} 

# Set our app-stored secret key with Stripe 
Stripe.api_key = Rails.configuration.stripe[:secret_key] 

任何幫助將不勝感激!

更新: 感謝來自stacksonstacks幫助,我需要的是在 'current_user.subid = stripe_sub.id' 聲稱 'subscription.user = CURRENT_USER',然後調用以「訂閱= current_user.subscription訂閱ID 「在降級方法。現在訂閱取消作品!

回答

2

看來current_user.subid回報nil在這條線:

customer.subscriptions.retrieve(current_user.subid).delete 

分配subidcurrent_user但你永遠不保存更改。 您只保存新創建的subscription

current_user.subid = stripe_sub.id 
current_user.subscription.save! 

如果您添加current_user.save!我認爲這將解決問題。

希望有幫助

+0

感謝您的回答!我確實添加了'current_user.save!'在我的create方法中'current_user.stripe_customer_id = customer.id'下,但我仍然遇到同樣的錯誤。 –

+0

你需要把它移動到這行下面'current_user.subid = stripe_sub.id',否則它不會保存新的'subid' – 2015-04-23 20:31:14

+0

我也試過將行移動到'current_user.subid = stripe_sub.id'下,但是同樣的問題依然存在。 我的想法是我應該在我的降級方法中的「customer.subscriptions.retrieve(current_user.subid).delete」下面添加「subscription.delete(或subscription.save!)」? –

相關問題