Ruby on rails 为支付条带订阅模型创建时接收GUID Null状态
在尝试创建用户后,我很难弄清楚为什么会收到{“guid”:null,“status”:“pending”,“error”:“Email不能为空”}。尽管如此,所有输入字段都是正确的。我不能越过这一点。我的服务器日志信息如下。你能帮我找出问题所在吗?最后,用户实际上被保存到数据库中,但我在创建一个之后马上得到一个空白屏幕…它不会重定向到目标控制器。我有一个与用户创建模型挂钩的邮件作业,该模型旨在向新用户发送电子邮件。在Stripe上,它说我没有活动订阅者,即使用户是创建的。我在下面添加了注册。我试图在操作逻辑时不破坏代码 Plan.rbRuby on rails 为支付条带订阅模型创建时接收GUID Null状态,ruby-on-rails,payola,Ruby On Rails,Payola,在尝试创建用户后,我很难弄清楚为什么会收到{“guid”:null,“status”:“pending”,“error”:“Email不能为空”}。尽管如此,所有输入字段都是正确的。我不能越过这一点。我的服务器日志信息如下。你能帮我找出问题所在吗?最后,用户实际上被保存到数据库中,但我在创建一个之后马上得到一个空白屏幕…它不会重定向到目标控制器。我有一个与用户创建模型挂钩的邮件作业,该模型旨在向新用户发送电子邮件。在Stripe上,它说我没有活动订阅者,即使用户是创建的。我在下面添加了注册。我试
class Plan < ApplicationRecord
include Payola::Plan
has_many :users
validates :stripe_id, inclusion: {in: Plan.pluck('DISTINCT stripe_id'),
message: 'not a valid subscription plan'}
def redirect_path(subscription)
'/'
end
end
注册\u controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
include Payola::StatusBehavior
before_action :cancel_subscription, only: [:destroy]
def new
build_resource({})
unless params[:plan].nil?
@plan = Plan.find_by!(stripe_id: params[:plan])
resource.plan = @plan
end
yield resource if block_given?
respond_with self.resource
end
def create
build_resource(sign_up_params)
plan = Plan.find_by!(id: params[:user][:plan_id].to_i)
resource.role = User.roles[plan.stripe_id] unless resource.admin?
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
subscribe
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
subscribe
end
else
clean_up_passwords resource
render json:
{error: resource.errors.full_messages.to_sentence},
status: 400
end
end
def change_plan
plan = Plan.find_by!(id: params[:user][:plan_id].to_i)
unless plan == current_user.plan
role = User.roles[plan.stripe_id]
if current_user.update_attributes!(plan: plan, role: role)
subscription = Payola::Subscription.find_by!(owner_id: current_user.id)
Payola::ChangeSubscriptionPlan.call(subscription, plan)
redirect_to edit_user_registration_path, :notice => "Plan changed."
else
flash[:alert] = 'Unable to change plan.'
build_resource
render :edit
end
end
end
private
def subscribe
return if resource.admin?
params[:plan] = current_user.plan
params[:stripeEmail] = current_user.email
subscription = Payola::CreateSubscription.call(params, current_user)
current_user.save
render_payola_status(subscription)
end
def sign_up_params
params.require(:user).permit(:email, :password, :password_confirmation, :plan_id, :terms_and_conditions, :full_name)
end
def cancel_subscription
subscription = Payola::Subscription.find_by!(owner_id: current_user.id, state: 'active')
Payola::CancelSubscription.call(subscription)
end
def update_card
@subscription = Subscription.find_by!(guid: params[:guid])
Payola::UpdateCard.call(@subscription, params[:stripeToken])
confirm_with_message("Card updated")
end
protected
def after_sign_up_path_for(resource)
authenticated_root_path(resource)
end
def after_update_path_for(resource)
user_path(resource)
end
end
答复是:
{"guid":null,"status":"pending","error":"Email can't be blank"}
当请求中未提交params[:stripeEmail]
时,可能会发生这种情况
Payloa的CreateSubscription
假设电子邮件作为params[:stripeEmail]
可用:
module Payola
class CreateSubscription
def self.call(params, owner=nil)
...
if params[:stripe_customer_id].present?
...
else
email = params[:stripeEmail]
end
sub = Payola::Subscription.new do |s|
s.plan = plan
s.email = email
...
end
...
end
end
end
尝试将stripeEmail
字段添加到注册表中
config.secret_key = Rails.application.secrets.stripe_api_key
config.publishable_key = Rails.application.secrets.stripe_publishable_key
Payola.subscribe 'customer.subscription.deleted' do |event|
sale = Sale.find_by(stripe_id: event.data.object.id)
user = User.find_by(email: sale.email)
UserMailer.expire_email(user).deliver
user.destroy
end
config.subscribe 'charge.refunded' do |event|
sale = Payola::Sale.find_by(stripe_id: event.data.object.id)
sale.refund! unless sale.refunded?
end
{"guid":null,"status":"pending","error":"Email can't be blank"}
module Payola
class CreateSubscription
def self.call(params, owner=nil)
...
if params[:stripe_customer_id].present?
...
else
email = params[:stripeEmail]
end
sub = Payola::Subscription.new do |s|
s.plan = plan
s.email = email
...
end
...
end
end
end