条纹支付适用于 LocalHost 但不适用于 Heroku
Stripe Payment Works on LocalHost but Does not work on Heroku
希望一切都好
我对 rails 上的 ruby 中的条带付款为何在我的本地主机(即 c9.io 帐户)上工作感到困惑,但是当我在 Heroku 中部署我的代码时,它给了我这个错误:
Cannot charge a customer that has no active card
我的 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.erbheader部分
<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
如果需要更多信息,请提出。
我认为在 Heroku 上进行测试时,您会为新客户创建费用,而该客户没有任何默认卡处于活动状态。请在 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"
)
card = customer.sources.create({:source => token})
charge = Stripe::Charge.create(
:amount => (@listing.price * 100).floor,
:description => 'charge.create',
:currency => "usd",
:customer => customer.id,
:source => card.id
)
flash[:notice] = "Thanks for ordering!"
rescue Stripe::CardError => e
flash[:danger] = e.message
end
所以这个错误意味着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 控制器中添加一些日志,以检查令牌是否已成功创建和提交。
我认为您的错误很简单,就是您创建了一个 Stripe 客户帐户并将令牌传递给客户,但没有将令牌传递给实际的费用。在这两种情况下都需要这样做。 Stripe 的许多用例可以用于创建订阅或稍后处理付款,这就是 Customer Create 将令牌记录在案的原因。但是要创建收费,您还需要这样做。所以我会编辑代码以在 Charge 创建中包含令牌,如下所示:
2003 年 11 月编辑
实际上,我继续将代码还原为您拥有的代码。通过阅读 API 的更多内容,我认为您可以使它以这种方式正常工作。也就是说,如果您打算创建一个可以再次收费的客户 later.The 我会看的方向是它在本地工作而不是在 Heroku 上工作。我认为这与 Stripe 代码无关,而与生产变更有关。查看 ENV 变量,看看是否为开发和生产设置了它。但不仅如此,我会预编译你的资产,因为 Heroku 往往会遇到 CSS 或 jQuery 的问题,至少在我从开发到生产时对我来说是这样。 运行 RAILS_ENV=production bundle exec rake assets:precompile
这应该可以解决问题,然后推送到 master。
参见: https://devcenter.heroku.com/articles/rails-asset-pipeline
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
如果您只是想向他们收费而不是在 Stripe 中保留信息,您可以使用:
token = params[:stripeToken]
begin
charge = Stripe::Charge.create(
:amount => (@listing.price * 100).floor,
:currency => "usd",
:source => token,
:description => "Example charge"
)
flash[:notice] = "Thanks for ordering!"
rescue Stripe::CardError => e
flash[:danger] = e.message
end
编辑 11/07
好的,所以在查看实际的回购协议时,我认为您遇到了问题,因为您首先没有必要的 javascript 调用来创建令牌。我会在这里引用 Stripe Api:
https://stripe.com/docs/stripe.js#collecting-card-details
并将其插入到您的 orders.js 文件中,但这取决于在您用于创建订单的表单中使用 类 命名的各种输入字段。我希望这现在可以帮助您指明正确的方向。据我所知,其他一切看起来都应该不错。
Stripe.card.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
希望一切都好 我对 rails 上的 ruby 中的条带付款为何在我的本地主机(即 c9.io 帐户)上工作感到困惑,但是当我在 Heroku 中部署我的代码时,它给了我这个错误:
Cannot charge a customer that has no active card
我的 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.erbheader部分
<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
我认为在 Heroku 上进行测试时,您会为新客户创建费用,而该客户没有任何默认卡处于活动状态。请在 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"
)
card = customer.sources.create({:source => token})
charge = Stripe::Charge.create(
:amount => (@listing.price * 100).floor,
:description => 'charge.create',
:currency => "usd",
:customer => customer.id,
:source => card.id
)
flash[:notice] = "Thanks for ordering!"
rescue Stripe::CardError => e
flash[:danger] = e.message
end
所以这个错误意味着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 控制器中添加一些日志,以检查令牌是否已成功创建和提交。
我认为您的错误很简单,就是您创建了一个 Stripe 客户帐户并将令牌传递给客户,但没有将令牌传递给实际的费用。在这两种情况下都需要这样做。 Stripe 的许多用例可以用于创建订阅或稍后处理付款,这就是 Customer Create 将令牌记录在案的原因。但是要创建收费,您还需要这样做。所以我会编辑代码以在 Charge 创建中包含令牌,如下所示:
2003 年 11 月编辑
实际上,我继续将代码还原为您拥有的代码。通过阅读 API 的更多内容,我认为您可以使它以这种方式正常工作。也就是说,如果您打算创建一个可以再次收费的客户 later.The 我会看的方向是它在本地工作而不是在 Heroku 上工作。我认为这与 Stripe 代码无关,而与生产变更有关。查看 ENV 变量,看看是否为开发和生产设置了它。但不仅如此,我会预编译你的资产,因为 Heroku 往往会遇到 CSS 或 jQuery 的问题,至少在我从开发到生产时对我来说是这样。 运行 RAILS_ENV=production bundle exec rake assets:precompile
这应该可以解决问题,然后推送到 master。
参见: https://devcenter.heroku.com/articles/rails-asset-pipeline
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
如果您只是想向他们收费而不是在 Stripe 中保留信息,您可以使用:
token = params[:stripeToken]
begin
charge = Stripe::Charge.create(
:amount => (@listing.price * 100).floor,
:currency => "usd",
:source => token,
:description => "Example charge"
)
flash[:notice] = "Thanks for ordering!"
rescue Stripe::CardError => e
flash[:danger] = e.message
end
编辑 11/07 好的,所以在查看实际的回购协议时,我认为您遇到了问题,因为您首先没有必要的 javascript 调用来创建令牌。我会在这里引用 Stripe Api: https://stripe.com/docs/stripe.js#collecting-card-details 并将其插入到您的 orders.js 文件中,但这取决于在您用于创建订单的表单中使用 类 命名的各种输入字段。我希望这现在可以帮助您指明正确的方向。据我所知,其他一切看起来都应该不错。
Stripe.card.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);