我正在尝试将Stripe.js集成到我的Rails应用程序中,用于活动预订网站,我收到上述错误。我在这里看了很多类似的问题,似乎没有一个解决方案可行。
这是我的代码 -
预订控制器
class BookingsController < ApplicationController
before_action :booking_params, only: [:create]
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
# which person is booking the event?
@booking.user = current_user
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.save
# CHARGE THE USER WHO'S BOOKED
# #{} == puts a variable into a string
Stripe::Charge.create(amount: @event.price, currency: "gbp",
card: @booking.stripe_token, description: "Booking number #{@booking.id}")
flash[:success] = "Your place on our event has been booked"
redirect_to event_path(@event)
else
flash[:error] = "Payment unsuccessful"
render "new"
end
end
private
def booking_params
params.require(:booking).permit(:stripe_token)
end
端
预订模式
class Booking < ActiveRecord::Base
belongs_to :event
belongs_to :user
端
预订视图 -
<%= simple_form_for [@event, @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>
<%= form.button :submit %>
<% end %>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
// This identifies your website in the createToken call below
Stripe.setPublishableKey('<%= STRIPE_PUBLIC_KEY %>');
// ...
// jQuery(function($) { - changed to the line below
$(document).on("ready page:load", function () {
$('#new_order').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;
});
});
var stripeResponseHandler = function(status, response) {
var $form = $('#new_order');
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="order[stripe_token]" />').val(token));
// and submit
$form.get(0).submit();
}
};
</script>
由于某种原因,它没有捕获booking_params。任何建议或指导将不胜感激。
答案 0 :(得分:0)
更改行:
$form.append($('<input type="hidden" name="order[stripe_token]" />').val(token));
要:
$form.append($('<input type="hidden" name="booking[stripe_token]" />').val(token));
并删除该行:
before_action :booking_params, only: [:create]
根本没有必要。
答案 1 :(得分:0)
尝试在提交表单的调用之前放置函数stripeResponseHandler
。
E.G。
<script type="text/javascript">
Stripe.setPublishableKey('<%= STRIPE_PUBLIC_KEY %>');
var stripeResponseHandler = function(status, response) {
var $form = $('#new_order');
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="order[stripe_token]" />').val(token));
// and submit
$form.get(0).submit();
}
};
// jQuery(function($) { - changed to the line below
$(document).on("ready page:load", function () {
$('#new_order').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>
另外,请更改此
$form.append($('<input type="hidden" name="order[stripe_token]" />').val(token));
到
$form.append($('<input type="hidden" name="booking[stripe_token]" />').val(token));
Alos,给表单一个id,就像这样
<%= simple_form_for [@event, @booking], id: "new_order" do |form| %>
它应与您的javascript中的ID匹配。