Javascript Rails-如何正确实施付款/预订流程
我正在rails中构建一个事件应用程序,并使用Stripe来收取付款。这花了我一段时间,但我已经收到了付款。但是,当我尝试增加要预订的空间数量时,系统将只接受一次付款。因此,如果一个活动的每个“空间”为10英镑,而有人想要预订5个空间,那么不管付款表上是否注明了正确的金额(本例中为50英镑),只收取10英镑。 我很确定这是一个MVC问题,但似乎无法发现如何解决它。在我看来,我有以下代码- bookings.new.html.erbJavascript Rails-如何正确实施付款/预订流程,javascript,ruby-on-rails,ruby,ruby-on-rails-4,stripe-payments,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-
<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)
确认您的预订
确认您希望在此处预订的空间数:
总金额
£0
卡号
CVC
有效期(年月日)
/
$('calculate total input')。on('keyup change',calculateBookingPrice);
函数calculateBookingPrice(){
var unitCost=parseFloat($('.calculate total.total')。data('unit-cost')),
numSpaces=parseInt($('.calculate total.num spaces').val(),
总计=(numSpaces*单位成本).toFixed(2);
若有(isNaN(总计)){
总数=0;
}
$('.calculate total span.total').text(total);
}
$(文档).ready(calculateBookingPrice)
(条纹代码)
如下面的示例所示,这在“实际付款”页面上运行良好-
因此,在这个例子中,我使用了@event.price,每个空间/预订1英镑。但是,如果我继续处理此预订,而不是Stripe收取的3英镑,它只显示为1英镑
我认为这可能与@event.price是我在我的观点中所表达的事实有关,但是,当我用@booking.total_amount替换@event.price时,我只会在我的付款表单(如上所述)中得到总金额,显示为零,无论我如何使用quantity函数,它都保持为零
正如您从视图代码中看到的,我的数量函数是用一些javascript处理的。我是不是在这个函数中遗漏了一些东西来实现这个功能
我的控制器中是否有正确的参数?这是我的控制器代码-
预订_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
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
class BookingsController
这是我的带有验证的模型代码-
Booking.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
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
班级预订e
错误。添加(:base,e.message)
假的
结束
结束
结束
结束
这让我困惑了一段时间。我对Rails还很陌生,所以我确信一个更有经验的眼睛可以发现我哪里出了问题。感谢您的指导 问题在于,您只需更改显示总量的
标记,而不是更改发送到服务器/条带本身的数据。这一行:
$('.calculate-total span.total').text(total);
只需更新文本值,而不是发送到服务器的实际参数
我认为您可以通过检查发送到BookingsController的参数来验证这一点
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