I am integrating stripe payments into my app. I switched out the Stripe default "Pay with Card" button for one I customized. After doing this, the test mode successfully pops-up and I can enter the test credentials. However, when I submit the test payment, and it is approved, it just stays on the same pin page rather than going to the charges page that I created for it.
Note: the app would transition just fine from stripe payment to the charges create.html.erb page up until I replaced the default stripe payment button with my new customized button.
Also, looking at developer tools I do note that it reads when I submit test payment:
Uncaught TypeError: Cannot read property 'submit' of null
I think this is referring to my code below where I submit the chargeForm.
Anyone encounter this issue before or know I can fix this? Many thanks!
app/views/pins/_details.html.erb
<%= form_tag charges_path, id: 'chargesForm' do %>
<script src=".js"></script>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<button id="btn-buy" type="button" class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-heart"></span> I want this!</button>
<script>
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
token: function(token, arg) {
document.getElementById("stripeToken").value = token.id;
document.getElementById("stripeEmail").value = token.email;
document.getElementById("chargeForm").submit();
}
});
document.getElementById('btn-buy').addEventListener('click', function(e) {
handler.open({
name: 'OMG! <%= @pin.manufacturer %>',
description: '<%= @pin.description %>',
amount: 1000
});
e.preventDefault();
})
</script>
<% end %>
app/controllers/charges_controller.rb
class ChargesController < ApplicationController
def create
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
end
app/views/charges/create.html.erb
You bought some Awesome stuff!
app/config/routes.rb
Rails.application.routes.draw do
resources :pins
resources :charges
devise_for :users
root "pins#index"
get "about" => "pages#about" #creates about_path
get "contact" => "pages#contact" #creates contact_path
get "auction" => "pages#auction" #creates auction_path
get "terms" => "pages#terms" #creates terms_path
post 'send_mail', to: 'contact#send_mail'
get 'contact', to: 'contact#show'
scope 'pins', controller: :pins do
scope '/:id' do
post 'bid', to: :bid
end
end
scope 'admin', controller: :admin do
scope 'pins' do
get '/:pin_id', to: :pin
end
end
end
I am integrating stripe payments into my app. I switched out the Stripe default "Pay with Card" button for one I customized. After doing this, the test mode successfully pops-up and I can enter the test credentials. However, when I submit the test payment, and it is approved, it just stays on the same pin page rather than going to the charges page that I created for it.
Note: the app would transition just fine from stripe payment to the charges create.html.erb page up until I replaced the default stripe payment button with my new customized button.
Also, looking at developer tools I do note that it reads when I submit test payment:
Uncaught TypeError: Cannot read property 'submit' of null
I think this is referring to my code below where I submit the chargeForm.
Anyone encounter this issue before or know I can fix this? Many thanks!
app/views/pins/_details.html.erb
<%= form_tag charges_path, id: 'chargesForm' do %>
<script src="https://checkout.stripe./checkout.js"></script>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<button id="btn-buy" type="button" class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-heart"></span> I want this!</button>
<script>
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
token: function(token, arg) {
document.getElementById("stripeToken").value = token.id;
document.getElementById("stripeEmail").value = token.email;
document.getElementById("chargeForm").submit();
}
});
document.getElementById('btn-buy').addEventListener('click', function(e) {
handler.open({
name: 'OMG! <%= @pin.manufacturer %>',
description: '<%= @pin.description %>',
amount: 1000
});
e.preventDefault();
})
</script>
<% end %>
app/controllers/charges_controller.rb
class ChargesController < ApplicationController
def create
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
end
app/views/charges/create.html.erb
You bought some Awesome stuff!
app/config/routes.rb
Rails.application.routes.draw do
resources :pins
resources :charges
devise_for :users
root "pins#index"
get "about" => "pages#about" #creates about_path
get "contact" => "pages#contact" #creates contact_path
get "auction" => "pages#auction" #creates auction_path
get "terms" => "pages#terms" #creates terms_path
post 'send_mail', to: 'contact#send_mail'
get 'contact', to: 'contact#show'
scope 'pins', controller: :pins do
scope '/:id' do
post 'bid', to: :bid
end
end
scope 'admin', controller: :admin do
scope 'pins' do
get '/:pin_id', to: :pin
end
end
end
Share
Improve this question
asked Jun 2, 2016 at 19:27
CodestudioCodestudio
5253 gold badges5 silver badges31 bronze badges
2 Answers
Reset to default 7Your Javascript code reads
document.getElementById("chargeForm").submit();
while your form name is chargesForm
<%= form_tag charges_path, id: 'chargesForm' do %>
I had a similar issue when in a page I had many forms for a delete action in a table. I was able to figure why I keep getting this error when I found out that the quotes(single or double quotes) must appear in the
document.getElementById("whatever id keep quotes ").submit();
and make sure it matches the form id.
e.g.
document.getElementById("'delform'.$key ").submit();
<form id="delform.$key" action="" ></form>