因此,基本上,我想将此脚本添加到一个页面上,它将自动重定向到结帐,而无需显示结帐按钮,而无需单击任何按钮。
我该怎么做?
我想这样做,因为我正在使用该代码不起作用的在线网站构建器。这是一个直接且简单的解决方法,我只需将网站构建器中的"结帐"按钮链接到此代码的外部页面。那对我有用。
<!-- Load Stripe.js on your website. -->
<script src="https://js.stripe.com/v3"></script>
<!-- Create a button that your customers click to complete their purchase. Customize the styling to suit your branding. -->
<button
style="background-color:#6772E5;color:#FFF;padding:8px 12px;border:0;border-radius:4px;font-size:1em"
id="ButtonPlan"
role="link"
>
Checkout
</button>
<div id="error-message"></div>
<script>
var stripe = Stripe('APIkey');
var checkoutButton = document.getElementById('ButtonPlan');
checkoutButton.addEventListener('click', function () {
// When the customer clicks on the button, redirect
// them to Checkout.
stripe.redirectToCheckout({
items: [{plan: 'ButtonPlan', quantity: 1}],
// Do not rely on the redirect to the successUrl for fulfilling
// purchases, customers may not always reach the success_url after
// a successful payment.
// Instead use one of the strategies described in
// https://stripe.com/docs/payments/checkout/fulfillment
successUrl: 'https://www.url.com/success',
cancelUrl: 'https://www.url.com/cancel',
})
.then(function (result) {
if (result.error) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer.
var displayError = document.getElementById('error-message');
displayError.textContent = result.error.message;
}
});
});
</script>
我希望该脚本在加载外部页面后自动重定向到结帐,而无需按钮来触发结帐页面。
只是调用相同的代码,但不要将其附加到 checkoutButton.addEventListener
:
<!-- Load Stripe.js on your website. -->
<script src="https://js.stripe.com/v3"></script>
<div id="error-message"></div>
<script>
var stripe = Stripe('APIkey');
// redirect them to Checkout.
stripe.redirectToCheckout({
items: [{plan: 'ButtonPlan', quantity: 1}],
// Do not rely on the redirect to the successUrl for fulfilling
// purchases, customers may not always reach the success_url after
// a successful payment.
// Instead use one of the strategies described in
// https://stripe.com/docs/payments/checkout/fulfillment
successUrl: 'https://www.url.com/success',
cancelUrl: 'https://www.url.com/cancel',
})
.then(function (result) {
if (result.error) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer.
var displayError = document.getElementById('error-message');
displayError.textContent = result.error.message;
}
});
</script>