gpt4 book ai didi

javascript - Rails 4 应用程序一页上的多个 Stripe 支付按钮

转载 作者:数据小太阳 更新时间:2023-10-29 04:38:44 26 4
gpt4 key购买 nike

我正在构建一个列出三个不同订阅选项的支付页面,并使用 Stripe 的结账来管理支付。

页面呈现正确,所有 3 个订阅选项都有应该链接到 Stripe 的“立即购买”按钮。

我的问题是第一个按钮是唯一正确拉起 Stripe 结帐流程的按钮。按钮 2 和 3 抛出以下错误:

未知操作找不到 ChargesController 的操作“索引”

我的支付页面的相关部分是:

  <% @plans.each do |plan| %>
<li class="col-md-3 plan <%= 'plan-primary' if plan.highlight? %>">
<div class="img-thumbnail">
<div class="caption">
<h3><%= plan.name %></h3>
<h4><%= plan_price(plan) %></h4>
<div class="call-to-action">
<% if @subscription.nil? %>
<% if plan.highlight? %>

<%= form_tag main_app.charges_path do %>
<script src="https://checkout.stripe.com/checkout.js"></script>

<button id="customButton" class="btn btn-success">Buy Now</button>

<script>
var handler = StripeCheckout.configure({
key: '<%= 'pk_test_my_pk' %>',
image: '/assets/my_logo.png',
token: function(response) {
var tokenInput = $("<input type=hidden name=stripeToken />").val(response.id);
var emailInput = $("<input type=hidden name=stripeEmail />").val(response.email);
$("form").append(tokenInput).append(emailInput).submit();
}
});

document.getElementById('customButton').addEventListener('click', function(e) {
handler.open({
name: 'My Co',
description: 'Listing subsctiption ($50.00)',
amount: 50*100,
shippingAddress: false
});
e.preventDefault();
});
</script>
<% end %>

<% else %>
<%= form_tag main_app.charges_path do %>
<script src="https://checkout.stripe.com/checkout.js"></script>

<button id="customButton" class="btn btn-large btn-primary">Buy Now</button>

<script>
var handler = StripeCheckout.configure({
key: '<%= 'pk_test_my_pk' %>',
image: '/assets/my_logo.png',
token: function(response) {
var tokenInput = $("<input type=hidden name=stripeToken />").val(response.id);
var emailInput = $("<input type=hidden name=stripeEmail />").val(response.email);
$("form").append(tokenInput).append(emailInput).submit();
}
});

document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'My Co',
description: 'Listing subsctiption ($40.00)',
amount: 40*100,
shippingAddress: false
});
e.preventDefault();
});
</script>
<% end %>


<% end %>

关于为什么 3 个按钮中只有一个可以正常工作的想法?

谢谢!

最佳答案

您可以通过使用唯一的按钮 ID 使其看起来正常工作,例如

<button id="<%= dom_id(pricing, 'btn') %>

但是还有一个问题,就是stripe js。如果您多次执行 StripeCheckout.configure ,它将创建多个具有相同名称属性的 iframe。不幸的是,这意味着无论您的客户试图购买什么,他们总是会在您插入的最后一件商品中售出,即使 Stripe 弹出窗口说它正在卖给他们其他东西。

我们使用了这个解决方案:一种形式,动态插入价格和时间:

<%= form_tag charges_path, id: 'stripe-payment-form' do %>
<%= hidden_field_tag 'amount', nil, id: 'payment_amount' %>
<%= hidden_field_tag 'name', nil, id: 'payment_name' %>
<%= hidden_field_tag 'days', nil, id: 'payment_days' %>

<% Pricing.all.each do |pricing| %>
<p>
<button id="<%= dom_id(pricing, 'btn') %>">
Buy <%= pricing.name %> for <%= number_to_currency(pricing.pounds, unit: '£') %>
</button>
</p>
<% end %>

<%= javascript_tag do %>
var handler = StripeCheckout.configure({
key: "<%= Rails.configuration.stripe[:publishable_key] %>",
image: "<%= image_path('/images/apple-icons/apple-touch-icon-144x144-precomposed.png') %>",
token: function(token, args) {
var form = $('#stripe-payment-form');
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
form.append($('<input type="hidden" name="stripeToken" />').val(token.id));
form.submit();
}
});

<% Pricing.all.each do |pricing| %>
document.getElementById('<%= dom_id(pricing, 'btn') %>').addEventListener('click', function(e) {
e.preventDefault();
var form = $('#stripe-payment-form');
// set the price etc for the button clicked
$('#payment_amount').val("<%= pricing.pence %>");
$('#payment_name').val("<%= pricing.name %>");
$('#payment_days').val("<%= pricing.days %>");
// Open Checkout with further options
handler.open({
name: 'Company name',
currency: 'GBP',
description: '<%= pricing.name %>',
amount: '<%= pricing.pence %>',
email: '<%= member.email %>',
});
});
<% end %>
<% end %>
<% end %>

关于javascript - Rails 4 应用程序一页上的多个 Stripe 支付按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21544146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com