Mike Whitehead

Joined

1,040 Experience
0 Lessons Completed
1 Question Solved

Activity

So, when I add total_amount as a column in my bookings table I add the datatype as 'integer'? I have price and price_pennies as integers also but I wasn't sure whether this had to be as a float or decimal as there will be prices like £8.99 or £4.50 for example but you say I can change this in the view?

Hi Chris, I really appreciate such a detailed response. So, what your saying is I need to strip out a lot of the logic I'm using in my controller and placing this in my model? This is something which I've felt I needed to do but wasn't quite sure how to go about it.

If I add total_amount to my bookings schema, should I add a method in my model like -

def total_amount
@booking.quantity * @event.price
end

What I mean is, would this help resolve the specific issue surrounding one user booking more than one space on a specific event or does the logic you've outlined above clear that up?

I'm building an events app using Rails and Stripe to handle payments. I've used javascript for my booking page in order to allow a user to book and pay for multiple spaces rather than just one at a time. However, when I do a test Stripe payment it's only processing the cost for one space. So if an event costs £10 and I want to book 4 spaces, my booking page allows me to indicate this and shows a cost of £40 but the payment on my Stripe dashboard only shows £10.

How do I rectify this?

I have a bookings controller for my Stripe processing code -

def create
    # actually process the booking
    @event = Event.find(params[:event_id])
    @booking = @event.bookings.new(booking_params)
    @booking.user = current_user


    if @booking.save

        # CHARGE THE USER WHO'S BOOKED
        Stripe::Charge.create(amount: @event.price_pennies, currency: "gbp",
            card: @booking.stripe_token, description: "Booking number #{@booking.id}", items: [{quantity: @booking.quantity}])

        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

I'm sure this is all basic MVC stuff but I cannot find the solution. I think it has to do with the amount: @event.price_pennies and/or how to handle the items: quantity array but I'm not sure. I don't think its anything to do with my views code so haven't put this up on here.

This is my booking params code with quantity included. I don't have a column in my bookings schema table for total_amount - should I add this and change @event.price_pennies to @booking.total_amount ?

  private

def booking_params
    params.require(:booking).permit(:stripe_token, :quantity)
end
logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.