【问题标题】:Stripe Payment Works on LocalHost but Does not work on HerokuStripe Payment 在 LocalHost 上有效,但在 Heroku 上无效
【发布时间】:2017-03-14 01:39:03
【问题描述】:

希望一切都好 我很困惑为什么我在 ruby​​ on rails 中的条带支付在我的本地主机(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

我的应用程序.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 如果需要更多信息,请提出来。

【问题讨论】:

  • 您是否正在混合测试和生产 API 密钥?
  • 我不这么认为。你能改写你的问题吗?您认为还有什么问题?
  • 您可能有单独的 Stripe API 密钥用于您的测试/开发和生产环境,那么您在 Heroku 的任何地方都使用正确的密钥吗?
  • @muistooshort 是的,我检查了一切正常
  • @muistooshort 我将我的 github 帐户添加到我的个人资料中如果您想查看我的代码。 :)

标签: ruby-on-rails heroku stripe-payments


【解决方案1】:

所以这个错误意味着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 文件和 Rails 控制器的 stripeResponseHandler 回调中添加一些日志,以检查令牌是否已成功创建和提交。

【讨论】:

  • 你是对的。由于某种原因,在 heroku 中我没有得到 params[:stripeToken] 的任何价值?你认为 heroku 对咖啡脚本有问题吗?
  • 我不知道为什么会这样。您能否在附加隐藏的stripeToken 字段后但在提交表单之前在stripeResponseHandler 回调中添加警报,以确保stripeToken 确实包含令牌?
  • 我已将我的 github 帐户添加到我的个人资料中如果您想查看我的代码。 :)
【解决方案2】:

我认为在 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

【讨论】:

  • 我试过了,但我得到了这个错误,我不能使用源两次
  • 您还有什么建议吗?
  • 怎么和你聊天
  • 创建客户时尝试删除 :source => 令牌
  • 我已将我的 github 帐户添加到我的个人资料中如果您想查看我的代码。 :)
【解决方案3】:

我认为您的错误只是您正在创建一个 Stripe 客户帐户并将令牌传递给客户,但没有将令牌传递给实际的 Charge。在这两种情况下都需要这样做。 Stripe 的许多用例可以用于创建订阅或用于稍后处理付款,这就是为什么 Customer Create 会将令牌存档。但要创建费用,您还需要执行此操作。所以我会编辑代码以在 Charge 创建中包含令牌,如下所示:

编辑于 11 月 3 日 实际上,我继续将代码恢复为您所拥有的。通过阅读更多 API,我认为您可以让它以这种方式正常工作。也就是说,如果您打算创建一个可以稍后再次收费的客户。那么我会看的方向是它在本地工作但在 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);

【讨论】:

  • 当我闪烁[:alert] token 的值时,我什么也没得到。除非 token = params[:stripeToken] flash[:alert] = 'You did not submit the form correct' end always I get you didn't submit the form correct alert
  • 我按照你说的做了,但收到此错误客户 cus_x 没有 ID 为 tok_y 的链接源。
  • 抱歉我的回复晚了。拳头我有这个问题,Gem::LoadError: Specified 'postgresql' for database adapter,但 gem 没有加载。将gem 'pg' 添加到您的 Gemfile(并确保其版本为 ActiveRecord 要求的最低版本)。我将 gem 'pg' 放在全局中并且没有错误,但我不确定。我按照您的建议运行了资产管道,但我仍然遇到问题。如果您愿意,我愿意分享我的 github 以解决问题。
  • 我已将我的 github 帐户添加到我的个人资料中如果您想查看我的代码。 :)
  • 查看您的订单。我看到您在那里反复列出了 nil ,据我所知,这可能是问题的一部分,因为它没有获取在表单中输入的数据,而是将其传递为 nil。具体看卡号、卡号、select_month和select_year。您在文本字段中列出了 nil。看起来是表单错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 2020-11-04
  • 2022-07-24
  • 2013-01-25
相关资源
最近更新 更多