问题描述
这是我第一次使用 Stripe 和 Rails,现在我正在尝试允许高级用户取消他们的订阅.
This is my first time working with Stripe and Rails and now I am trying to allow premium users to cancel their subscriptions.
我可以使用我的代码将用户从标准级别升级到高级级别,但是当我尝试将高级用户降级到标准级别时遇到问题.
I can upgrade a user from standard level to premium level with my code, but I am having issues when I attempt to downgrade a premium user to the standard level.
我遵循了取消订阅"的 Stripe Ruby API 参考:https://stripe.com/docs/api?lang=ruby#cancel_subscription,但是当我单击取消订阅"按钮时出现此错误:
I have followed Stripe Ruby API References of "Cancel a subscription": https://stripe.com/docs/api?lang=ruby#cancel_subscription, but I got this error when I clicked the "cancel subscription" button:
NoMethodError - 未定义的方法 encoding' for nil:NilClass:/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/cgi/util.rb:7:in
escape'条纹 (1.21.0) lib/stripe/list_object.rb:19:in retrieve'app/controllers/subscriptions_controller.rb:55:in
downgrade'
NoMethodError - undefined method encoding' for nil:NilClass: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/cgi/util.rb:7:in
escape' stripe (1.21.0) lib/stripe/list_object.rb:19:in retrieve' app/controllers/subscriptions_controller.rb:55:in
downgrade'
我的 Rails 版本是 4.2.1.
My rails version is 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
应用控制器:
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
config/initializers/stripe.rb:
config/initializers/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",然后在降级方法中使用subscription = current_user.subscription"调用订阅 ID.现在订阅取消有效!
Update:Thanks for help from stacksonstacks, all I need is asserting 'subscription.user = current_user' under 'current_user.subid = stripe_sub.id', and then call subscription id with "subscription = current_user.subscription" in downgrade method. Now subscription cancelling works!
推荐答案
看来 current_user.subid
在这一行返回 nil
:
It seems current_user.subid
returns nil
on this line:
customer.subscriptions.retrieve(current_user.subid).delete
您为 current_user
分配了 subid
,但从未保存更改.您只保存新创建的订阅
.
You assign subid
for current_user
but you never save the changes.You only save the newly created subscription
.
current_user.subid = stripe_sub.id
current_user.subscription.save!
如果您添加 current_user.save!
我认为这将解决问题.
If you add current_user.save!
I think this will solve the problem.
希望有帮助
这篇关于如何修复此错误“未定义方法‘编码’为 nil:NilClassa"并取消订阅计划有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!