Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3: Instance variable not working as charge amount for Stripe?
    primarykey
    data
    text
    <p>I'm adding Stripe to my Rails 3 app to accept credit card payments, and I'm trying to use an instance variable, "@total", for the amount to be charged to the customers. </p> <p>Here's what I currently have in my model:</p> <pre><code>class Video &lt; ActiveRecord::Base attr_accessible :age, :category, :description, :ethnicity, :gender, :genre, :interest, :length, :panda_video_id, :size, :tagline, :title, :video_id, :stripe_card_token attr_writer :current_step attr_accessor :stripe_card_token serialize :size serialize :age serialize :gender serialize :ethnicity has_many :audiences, :dependent =&gt; :destroy accepts_nested_attributes_for :audiences, :allow_destroy =&gt; true #validates_presence_of :panda_video_id, :if =&gt; lambda { |o| o.current_step == "upload" } def panda_video @original_video ||= Panda::Video.find(panda_video_id) end def save_with_payment if valid? charge = Stripe::Charge.create( amount: @total, currency: "usd", card: stripe_card_token ) end rescue Stripe::InvalidRequestError =&gt; e logger.error "Stripe error while creating charge: #{e.message}" errors.add :base, "There was a problem with your credit card." false end def current_step @current_step || steps.first end def steps %w[upload info audience review] end def next_step self.current_step = steps[steps.index(current_step)+1] end def previous_step self.current_step = steps[steps.index(current_step)-1] end def first_step? current_step == steps.first end def last_step? current_step == steps.last end def all_valid? steps.all? do |step| self.current_step = step valid? end end end </code></pre> <p>I contacted Stripe support, and they said that the instance variable should be working here. So, I suspect the problem lies with my controller:</p> <pre><code>class VideosController &lt; ApplicationController def index @videos = Video.all end def show @video = Video.find(params[:id]) @original_video = @video.panda_video @h264_encoding = @original_video.encodings["h264"] end def new session[:video_params] ||= {} @video = Video.new(session[:video_params]) @video.current_step = session[:video_step] end def create session[:video_params].deep_merge!(params[:video]) if params[:video] #Save total value and audience form to session. @total = session[:video_params]["size"].collect(&amp;:to_i).sum - 10 if session[:video_params]["size"] @audiences = session[:video_params].slice("size", "gender", "age", "ethnicity").to_json @video = Video.new(session[:video_params]) @video.current_step = session[:video_step] if @video.valid? if params[:back_button] @video.previous_step elsif @video.last_step? @video.save if @video.all_valid? &amp;&amp; @video.save_with_payment else @video.next_step end session[:video_step] = @video.current_step end if @video.new_record? render "new" else session[:video_step] = session[:video_params] = nil flash[:notice] = "Video saved" redirect_to @video end end def edit @video = Video.find(params[:id]) end def update @video = Video.find(params[:id]) if @video.update_attributes(params[:video]) redirect_to @video, :notice =&gt; "Successfully updated video." else render :action =&gt; 'edit' end end def destroy @video = Video.find(params[:id]) @video.destroy redirect_to videos_url, :notice =&gt; "Successfully destroyed video." end end </code></pre> <p>My rails app is a multi-step form, where all the data is stored in a session. @total sums up the input values in the audience step of the form, and then that @total number shows up in the final step, where the customer enters their payment info. The @total number currently shows up on this page, but for some reason Stripe can't process it, since I keep getting the following error: </p> <pre><code>Stripe error while creating charge: Missing required param: amount </code></pre> <p>What am I doing wrong here?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload