我可以将新客户保存到我的网络应用中,但我无法向该客户提供信用卡。我相信我正确地遵循了stripe.com的指示,但我似乎无法弄明白。我正在使用ruby / rails和haml
我的代码在
下面payment.html.haml:
= form_tag("", method: "POST", id: "payment-form") do
%span.payment-errors
.field
= label_tag :card_number, "Credit Card Number", class: "labelTag"
= text_field_tag :card_number, nil, {:class => "ss-form-control", :name => nil, "data-stripe" => "number"}
.field
= label_tag :card_code, "Security Code on Card (CVC)", class: "labelTag"
= text_field_tag :card_code, nil, {:class => "ss-form-control minilabel", :name => nil, "data-stripe" => "cvc"}
.field
= label_tag :card_month, "Card Expiration", class: "labelTag"
= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month", class: 'minilabel', "data-stripe" => 'exp-month'}
= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year", "data-stripe" => 'exp-year'}
%button.standardButton.btn.btn-sumbit{type: "submit"} Submit Payment
的application.js
Stripe.setPublishableKey('mykey');
$('#payment-form').submit(function(event) {
var $form = $(this);
alert('first')
// Disable the submit button to prevent repeated clicks
$form.find('button').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 = $('#payment-form');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
alert('second');
// 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="stripeToken" />').val(token));
// and submit
$form.get(0).submit();
}
};
user_controller.rb
# Get the credit card details submitted by the form
token = params[:stripeToken]
# Create a Customer
customer = Stripe::Customer.create(
:card => token,
:description => current_user.email
)
答案 0 :(得分:2)
如果没有在帖子表单上看到Rails登录就很难说这里的错误,但我想这是因为你没有在form_tag中指定路径。即。
= form_tag("", method: "POST", id: "payment-form")
而不是
= form_tag("/some_path", method: "POST", id: "payment-form")
或
= form_tag(some_path, method: "POST", id: "payment-form")