简体   繁体   English

如何在没有按钮的情况下自动重定向到页面加载时的Stripe Checkout

[英]How to automatically redirect to Stripe checkout on page load without a button

So basically, I want to add this script to a page where it will automatically redirect to the checkout, without showing a checkout button and without having to click on any button. 因此,基本上,我想将此脚本添加到页面中,该脚本将自动重定向到检出页面,而无需显示检出按钮且无需单击任何按钮。

How do I do this? 我该怎么做呢?

I want to do this because I'm using an online website builder where this code doesn't work. 我要这样做是因为我使用的在线网站构建器无法使用此代码。 This is a direct and easy workaround where I would simply link the checkout button within the website builder to an external page with this code. 这是一种直接而又简单的解决方法,我可以使用此代码将网站构建器中的checkout按钮简单地链接到外部页面。 That would work for me. 那对我有用。

<!-- 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>

I expect this script to automatically redirect to checkout once external page is loaded, without the need for a button to trigger checkout page. 我希望此脚本在加载外部页面后自动重定向到结帐,而无需按钮来触发结帐页。

Just call the same code but don't attach it to checkoutButton.addEventListener : 只需调用相同的代码,但不要将其附加到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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM