【发布时间】:2012-08-15 05:44:37
【问题描述】:
我正在构建一个使用信用卡的 Rails 应用程序,我正在尝试使用 Stripe 来完成它。我在将数据从我的应用程序传递到 Stripe 以进行收费时遇到一些问题。这就是我希望在这个主题上获得帮助的原因。
首先,我有一个标准表单(带有值而不是占位符,用于快速提交以进行测试)。表单成功将姓名和电子邮件输入数据库,客户的“计划”暂时硬编码在控制器中:
<%= form_for @customer do |f| %>
<div class="payment-errors"></div>
<div class="name field">
<%= f.label :name %>
<%= f.text_field :name, :value => "Your name" %>
</div>
<div class="email field">
<%= f.label :email %>
<%= f.text_field :email, :value => "yourname@example.com" %>
</div>
<div class="cc_number field">
<%= label_tag 'cc_number' %>
<%= text_field_tag 'cc_number', nil, :value => "4242424242424242" %>
</div>
<div class="ccv field">
<%= label_tag 'ccv' %>
<%= text_field_tag 'ccv', nil, :value => "123" %>
</div>
<div class="cc_expiration field">
<%= label_tag 'cc_month', "Expiration date" %>
<%= text_field_tag 'cc_month', nil, :value => "12" %>
<%= text_field_tag 'cc_year', nil, :value => "2012" %>
</div>
<div class="actions">
<%= f.submit "Continue", :class => 'btn' %>
</div>
<% end %>
还有在我上面代码所在的signups_view,我有这个JS,mostly provided by Stripe:
<script type="text/javascript">
// this identifies your website in the createToken call below
Stripe.setPublishableKey('<%= STRIPE['public'] %>');
function stripeResponseHandler(status, response) {
if (response.error) {
// show the errors on the form
$(".payment-errors").text(response.error.message);
$("input[type=submit]").removeAttr("disabled");
} else {
var form$ = $("form");
// 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='customer[stripe_token]' id='stripeToken' value='" + token + "'/>");
// and submit
$('.cc_number.field, .ccv.field, .cc_expiration.field').remove();
form$.get(0).submit();
}
}
$(document).ready(function() {
$("form").submit(function(event) {
// disable the submit button to prevent repeated clicks
$('input[type=submit]').attr("disabled", "disabled");
Stripe.createToken({
number: $('#cc_number').val(),
cvc: $('#ccv').val(),
exp_month: $('#cc_month').val(),
exp_year: $('#cc_year').val()
}, stripeResponseHandler);
// prevent the form from submitting with the default action
return false;
});
});
</script>
form$.append("<input type='hidden' name='customer[stripe_token]' id='stripeToken' value='" + token + "'/>"); 行似乎有问题,因为我的 Ruby 应用程序在到达 customer[stripe_token] 时中断。
Finally, in my `customers_controller`, I have:
def create
@customer = Customer.new(params[:customer])
@customer.product =
if @customer.save
save_order
redirect_to @customer
else
render action: 'new'
end
def save_order
Stripe.api_key = STRIPE['secret']
charge = Stripe::Charge.create(
:amount => 20,
:currency => "usd",
:card => @customer.stripe_token,
:description => "Product 1"
)
end
每当我提交表单时,它每次都会点击控制器中的else 子句,经过大量调试、谷歌搜索并从头开始重新构建,我仍然感到困惑。
非常感谢任何帮助。
编辑:添加了customer model
attr_accessible :name, :email, :stripe_token, :product
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true,
:format => { :with => email_regex },
:length => { :minimum => 6, :maximum => 60 },
:uniqueness => { :case_sensitive => false }
validates :name, :length => {:minimum => 2, :maximum => 80 }
【问题讨论】:
标签: javascript ruby-on-rails forms stripe-payments