之前已经问过这个问题但是在任何一个链接上都没有明显的解决方案,并且在尝试链接中提供的所有方法后问题仍然存在。我正在尝试生成一个条带订阅计划,每次我完成该过程时都会收到错误,因为该客户没有附加付款来源。"正如您在屏幕截图中看到的那样,stripe_card_token也会生成,我也有来自JS的console.log标记,并且也会生成该标记。我附上了所有相关的代码文件。
此项目的Github链接:https://github.com/arpit016/picture-app
Stripeerror截图
user.rb文件
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :plan
attr_accessor :stripe_card_token
def save_with_payment
if valid?
customer = Stripe::Customer.create(
:source => stripe_card_token,
:email => email,
:plan => plan_id
)
self.stripe_customer_token = customer.id
save!
end
end
end
users.js文件
$(document).ready(function() {
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'));
$('#form-submit-btn').click(function(event) {
event.preventDefault();
$('input[type=submit]').prop('disabled', true);
var error = false;
if (!error) {
Stripe.card.createToken($('#new_user'), stripeResponseHandler);
}
return false;
}); //form submission
function stripeResponseHandler(status, response){
if (status == 200){
//Get a reference to the form
var f = $('#new_user');
//Get the token from the response
var token = response.id;
console.log("Token:" + token);
//Add the token to the form
f.append('<input type="hidden" name="user[stripe_card_token]" value="' + token + '" />');
console.log(f)
//Submit the form
f.get(0).submit();
}
else {
$('#stripe_error').text(response.error.message).show();
$('input[type=submit]').attr('disabled', false)
}
}
});
设计修改注册控制器
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :select_plan, only: [:new]
def create
super do |resource|
if params[:plan]
resource.plan_id = params[:plan]
if resource.plan_id == 2
resource.save_with_payment
else
resource.save
end
end
end
end
def select_plan
unless params[:plan] && (params[:plan] == '1' || params[:plan] == '2')
flash[:warning] = "Please select a valid membership"
redirect_to root_url
end
end
end
答案 0 :(得分:0)
想出来。一个铁杆菜鸟错误。我没有更新我的application_controller.rb文件,该文件应该包含以下的设计行:
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:stripe_card_token])
end