【问题标题】:Must provide source or customer. rails必须提供来源或客户。导轨
【发布时间】:2015-12-16 01:32:43
【问题描述】:

当我尝试运行我的代码时,我收到了上面的错误。我不明白为什么我会得到这个,一切对我来说似乎都很清楚..

这是我的控制器的创建操作:

def create
    @user = current_user
    @order = current_order
    @amount = @order.subtotal
    token = params[:stripeToken]

  charge = Stripe::Charge.create(
    :amount      => (@amount.to_i * 100),
    :description => 'Rails Stripe customer',
    :currency    => 'eur',
    :source      => token
  )

  redirect_to root_path

rescue Stripe::CardError => e
  flash[:error] = e.message
  render 'new'
end

我的表格:

<%= form_tag pay_path, html: { id: 'payment-form' }  do %>
<div class="row">
</div>
<div class="row">
  <div style="position: absolute; left: 20px">
    <label class="control-label" for="email">Email</label>
    <input type="email" name="email" id="email" placeholder="you@example.com" style="width: 25em"/>
  </div>
  <div style="position: absolute; left: 400px">
    <label class="control-label" for="number">Card Number</label>
    <input type="text" size="20" data-stripe="number" id="number" placeholder="**** **** **** ****" pattern="[\d ]*" style="width: 18em"/>
  </div>
</div>
<div class="row" style="margin-top: 65px">
  <div style="position: absolute; left: 20px">
    <label class="control-label" for="cvc">CVC</label>
    <input type="text" style="width: 3em" size="3" data-stripe="cvc" id="cvc" placeholder="***" pattern="\d*"/>
    <img id="card-image" src="/img/credit.png" style="height: 30px; padding-left: 10px; margin-top: -10px">
  </div>
  <div style="position: absolute; left: 150px">
    <label class="control-label">Exp (MM/YYYY)</label>
    <input style="width: 2em" type="text" size="2" id="exp-month" data-stripe="exp-month" placeholder="MM" pattern="\d*"/>
    <span> / </span>
    <input style="width: 3em" type="text" size="4" id="exp-year" data-stripe="exp-year" placeholder="YYYY" pattern="\d*"/>
  </div>
</div>
<div class="row" style="margin-top: 70px">
  <div class="price" style="position: absolute; left: 20px;"><%= @amount %></div>
  <div style="position: absolute; left: 400px">
    <button type="submit" class="btn btn-primary btn-large">Buy Now</button>
  </div>
</div>

我的布局/application.html.erb:

<script type="text/javascript" src="https://js.stripe.com/v2/">    </script>
<script type="text/javascript">
$(function(){
  Stripe.setPublishableKey('<%=    Rails.configuration.stripe[:publishable_key] %>');
});
</script>

我看到了很多关于这个主题的资源,但没有一个能帮助我。 任何想法 ?

编辑:

routes.rb:

post '/payer-la-commande', to: 'transactions#create',   as: :pay

编辑 2

错误:

[localhost] [127.0.0.1] [1f4eb06b-26d4-41] Processing by    TransactionsController#create as HTML
[localhost] [127.0.0.1] [1f4eb06b-26d4-41]   Parameters: {"utf8"=>"✓", "authenticity_token"=>"67gwUvgY1xxxxxxxxxxxmtqwHyHQs3TUoanH6EDUx7wkCMVQIdqcxEuQEjoSUc/AXThw==", "email"=>""}
[localhost] [127.0.0.1] [1f4eb06b-26d4-41]   User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2  ORDER BY `users`.`id` ASC LIMIT 1
[localhost] [127.0.0.1] [1f4eb06b-26d4-41]   Order Load (0.3ms)  SELECT  `orders`.* FROM `orders` WHERE `orders`.`id` = 2 LIMIT 1
[localhost] [127.0.0.1] [1f4eb06b-26d4-41]   OrderItem Load (1.1ms)  SELECT `order_items`.* FROM `order_items` WHERE `order_items`.`order_id` = 2
[localhost] [127.0.0.1] [1f4eb06b-26d4-41]   Product Load (0.4ms)  SELECT  `products`.* FROM `products` WHERE `products`.`active` = 1 AND `products`.`id` = 1 LIMIT 1
[localhost] [127.0.0.1] [1f4eb06b-26d4-41] Completed 500 Internal Server Error in 4197ms (ActiveRecord: 2.1ms)
[localhost] [127.0.0.1] [1f4eb06b-26d4-41] 
Stripe::InvalidRequestError (Must provide source or customer.):
  app/controllers/transactions_controller.rb:16:in `create'

来自条带日志,解析的请求 POST 正文:

amount: "100"
description: "Rails Stripe customer"
currency: "eur"

编辑 3:

POST http://localhost:3000/payer-la-commande 500 (Internal Server Error)
Navigated to http://localhost:3000/payer-la-commande
content.js:4 Uncaught TypeError: Cannot read property 'unique' of nullinit @ content.js:4(anonymous function) @ content.js:10
2content.js:4 Uncaught TypeError: Cannot read property 'unique' of nullinit @ content.js:4(anonymous function) @ VM658:1

【问题讨论】:

  • 调用该操作时params[:stripeToken] 的值是多少?这就是发送到源的值,它应该是 Stripe.js 令牌。
  • 我暂时没有价值

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


【解决方案1】:

params[:stripeToken] 是空白的,因为您缺少将表单发送到条带、获取令牌值、将其置于隐藏字段并将其发送到服务器 (rails) 的 javascript,请参阅 https://stripe.com/docs/tutorials/forms#create-a-single-use-token 了解更多信息。

编辑:获得令牌后,您必须使用 https://stripe.com/docs/tutorials/forms#sending-the-form-to-your-server 之类的方式将其发送到服务器(与以前相同的文档,只需完整阅读)

【讨论】:

  • 用您拥有的所有信息更新您的问题? stripeResponseHandler 被调用了吗?那么response.id (token) 的值是多少?您需要通过分享更多信息来为我们提供更多帮助。
  • 感谢您的回复,实际上,我得到的唯一错误是我添加到第二个编辑中的错误。如何用 pry 调出 js 错误?
  • 在您的浏览器控制台上(通过打开开发人员工具或右键单击任何元素和“检查”或类似内容可见)。类似于rails中的byebugdebugger,你可以在JS中使用“调试器”,但它只会在开发者工具打开时停止,或者干脆使用console.log(variable)输出东西到控制台。您应该快速阅读在浏览器上调试 javascript。使用您找到的所有信息继续编辑您的问题。
  • 我刚刚删除了所有内容并重写了它,它可以工作,谢谢你的帮助。