在我的Rails应用程序中使用Stripe对卡充电时出现问题
Problems charging a card with Stripe in my Rails app
我正在构建一个可以使用信用卡的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,主要由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 }
查看客户模型会有助于了解发生了什么。如果@Customer.save返回false,则表示验证器可能失败。
此外,您的模型上是否有stripe_token作为可访问属性?否则,您将无法像现在这样将其从表单中分配。请注意,令牌应该而不是存储在数据库中,因为它只能使用一次。
class Customer
attr_accessor :stripe_token # do you have this?
end
还有一点需要注意:您可能需要存储Stripe ID字段,以便稍后检索客户付款并取消他们的帐户。
相关文章:
- 在指令控制器中使用$attrs时出现问题
- 将PHP变量传递给jQuery时遇到问题
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- 参数变量出现ngTable指令问题
- 剑道网格jQuery动画()问题
- 我的jQuery插件参数没有正确启动,遇到了问题
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- TableExport jquery插件:文件名和扩展名问题
- JavaScript Pub/Sub属性访问问题
- JavaScript异步问题
- 如何解决Yii中的页面刷新问题
- Safari(Mac OS)上的jQuery平滑滚动问题
- jqGrid树网格问题
- 使用正则表达式评估电子邮件地址时出现性能问题
- 如何消除代码中的未定义和其他问题
- JavaScript代码问题:我正在将对象转换为数组
- 三星智能电视应用程序;Brightcove示例应用程序远程控制问题
- 由于iframe导致的问题
- ReactJS和SpringDataRest缓存问题可能与websocket有关
- 在我的Rails应用程序中使用Stripe对卡充电时出现问题