Reputation: 183
when I attempt to run my code, I got the error above. i don't understand why I get this, everything seems clear to me..
Here's my controller's create action :
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
my form :
<%= 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="[email protected]" 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>
my layouts/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>
I saw so many resources on this topic, but none helped me. Any idea ?
EDIT :
routes.rb :
post '/payer-la-commande', to: 'transactions#create', as: :pay
EDIT 2
error :
[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'
from stripe logs, Parsed Request POST Body :
amount: "100"
description: "Rails Stripe customer"
currency: "eur"
EDIT 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
Upvotes: 1
Views: 616
Reputation: 7167
params[:stripeToken]
is blank because you are missing the javascript that sends the form to stripe, gets a token value, puts it on a hidden field and sends that to the server (rails) see https://stripe.com/docs/tutorials/forms#create-a-single-use-token for additional information.
EDIT: Once you have the token, you have to send it to the server using something like https://stripe.com/docs/tutorials/forms#sending-the-form-to-your-server (same document than before, just read it entirely)
Upvotes: 2