【发布时间】:2020-08-28 20:45:19
【问题描述】:
我在 Rails 5 上的 ruby 中有我的网站,我正在使用 this link 更新我的条带支付网关,但单击我的按钮不会将我重定向到条带结帐,这就是我在控制器中所拥有的:
def index
Stripe.api_key = Rails.configuration.stripe[:secret_key]
session = Stripe::Checkout::Session.create(
payment_method_types: ['card'],
line_items: [{
price: 'price_1HKywnBS16ZK5Vr3GYCRvTir',
quantity: 1,
}],
mode: 'subscription',
success_url: 'https://www.my_site.network/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url: 'https://www.my_site.network/cancel',
)
end
我认为错误可能是在我的索引视图的 javascript 代码中替换会话 id 时:
<button id="checkout-button">Pay</button>
<script src="https://js.stripe.com/v3/"></script>
<script type="text/javascript">
var stripe = Stripe('<%= Rails.configuration.stripe[:publishable_key] %>');
var checkoutButton = document.getElementById('checkout-button');
checkoutButton.addEventListener('click', function() {
stripe.redirectToCheckout({
// Make the id field from the Checkout Session creation API response
// available to this file, so you can provide it as argument here
// instead of the {{CHECKOUT_SESSION_ID}} placeholder.
sessionId: '<%=session.id%>'
}).then(function (result) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `result.error.message`.
});
});
</script>
【问题讨论】:
-
您好,Jeff,如果可以的话,在您单击 Checkout 按钮时查看浏览器控制台中显示的错误(如果有的话)会很有帮助。这应该可以更清楚地说明这个问题:developers.google.com/web/tools/chrome-devtools/open
-
@ttmarek 你是对的,控制台给我这个错误(索引):1 Uncaught IntegrationError: stripe.redirectToCheckout: sessionId 的值无效。您指定了“340b63dc228dcafdd225f23ee789b573”。
-
@ttmarek 我想我没有显示会话的 id,我怎样才能得到它?
标签: ruby-on-rails-4 ruby-on-rails-5 stripe-payments