javascript - Rails Stripe Gem - page needs to be refreshed before payment will process -


i'm building events site using rails. i'm using stripe gem process payments. seems working fine other whenever want make payment (in test/development mode @ present) have refresh page , re-enter payment details before payment process. there's no error code coming happens every time.

what causing this? here's relevant code -

bookings_controller.rb

class bookingscontroller < applicationcontroller  before_action :authenticate_user!  def new     # booking form     # need find event we're making booking on     @event = event.find(params[:event_id])     # , because event "has_many :bookings"     @booking = @event.bookings.new     # person booking event?     @booking.user = current_user     #@booking.quantity = @booking.quantity     #@total_amount = @booking_quantity.to_f * @event_price.to_f  end  def create     # process booking     @event = event.find(params[:event_id])     @booking = @event.bookings.new(booking_params)     @booking.user = current_user     #@total_amount = @booking.quantity.to_f * @event.price.to_f      booking.transaction         @booking.save!         @event.reload         if @event.bookings.count > @event.number_of_spaces             flash[:warning] = "sorry, event booked."             raise activerecord::rollback, "event booked"         end      end      if @booking.save          # charge user who's booked         # #{} == puts variable string         stripe::charge.create(amount: @event.price_pennies, currency: "gbp",             card: @booking.stripe_token, description: "booking number #{@booking.id}")          flash[:success] = "your place on our event has been booked"         redirect_to event_path(@event)     else         flash[:error] = "payment unsuccessful"         render "new"     end      if @event.is_free?          @booking.save!         flash[:success] = "your place on our event has been booked"         redirect_to event_path(@event)     end end   #def total_amount     #@total_amount = @booking.quantity * @event.price #end  private  def booking_params     params.require(:booking).permit(:stripe_token, :quantity) end    end 

new.html.erb (bookings)

    <% if @event.is_free? %> <div class="col-md-6 col-md-offset-3" id="eventshow">   <div class="row">     <div class="panel panel-default">         <div class="panel-heading">             <h2>your booking confirmation</h2>         </div>                 <div class="panel-body">                          <h1>hi there</h1>                          <p>you have placed booking on <%= @event.title %></p>                          <p>your order number <%= @booking.id %></p>                          <p>we hope have wonderful time. enjoy!</p>                          <p>love mama knows best</p>                 </div>                           <div class="panel-footer">                     <%= link_to "home", root_path %>                   </div>     </div>   </div> </div>                                        <% else %>  <div class="col-md-6 col-md-offset-3" id="eventshow">   <div class="row">     <div class="panel panel-default">         <div class="panel-heading">             <h2>confirm booking</h2>         </div>              <div class="panel-body">                      <p>total amount<%= @event.price %></p>                 <%= simple_form_for [@event, @booking], id: "new_booking" |form| %>                       <span class="payment-errors"></span>                  <div class="form-row">                     <label>                       <span>card number</span>                       <input type="text" size="20" data-stripe="number"/>                     </label>                 </div>                  <div class="form-row">                   <label>                   <span>cvc</span>                   <input type="text" size="4" data-stripe="cvc"/>                   </label>                 </div>                  <div class="form-row">                     <label>                         <span>expiration (mm/yyyy)</span>                         <input type="text" size="2" data-stripe="exp-month"/>                     </label>                     <span> / </span>                     <input type="text" size="4" data-stripe="exp-year"/>                 </div>             </div>             <div class="panel-footer">                     <%= form.button :submit %>               </div>   <% end %> <% end %>        </div>   </div> </div>      <script type="text/javascript" src="https://js.stripe.com/v2/"></script>  <script type="text/javascript">   // identifies website in createtoken call below   stripe.setpublishablekey('<%= stripe_public_key %>');   // ...   var striperesponsehandler = function(status, response) {   var $form = $('#new_booking');   if (response.error) {     // show errors on form     $form.find('.payment-errors').text(response.error.message);     $form.find('button').prop('disabled', false);   } else {     // token contains id, last4, , card type     var token = response.id;     // insert token form gets submitted server     $form.append($('<input type="hidden" name="booking[stripe_token]" />').val(token));     // , re-submit     $form.get(0).submit();   } };  jquery(function($) {   $('#new_booking').submit(function(e) {     var $form = $(this);     // disable submit button prevent repeated clicks     $form.find('input[type=submit]').prop('disabled', true);     stripe.card.createtoken($form, striperesponsehandler);     // prevent form submitting default action     return false;   }); });   </script> 

here's output terminal when click on 'create booking' first time before refresh -

started "/events/23/bookings/new" ::1 @ 2016-07-28 16:09:14 +0100 processing bookingscontroller#new html   parameters: {"event_id"=>"23"}   user load (0.1ms)  select  "users".* "users" "users"."id" = ?  order "users"."id" asc limit 1  [["id", 2]]   event load (0.1ms)  select  "events".* "events" "events"."id" = ? limit 1  [["id", 23]]   rendered bookings/new.html.erb within layouts/application (4.3ms) completed 200 ok in 132ms (views: 128.6ms | activerecord: 0.2ms) 

i'm sure it's simple fix can't figure out. assistance appreciated.

i think in part:

@booking.save! @event.reload 

you use reload not creating event in moment, think don't need load event in way. booking event. try removing reload.

can copy , paste output of terminal when try make payment @ first time (without refreshing page)

some references: http://apidock.com/rails/activerecord/persistence/reload


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -