gpt4 book ai didi

javascript - Rails/Stripe - 付款不会处理

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:20 24 4
gpt4 key购买 nike

我正在使用 Rails 和 Stripe 构建一个事件应用程序来处理付款。我在我的预订模型中设置了一个预订方法来处理付款流程。

代码必须能够允许用户一次预订多个空间。

我没有收到任何错误,但是,每次我尝试处理任何类型的预订时,我都会从我的代码中收到错误消息“预订不成功”。我似乎不明白为什么它直接跳到这里而不是完成付款和预订。

这是我预订模型中的预订方法 -

    def reserve
# Don't process this booking if it isn't valid
return unless valid?

# We can always set this, even for free events because their price will be 0.
self.total_amount = 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
charge = Stripe::Charge.create(amount: total_amount, currency: "gbp", card: stripe_token, description: "Booking number #{Booking.id}", items: [{quantity: quantity}])
self.stripe_charge_id = charge.id
save
rescue Stripe::CardError => e
errors.add(:base, e.message)
false
end
end
end
end

在我的 bookings_controller 中,我有以下新建和创建操作 -

    def new

@event = Event.find(params[:event_id])
@booking = @event.bookings.new(quantity: params[:quantity])
@booking.user = current_user

end

def create
@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)
end

是什么导致@booking.reserve 失败并且代码只是默认为闪存错误消息?

这是我认为的代码。我需要在我的 Stripe javascript 代码中添加任何东西吗?

booking.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>

<script type="text/javascript">
Stripe.setPublishableKey('<%= STRIPE_PUBLIC_KEY %>');
var stripeResponseHandler = function(status, response) {
var $form = $('#new_booking');

if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('input[type=submit]').prop('disabled', false);
} else {
// token contains id, last4, and card type
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="booking[stripe_token]" />').val(token));
// and submit
$form.get(0).submit();
}
};

// jQuery(function($) { - changed to the line below
$(document).on("ready page:load", function () {

$('#new_booking').submit(function(event) {
var $form = $(this);

// Disable the submit button to prevent repeated clicks
$form.find('input[type=submit]').prop('disabled', true);

Stripe.card.createToken($form, stripeResponseHandler);

// Prevent the form from submitting with the default action
return false;
});
});
</script>

请问关键线是不是这条线?

  self.stripe_charge_id = charge.id

我需要在预订表中添加任何内容吗?这是架构 -

create_table "bookings", force: :cascade do |t|
t.integer "event_id"
t.integer "user_id"
t.string "stripe_token"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "quantity"
t.integer "total_amount"
end

我是 Rails 的新手,这是我第一次尝试实现这样的支付流程。我敢肯定,对于有经验的人来说,这可能是直截了当的。任何援助将不胜感激。

问题更新 - 25/09/16

我已经对我的问题做了一些工作,我发现这一行阻止了整个“保留”方法的继续(正如它所暗示的那样)-

  # Don't process this booking if it isn't valid
return unless valid?

然后我遇到了这一行的“未定义方法‘*’for nil:class”-

 # We can always set this, even for free events because their price will be 0.
self.total_amount = booking.quantity * event.price

所以,我已经注释掉了这两行,因为我不知道为什么它们不起作用。我希望至少能够到达可以处理付款的阶段,但现在我收到此错误 -

Stripe error - source or customer

我很确定 source 是我的应用程序的正确选项,我已经尝试为此添加代码,但它没有采用。什么是正确的选择?

最佳答案

在您的 bookings_controller::create 操作中,您正在使用

访问事件
@event = Event.find(params[:event_id])

我检查了 new.html.erb,我认为你没有通过事件 ID 来创建操作。请检查一下。

使用 javascript 传递 event_id:

var eventID = "<%= @event.id %>"

在您启动 javascript 的 View 中添加以上行。

$form.append($('<input type="hidden" name="event_id">').val(eventID));

将此添加到 $('#new_booking').submit 函数中。

关于javascript - Rails/Stripe - 付款不会处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39593549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com