我正在我的 Rails 应用程序中实现 Stripe,当我加载输入表单时出现 Invalid Token 错误 - 我还没有提交客户数据。我主要遵循http://railscasts.com/episodes/288-billing-with-stripe教程。我做了一些修改,因为它有些不完整。
books/show.html.erb 是我链接到表单的页面:
<b>Title:</b> <%= @book.title %> </p>
<b>Author:</b> <% authorid = @book.author %></p>
<%= @book.id %>
<%= link_to "Buy Now", new_purchase_path(:book_id => @book.id) %>
purchase/new.html.erb 是用户填写信息的地方。加载时,我收到 Invalid Token 错误:
<%= form_for @purchase do |f| %>
<% if @purchase.errors.any? %>
<%= pluralize(@purchase.errors.count, "error") %> prohibited this purchase from being saved.
<% @purchase.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
<% end %>
<%= f.hidden_field :stripe_card_token %>
<% if @purchase.stripe_card_token.present? %>
Credit card has been provided.
<% else %>
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil %><p>
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %><p>
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
<% end %>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
<%= f.submit "Purchase" %>
<% end %>
purchase.js.coffee 与教程中的几乎相同。我添加了一些警报。根据我的 Stripe 仪表板的状态是 402。这是一个 POST /v1/tokens 错误,响应正文是:
error:
type: "card_error"
message: "This card number looks invalid"
param: "number"
购买.js.coffee:
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
purchase.setupForm()
purchase =
setupForm: ->
$('#new_purchase').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
purchase.processCard()
false
else
true
processCard: ->
card =
number: $('#card_number').val()
cvc: $('#card_code').val()
expMonth: $('#card_month').val()
expYear: $('#card_year').val()
Stripe.createToken(card, purchase.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 200
alert('This token can still be charged.')
alert(response.id)
$('#purchase_stripe_card_token').val(response.id)
$('#new_purchase')[0].submit()
else
alert(response.error.message)
alert('The token was invalid, or has been used.')
$('#stripe_error').text(response.error.message)
$('input[type=submit]').attr('disabled', false)
我已经尝试了几个版本的 purchase.rb 模型,例如注释掉 Stripe::Charge 函数,但仍然得到 402 Token 错误。但是创建客户是成功的(代码 200)。
class Purchase < ActiveRecord::Base
attr_accessible :stripe_customer_token, :author_id, :book_id
attr_accessor :stripe_card_token
belongs_to :book
def save_with_payment
if valid?
customer = Stripe::Customer.create(
:description => "customer email",
:card => stripe_card_token
)
self.stripe_customer_token = customer.id
# charge = Stripe::Charge.create( - this code doesn't work either
# :amount => 1000,
# :currency => "usd",
# :card => stripe_card_token,
# :description => "book title"
# )
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
end
如果我取消注释 Stripe::Charge 代码,我得到的错误是:Stripe::CardError in PurchasesController#create 无法向没有活动卡的客户收费
而且,我的 purchase_controller.rb 中的 create 方法
def create
@purchase = Purchase.new(params[:purchase])
if @purchase.save_with_payment
redirect_to @purchase, :notice => "Thank you for purchasing this book!"
else
render :new
end
end
这是我在 purchase_controller.rb 中的新方法:
定义新
book = Book.find(params[:book_id]) @purchase = book.purchases.build
结尾
但是如果我在提交购买后点击返回按钮(返回购买/new.html.erb 页面),第二个“购买”将输入到我的数据库中,并且我的 Stripe 日志中该 POST 令牌的代码是 200 (经过)!!!
这是从 coffeescript 编译的 javascript:
(功能() {
var 购买;
jQuery(函数(){
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'));
return purchase.setupForm();
});
购买 = {
setupForm:函数(){
$('#new_purchase').submit(function() { return $('input[type=submit]').attr('disabled', true); }); if ($('#card_number').length) { purchase.processCard(); return false; } else { return true; }
},
进程卡:函数(){
var card; card = { number: $('#card_number').val(), cvc: $('#card_code').val(), expMonth: $('#card_month').val(), expYear: $('#card_year').val() }; return Stripe.createToken(card, purchase.handleStripeResponse);
},
handleStripeResponse:函数(状态,响应){
if (status === 200) { alert('This token can still be charged.'); alert(response.id); $('#purchase_stripe_card_token').val(response.id); return $('#new_purchase')[0].submit(); } else { alert(response.error.message); alert('The token was invalid, or has been used.'); $('#stripe_error').text(response.error.message); return $('input[type=submit]').attr('disabled', false); }
}
};
}).call(this);