希望一切都好
我很困惑为什么我在 ruby on rails 中的 strip 支付可以在我的本地主机上运行,它是 c9.io 帐户,但是当我在 Heroku 中部署我的代码时,它给了我这个错误:
我的 orders.coffee 文件:
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
payment.setupForm()
payment =
setupForm: ->
$('#new_order').submit ->
$('input[type=submit]').attr('disabled', true)
obj = Stripe.card.createToken($('#new_order'), payment.handleStripeResponse)
alert(JSON.stringify(obj));
handleStripeResponse: (status, response) ->
if status == 200
$('#new_order').append($('<input type="hidden" name="stripeToken" />').val(response.id))
$('#new_order')[0].submit()
else
$('#stripe_error').text(response.error.message).show()
$('input[type=submit]').attr('disabled', false)
从本地主机中的我的 orders.coffee 出来:
我的 application.html.erb 标题部分
<head>
<title>Estydemo</title>
<%= stylesheet_link_tag 'application', media: 'all'%>
<%= javascript_include_tag 'https://js.stripe.com/v2/', type: 'text/javascript' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
<%= tag :meta, :name=> 'stripe-key', :content => STRIPE_PUBLIC_KEY %>
</head>
我的 orders_controller.rb 条纹部分:
Stripe.api_key = ENV["stripe_api_key"]
#flash[:notice] = Stripe.api_key
#puts "stripe api key is " + Stripe.api_key
token = params[:stripeToken]
begin
customer = Stripe::Customer.create(
:source => token,
:description => "Customer.create"
)
charge = Stripe::Charge.create(
:amount => (@listing.price * 100).floor,
:description => 'charge.create',
:currency => "usd",
:customer => customer.id
)
flash[:notice] = "Thanks for ordering!"
rescue Stripe::CardError => e
flash[:danger] = e.message
end
我的 application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
我从 heroku 获得的示例的条纹仪表板
谁能告诉我为什么我有这么奇怪的问题?
左边一个来自heroku,右边一个来自localhost
如果需要更多信息,请询问。
最佳答案
所以这个错误意味着 charge creation request ( Stripe::Charge.create(...)
) 失败,因为客户没有支付来源。
这反过来意味着当你 created the customer :
customer = Stripe::Customer.create(
:source => token,
:description => "Customer.create"
)
token
必须是 nil
或空字符串,因此没有 source
参数被发送到 Stripe。您可以通过查看 Stripe 仪表板中客户创建请求的日志条目来检查这一点。这反过来意味着
params[:stripeToken]
本身就是 nil
或一个空字符串。不幸的是,我不确定为什么会这样。我建议在 CoffeeScript 文件的
stripeResponseHandler
回调和 Rails Controller 中添加一些日志,以检查 token 是否已成功创建和提交。