samedi 25 avril 2015

Stripe - Passing in parameter "Amount" from another table


So I am implementing the checkout.js Stripe script to pay for a "booking". I have the following in booking/show.html.erb

  <center>
  <b>Address:</b>
  <%= @booking.address %>
</p>

<p>
  <b>Catering:</b>
  <%= @booking.catering.foodtype %>
  <b>$</b>
    <%= @booking.catering.cost %>
</p>

<p>
  <b>Customer:</b>

  <%= @booking.customer.name %>
</p>

<p>
  <b>Eventtype:</b>
  <%= @booking.eventtype %>
</p>

<p>
  <b>Finishdatetime:</b>
  <%= @booking.finishdatetime %>
</p>

<p>
  <b>Marquee:</b>

   <%= @booking.marquee.name%>
   <b>$</b>
  <%= @booking.marquee.cost%>
</p>

<p>
  <b>Musician:</b>
  <%= @booking.musician.name %>
  <b>$</b>
  <%= @booking.musician.cost %>
</p>

<p>
  <b>Size:</b>
  <%= @booking.size %>
</p>

<p>
  <b>Startdatetime:</b>
  <%= @booking.startdatetime %>
</p>

<p>
  <b>Toilet:</b>
  <%= @booking.toilet.name %>
  <b>$</b>
   <%= @booking.toilet.cost %>
</p>

<p>
 <b>TOTAL COST:</b>
 <%=  @totalcost = @booking.toilet.cost + @booking.musician.cost + @booking.catering.cost  + @booking.marquee.cost %>

 </p>


 <p>
<a href="#" onclick="window.print();return false;">Print this invoice</a>
</p> 

<a href="http://localhost:3000/charges/new">Pay</a>

</center>

However in my charges controller, the amount is hard coded into the create method, like so

    class ChargesController < ApplicationController

    def new
end

def create
  # Amount in cents
  @amount = 500

  customer = Stripe::Customer.create(
    :email => params[:email],
    :card  => params[:stripeToken]
  )

  charge = Stripe::Charge.create(
    :customer    => customer.id,
    :amount      => @amount,
    :description => 'Rails Stripe customer',
    :currency    => 'eur'
  )

rescue Stripe::CardError => e
  flash[:error] = e.message
  redirect_to charges_path
end
end

I want to have it so that when I click "Pay" at the bottom of my show it brings me to the charges/new page where the amount is passed over. I'm not sure how to do this as the charges create method won't know for which booking instance to take the params from because it is just a direct href link to the charges/new page.

It is probably quite complicated but if someone could steer me in the right direction that would be great!


Aucun commentaire:

Enregistrer un commentaire