当我尝试运行我的代码时,我收到了上面的错误。我不明白为什么我这样做,一切似乎都清楚了。
这是我的控制器的创建动作:
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'
来自条带日志,Parsed Request POST Body:
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
答案 0 :(得分:2)
params[:stripeToken]
是空白的,因为您缺少将表单发送到条带的javascript,获取标记值,将其放在隐藏字段上并将其发送到服务器(rails),请参阅https://stripe.com/docs/tutorials/forms#create-a-single-use-token其他信息。
编辑:获得令牌后,您必须使用类似https://stripe.com/docs/tutorials/forms#sending-the-form-to-your-server的内容将其发送到服务器(与以前相同的文档,只需完全阅读)