【发布时间】:2014-11-04 21:39:10
【问题描述】:
我有一个带有单选按钮的表格,用于选择捐赠金额的选项。
看起来像这样:
<tr>
<td><input type="radio" name="amount" value="10"><span>$10</span></td>
<td><input class="awk" type="radio" name="amount" value="25"><span>$25</span></td>
</tr>
<tr>
<td><input type="radio" name="amount" value="50"><span>$50</span></td>
<td><input type="radio" name="amount" value="100"><span>$100</span></td>
</tr>
有一个支付按钮,可以打开 Stripes 支付网关。
我的表格是这样的:
<%= form_tag charges_path, id: 'chargeForm' do %>
<script src="https://checkout.stripe.com/checkout.js"></script>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<button id="customButton" class="btn btn-large btn-primary">Buy Now</button>
<script>
var handler = StripeCheckout.configure({
key: 'foo',
image: '/assets/my_logo.png',
token: function(token, args) {
document.getElementById("stripeToken").value = token.id;
document.getElementById("stripeEmail").value = token.email;
document.getElementById("chargeForm").submit();
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'My Company',
description: 'Product ($60.00)',
amount: (100 * $('input[name=amount]:checked', '#stripe_donate').val()),
shippingAddress: false
});
e.preventDefault();
});
</script>
<% end %>
这会加载条带模式,在支付按钮上正确显示正确的金额,但我如何告诉 RAILS 向客户收取多少费用?由于 Rails 控制器代码,Stripe 无论如何都要收取 5.00 美元:
class ChargesController < ApplicationController
def new
end
def create
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => 'example@stripe.com',
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
end
那么我如何告诉控制器中的rails @amount 需要设置为从页面接收到的值?我是否完全跳过 rails 路线并使用纯 JS 执行此操作?
【问题讨论】:
标签: javascript jquery ruby-on-rails stripe-payments