Rails - 如何正确实施付款/预订流程

时间:2016-10-04 11:56:05

标签: javascript ruby-on-rails ruby ruby-on-rails-4 stripe-payments

我正在使用rails构建一个事件应用程序,而我正在使用Stripe来收取我的付款。我花了一段时间,但我得到了付款。但是,当我尝试增加预订空间的数量时,系统只需支付一笔款项。因此,如果一个事件是每个“空间”10英镑,并且有人想要预订5个空格,无论付款形式是否说明正确的金额(本例中为50英镑),只收取10英镑。 我很确定这是一个MVC问题,但似乎无法发现如何解决它。在我的观点中,我有以下代码 -

bookings.new.html.erb

    <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 Your Booking</h2>
        </div>
                  <div class="calculate-total">
                              <p>
                                  Confirm number of spaces you wish to book here:
                                    <input type="number" placeholder="1"  min="1" value="1" class="num-spaces">
                              </p>
                                <p>
                                    Total Amount
                                    £<span class="total" data-unit-cost="<%= @event.price %>">0</span>
                                </p>
                          </div>





                <%= simple_form_for [@event, @booking], id: "new_booking" do |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">
    $('.calculate-total input').on('keyup change', calculateBookingPrice);

function calculateBookingPrice() {
  var unitCost = parseFloat($('.calculate-total .total').data('unit-cost')),
      numSpaces = parseInt($('.calculate-total .num-spaces').val()),
      total = (numSpaces * unitCost).toFixed(2);

  if (isNaN(total)) {
    total = 0;
  }

  $('.calculate-total span.total').text(total);
}

  $(document).ready(calculateBookingPrice)

</script>



<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

(code for stripe)

这在实际付款页面上正常工作,如下例所示 -

Payment form with quantity function

所以,对于这个例子,我使用了每个空间/预订1英镑的@ event.price。但是,如果我继续处理这个预订,而不是Stripe收集的3英镑,它只显示为1英镑。

我认为这可能与@ event.price是我在我的观点中表达的事实有关,但是,当我用@ booking.total_amount替换@ event.price时我只得到我的总金额付款表格(如上所示)显示为零,无论我如何处理数量函数,它都保持为零。

从视图代码中可以看出,我的数量函数是用一些javascript处理的。我是否遗漏了此功能中的某些内容以使其正常工作?

我的控制器中是否有正确的参数?这是我的控制器代码 -

bookings_controller.rb

     class BookingsController < ApplicationController

    before_action :authenticate_user!

    def new
        # booking form
        # I need to find the event that we're making a booking on
        @event = Event.find(params[:event_id])
        # and because the event "has_many :bookings"
        @booking = @event.bookings.new(quantity: params[:quantity])
        # which person is booking the event?
        @booking.user = current_user
        #@total_amount = @event.price * @booking.quantity


    end

    def create

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

            if 
                @booking.reserve
                flash[:success] = "Your place on our event has been booked"
                redirect_to event_path(@event)
            else
                flash[:error] = "Booking unsuccessful"
                render "new"
            end
    end


    private

    def booking_params
        params.require(:booking).permit(:stripe_token, :quantity, :event_id, :stripe_charge_id, :total_amount)
    end



end

这是我的带有验证的模型代码 -

Booking.rb

     class Booking < ActiveRecord::Base

    belongs_to :event
    belongs_to :user


    validates :total_amount, presence: true, numericality: { greater_than: 0 }
    validates :quantity, :total_amount, presence: true

    def reserve
        # Don't process this booking if it isn't valid
        self.valid?



                # Free events don't need to do anything special
                if event.is_free?
                save!

                # Paid events should charge the customer's card
                else

                    begin
                        self.total_amount = event.price * self.quantity
                        charge = Stripe::Charge.create(
                            amount: total_amount * 100,
                            currency: "gbp",
                            source: stripe_token, 
                            description: "Booking created for amount #{total_amount}")
                        self.stripe_charge_id = charge.id
                        save!
                    rescue Stripe::CardError => e
                    errors.add(:base, e.message)
                    false
                end
            end 

    end
end

这让我难以忍受了一段时间。我对Rails很陌生,所以要确保一个更有经验的眼睛可以发现我出错的地方。任何指导表示赞赏。

2 个答案:

答案 0 :(得分:1)

请注意,这不是逐行修复,而是在正确的方向上轻推。

你把所有东西塞进一组极其有限的模型和控制器中。

我明白了 - “太多课程”起初可能看起来很吓人。但是,拥有完成一项工作并且做得好的部分比拥有一些封装整个域的上帝类更好。后者是维护的噩梦。

例如,考虑用户想要预订2名成人和1名儿童的情况。您的单一预订模式不会被削减。

# respresents an order
class Booking < ActiveRecord::Base
  enum status: [:open, :payed]
  belongs_to :event
  belongs_to :user

  has_many :items
  has_many :payments

  accepts_nested_attributes_for :items

  # get a sub_total (minus taxes)
  def sub_total
    items.map(&:sub_total).sum
  end

  def grand_total
    sub_total + taxes
  end
end

# represents an item (a seat for example) in a booking
class Item < ActiveRecord::Base
  belongs_to :booking

  def sub_total
    (price - rebate) * quantity
  end
end

# respresents a payment attached to a booking
class Payment < ActiveRecord::Base
  belongs_to :booking

  # charges payment to stripe and saves
  def charge!
  end
end

您还希望为每项工作分别设置控制器:

class BookingsController < ActiveRecord::Base

  before_action :set_event, only: [:new, :create]

  # GET /bookings/:booking_id
  # Show a users
  def show
    @booking = Booking.joins(:event, :payments).find(params[:id])
  end

  # GET /events/:event_id/bookings/new
  def new
    @booking = @event.bookings.new(user: current_user)
  end

  # POST /events/:event_id/bookings
  def create
    @booking = @event.bookings.new(booking_params) do |b|
      b.user = current_user
    end

    if @booking.save
      if @booking.free?
        redirect_to @booking
      else
        redirect_to new_payment_path(@booking)
      end
    else
      render :new
    end
  end

  private 

     def set_event
        @event = Event.find(params[:id])
     end

     def booking_params
       params.require(:booking).permit(items_attributes: [:foo, :bar])
     end
end
class PaymentsController < ActiveRecord::Base

  before_action :set_booking

  # GET /bookings/:booking_id/payments/new
  def new
    @payment = @booking.payments.new 
  end

  # POST /bookings/:booking_id/payments
  def create
    @payment = @booking.payments.new do |p|
      p.total = @booking.grand_total
    end

    if !@payment.valid?
      render :new
    else
      if @payment.charge!
        redirect_to @booking, success: 'Thank you for your payment!'
      else
        render :new
      end
    end
  end

  private
    def set_booking
      @booking = Booking.find(params[:id])
    end

    def payment_params
      params.require(:payment).permit(:foo, :bar)
    end
end

但我希望将整个事情放在一个页面中!

作为单个表单/发布请求执行整个操作会在控制器中创建一个非常脆弱和复杂的流程,并且会有大量的代码路径。

此外,您的javascript代码会让恶意用户通过更改DOM来设置他们认为合适的价格。

首先了解如何以同步方式正确执行此操作。一旦工作,您可以将JSON响应添加到控制器并将流实现为许多AJAX调用。

答案 1 :(得分:0)

问题是您只需更改显示总金额的<span>标记,而不是发送到服务器/条带本身的数据。这一行:

$('.calculate-total span.total').text(total);

只需更新 textual 值,而不是发送到服务器的实际参数。

我认为您可以通过检查发送到BookingsController的参数来验证这一点。如果您在控制器中打印出params,我怀疑您会看到Javascript未更新quantity参数。

我真的不记得simple_form是如何工作的,但您应该能够为quantity属性添加隐藏的输入字段,然后通过Javascript函数设置它。像这样:

function calculateBookingPrice() {
  var unitCost = parseFloat($('.calculate-total .total').data('unit-cost')),
      numSpaces = parseInt($('.calculate-total .num-spaces').val()),
      total = (numSpaces * unitCost).toFixed(2);

  if (isNaN(total)) {
    total = 0;
  }

  $('.calculate-total span.total').text(total);

  // assumes there's an input field with id "booking-quantity".
  $('#booking-quantity').val(total); 
}

$(document).ready(calculateBookingPrice)