我正在使用rails构建活动应用。对于付款流程,我使用Stripe和一些javascript来更新金额。我希望用户能够指定他们希望支付的空间数量(数量),并为此更新他们需要支付的总金额。 我正在使用的代码是更新应付总金额的文本,但它没有更新服务器,因此每次我进行测试付款时,只收集一个金额。因此,如果一个事件每个空间花费10英镑,我尝试输入,比如4个空格(40英镑),当我进行测试时,Stripe只收集了10英镑。 我需要确保正确传递数量参数。
这是我的预订页码 -
new.booking.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>
<%= simple_form_for [@event, @booking], id: "new_booking" do |form| %>
<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>
<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>
这是我的控制器代码,数量为参数 -
bookings_controller.rb
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
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?
# We can always set this, even for free events because their price will be 0.
#self.total_amount = booking.quantity * event.price
# 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
如何修改此值以正确收集总金额?
这是我的开发日志的输出 -
Started POST "/events/25/bookings" for ::1 at 2016-10-06 18:57:42 +0100
Processing by BookingsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"qHYjvqAqNORnpBKjk4KdQ4++X53Av1KVO+BVIWkq0asLbyd8ssEJKFFcvR9CQHwc+UdAM041gpO3ZFlz0t7o9Q==", "quantity"=>"2", "booking"=>{"stripe_token"=>"tok_191g7l2hHbuHRh0YjftQP0eC"}, "event_id"=>"25"}
[1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1[0m [["id", 2]]
[1m[35mEvent Load (0.2ms)[0m SELECT "events".* FROM "events" WHERE "events"."id" = ? LIMIT 1 [["id", 25]]
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35mSQL (0.3ms)[0m INSERT INTO "bookings" ("stripe_token", "event_id", "user_id", "total_amount", "stripe_charge_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["stripe_token", "tok_191g7l2hHbuHRh0YjftQP0eC"], ["event_id", 25], ["user_id", 2], ["total_amount", 1], ["stripe_charge_id", "ch_191g7m2hHbuHRh0YGAZhmxIu"], ["created_at", "2016-10-06 17:57:43.430265"], ["updated_at", "2016-10-06 17:57:43.430265"]]
答案 0 :(得分:0)
我认为输入标记中缺少name参数。试试下面的一个。
puts params[:quantity]
并检查服务器中是否接收到具有正确值的数量参数。
可能在控制器的create方法中使用UNCOMPRESS()
。