【发布时间】:2014-01-10 23:47:34
【问题描述】:
我正在尝试使用 Bootstrap 3 将 Stripe 放入我在 Rails 4 中构建的 Web 应用程序中。我已经关注了 Stripe 网站 (https://stripe.com/docs/tutorials/forms) 上的文档,但在创建令牌时遇到了问题。 我的 Stripe 日志显示没有创建令牌,所以我可能是我的 JS/表单导致了问题。
我的表格: _new.html.erb
<form action="/charges" method="POST" id="payment-form">
<span class="payment-errors"></span>
<div class="form-group left-inner-addon ">
<i class="glyphicon glyphicon-user"></i>
<input name="sender_name" type="text" class="form-control" placeholder="Sender's Name"/>
</div>
<div class="form-group left-inner-addon">
<i class="glyphicon glyphicon-send"></i>
<input name="recipient_email" type="email" class="form-control" placeholder="Recipient's Email"/>
</div>
<div class="form-group left-inner-addon">
<i class="glyphicon glyphicon-pencil"></i>
<input name="recipient_msg" type="text" class="form-control" placeholder="Message to Recipient"/>
</div>
<div class="form-group left-inner-addon ">
<i class="glyphicon glyphicon-credit-card"></i>
<input type="text" class="form-control" placeholder="Credit Card Number" data-stripe="number"/>
</div>
<div class="form-group">
<div class="row">
<div class="col-xs-4 left-inner-addon ">
<i class="glyphicon glyphicon-calendar"></i>
<input type="text" class="form-control" placeholder="MM " data-stripe="exp-month"/>
</div>
<div class="col-xs-5 left-inner-addon ">
<i class="glyphicon glyphicon-calendar"></i>
<input type="text" class="form-control" placeholder="YYYY" data-stripe="exp-year"/>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-xs-5 left-inner-addon ">
<i class="glyphicon glyphicon-ok-circle"></i>
<input type="text" class="form-control" placeholder="CVC" data-stripe="cvc"/>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Purchase</button>
</form>
与页面上呈现的条带关联的 JS/Jquery 包含在我的 _stripe.html.erb 部分中,该部分被调用到我的 _head.html.erb 部分中。 _stripe.html.erb
<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_gGPOa1BlZvxdjnlnvJayZMzQ');
var stripeResponseHandler = function(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 {
// 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" />').val(token));
// and re-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>
我的收费控制器使用文档中的代码 charges_controller.rb
class ChargesController < ApplicationController
def new
end
def create
# Set your secret key: remember to change this to your live secret key in production
# See your keys here https://manage.stripe.com/account
Stripe.api_key = "SECRET_KEY"
# Get the credit card details submitted by the form
token = params[:stripeToken]
# Create a Customer
customer = Stripe::Customer.create(
:card => token,
:description => "payinguser@example.com"
)
puts customer
# Charge the Customer instead of the card
Stripe::Charge.create(
:amount => 1000, # in cents
:currency => "cad",
:customer => customer.id
)
# Save the customer ID in your database so you can use it later
save_stripe_customer_id(user, customer.id)
# Later...
customer_id = get_stripe_customer_id(user)
Stripe::Charge.create(
:amount => 1500, # $15.00 this time
:currency => "cad",
:customer => customer_id
)
end
private
def charges_params
params.require(charges).permit(:sender_msg, :recipient_email, :recipient_msg, :stripeToken)
end
end
当我将客户对象打印到控制台时,在我点击应用程序中的提交按钮后,它会打印:
{"id":"cus_3AahFG5MiJMbc9","object":"customer","created":1387731342,"livemode":false,"description":"payinguser@example.com","email":null,"delinquent":false,"metadata":{},"subscription":null,"discount":null,"account_balance":0,"cards":{"object":"list","count":0,"url":"/v1/customers/cus_3AahFG5MiJMbc9/cards","data":[]},"default_card":null}
条带状态和响应 在我的条带日志中,我看到 v1/charges 的 402 POST 和 v1/customers 的 200 POST
402 响应正文:
error:
message: "Cannot charge a customer that has no active card"
type: "card_error"
param: "card"
code: "missing"
200 响应正文:
object: "customer"
created: 1387668402
id: cus_3AJmiyuxLK9rTw
livemode: false
description: "payinguser@example.com"
email: null
delinquent: false
metadata:
subscription: null
discount: null
account_balance: 0
cards:
object: "list"
count: 0
url: "/v1/customers/cus_3AJmiyuxLK9rTw/cards"
data:
default_card: null
我试图找出我的代码没有创建令牌的原因。 谢谢,
【问题讨论】:
-
stripeResponseHandler中status和response的值是多少? -
你确定你的密钥是
pk_test_gGPOa0BlFvxdjnlnvJayZMzQ(也不建议在任何地方发布你的公钥、私钥或任何与Stripe相关的密钥,是的,这是一个“测试”,但是仍然...例如我的是pk_NGv0HjI51YaznqbWIt4qkpI1cNg82(我更改了几个字母使其无效),我没有那个 test 值 -
@pdoherty926 刚刚发布了 402 和 200 响应。来自控制器的详细信息正在传递,而不是来自表单的数据?
-
@nrathaus 感谢您的提醒,我已经编辑了密钥中的几个字符。我的 API 密钥已在其中测试“测试”密钥,并存在于“实时”密钥中。
-
@Questifer 你解决过这个问题吗?有同样的问题
标签: ruby-on-rails ruby-on-rails-4 stripe-payments