我正在将Stripe集成到我的Rails应用程序中,当我尝试运行我的付款表单时,我的Stripe日志中出现以下错误:
{ “错误”:{ “type”:“invalid_request_error”, “消息”:“你必须提供有效的卡” }
使用此请求正文:
{ “计划”:“1”, “description”:“jjets718@yahoo.com” }
我意识到在请求正文中,当我在付款表单中提交信用卡信息时,我应该拥有Stripe给我的令牌。当我在控制器中保存付款时,我使用我在模型中定义的以下方法:
def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, plan: 1, card: stripeToken)
self.stripe_customer_token = customer.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
如果有人可以帮我弄清楚为什么在创建客户时Stripe令牌没有被提交给Stripe,以及我如何解决潜在的错误,那就太棒了。 My Stripe javascript生成一个名为'stripeToken'的Stripe标记;所以问题必须在于控制器如何获取令牌并创建客户。
<head>
<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<!-- jQuery is used only for this example; it isn't required to use Stripe -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
// this identifies your website in the createToken call below
Stripe.setPublishableKey('pk_gV4NDoU8ymr5md49FJeNb5E1lIAda');
function stripeResponseHandler(status, response) {
if (response.error) {
// re-enable the submit button
$('.submit-button').removeAttr("disabled");
// show the errors on the form
$(".payment-errors").html(response.error.message);
} else {
var form$ = $("#payment-form");
// token contains id, last4, and card type
var token = response['id'];
// insert the token into the form so it gets submitted to the server
form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
// and submit
form$.get(0).submit();
}
}
$(document).ready(function() {
$("#payment-form").submit(function(event) {
// disable the submit button to prevent repeated clicks
$('.submit-button').attr("disabled", "disabled");
// createToken returns immediately - the supplied callback submits the form if there are no errors
Stripe.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
return false; // submit from callback
});
});
if (window.location.protocol === 'file:') {
alert("stripe.js does not work when included in pages served over file:// URLs. Try serving this page over a webserver. Contact support@stripe.com if you need assistance.");
}
</script>
</head>