Javascript 无法验证条带支付的CSRF令牌[Rails 4]
我目前使用的是他们网站上提供给你的默认条带表单。在进行交易时,除了为用户处理适当的授权令牌之外,其他一切似乎都起作用。我将在下面发布所有方法和表单的代码 用户\u controller.rbJavascript 无法验证条带支付的CSRF令牌[Rails 4],javascript,ruby-on-rails,ruby,ruby-on-rails-4,stripe-payments,Javascript,Ruby On Rails,Ruby,Ruby On Rails 4,Stripe Payments,我目前使用的是他们网站上提供给你的默认条带表单。在进行交易时,除了为用户处理适当的授权令牌之外,其他一切似乎都起作用。我将在下面发布所有方法和表单的代码 用户\u controller.rb def info @subscription = current_user.subscription end def charge token = params["stripeToken"] customer = Stripe::Customer.create(
def info
@subscription = current_user.subscription
end
def charge
token = params["stripeToken"]
customer = Stripe::Customer.create(
source: token,
plan: 'gbsubscriptionlevel1',
email: current_user.email
)
current_user.subscription.stripe_user_id = customer.id
current_user.subscription.active = true
current_user.subscription.save
redirect_to users_info_path
end
routes.rb
get '/users/info', to: 'users#info'
post '/users/charge', to: 'users#charge'
_stripe_form.html.erb
<div class="container">
<form action="/users/charge" method="POST" id="payment-form">
<span class="payment-errors"></span>
<div class="row">
<div class="col-xs-4">
<label>
<span>Card Number</span>
<input class="form-control" type="text" size="20" data-stripe="number"/>
</label>
</div>
</div>
<div class="row">
<div class="col-xs-1">
<label>
<span>CVC</span>
<input class="form-control" type="text" size="4" data-stripe="cvc"/>
</label>
</div>
</div>
<div class="row">
<div class="col-xs-1">
<label>MM</label>
<input class="form-control" type="text" size="2" data-stripe="exp-month" placeholder="01"/>
</div>
<div class="col-xs-1">
<label>YYYY</label>
<input class="form-control" type="text" size="3" data-stripe="exp-year" placeholder="2020"/>
</div>
</div>
<div class="row">
<div class="col-xs-1">
<br/>
<button class="btn btn-primary-outline" type="submit">Create Subscription</button>
</div>
</div>
</form>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
// This identifies your website in the createToken call below
Stripe.setPublishableKey('pk_test_tmBNNUvHTmtWXLhSL1q647iH');
//
function stripeResponseHandler(status, response) {
var $form = $('#payment-form');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
// response contains id and card, which contains additional card details
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and submit
$form.get(0).submit();
}
}
jQuery(function ($) {
$('#payment-form').submit(function (event) {
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
});
</script>
</div>
这可能不是最好的解决方案,但您可以忽略错误
protect_from_forgery with: :null_session
放置在控制器中
更好的解决方案是使用rails form helper构建表单。这可能不是最好的解决方案,但您可以使用忽略错误
protect_from_forgery with: :null_session
放置在控制器中
更好的解决方案是使用rails表单助手构建表单。本质上,表单标记不会自动生成CSRF令牌,除非明确要求这样做,但是表单标记也不会自动生成CSRF令牌,因为它们会将其切碎 我通过在表单中添加一个隐藏的输入来解决这个问题
<input type="hidden" value="<%= form_authenticity_token() %>"name="authenticity_token"/>
下面是一个例子:
<%= form_tag("/payments/create", remote: true) do %>
<%= render partial: "shared/stripe_checkout_button" %>
<%= hidden_field_tag(:product_id, @product.id) %>
<input type="hidden" value="<%= form_authenticity_token() %>"name="authenticity_token"/>
<% end %>
他们阻止自动生成CSRF令牌的主要原因与人员片段缓存有关,因为从缓存中提取表单时,真实性令牌在后续请求中会出错。本质上,表单标签不会自动生成CSRF令牌,除非明确要求这样做,然而,他们也没有为tag's形成_,因为他们把它切碎了 我通过在表单中添加一个隐藏的输入来解决这个问题
<input type="hidden" value="<%= form_authenticity_token() %>"name="authenticity_token"/>
下面是一个例子:
<%= form_tag("/payments/create", remote: true) do %>
<%= render partial: "shared/stripe_checkout_button" %>
<%= hidden_field_tag(:product_id, @product.id) %>
<input type="hidden" value="<%= form_authenticity_token() %>"name="authenticity_token"/>
<% end %>
他们阻止自动生成CSRF令牌的主要原因与人员片段缓存有关,因为从缓存中提取表单时,真实性令牌在后续请求中可能会出错。您应该使用rails表单帮助程序创建表单,以便表单插入必要的CSRF令牌字段您应该使用rails表单助手创建表单,以便它插入必要的CSRF令牌fieldsOk。我有一种感觉,传统的html表单在没有Rails支持的情况下可能会彻底崩溃。当您可以添加另一行代码时,执行这一行代码有什么好处?所有的事情都是一样的,这意味着表单已经构建好了,您只需要添加一些东西,使用
authenticity\u token
Ok来实现解决方案可能是更好的主意。我有一种感觉,传统的html表单在没有Rails支持的情况下可能会彻底崩溃。当您可以添加另一行代码时,执行这一行代码有什么好处?一切都是平等的,这意味着表单已经构建好了,您只需添加一些东西,这可能是使用authenticity\u token实现解决方案的更好主意