I am using Stripe Checkout and I would like to the user to set how much he/she wants to pay. I would like to pass variables from the "new" to "create" controller, specifically the donation amount. How do I pass the @amount variable from "new" to "create" using the Stripe template. In my controller I have:
def new
@amount = 10
@amount_in_cents = @amount * 100
end
def create
# Amount in cents...HOW DO I GET THIS TO BE PULLED FROM 'NEW'
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => 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 new_charge_path
end
My view for new.html.erb:
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $<%= @amount %></span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="Your donation of $<%= @amount %>"
data-amount="<%= @amount_in_cents %>"
data-email = "test@gmail.com"
data-locale="auto"></script>
<% end %>
View for create.html.erb
<h2>Thanks, you paid <strong>$5.00</strong>!</h2>
<%= @amount %>