我无法让我的 Stripe-API 支付元素和链接身份验证元素可见,只有立即付款按钮出现

问题描述 投票:0回答:0

我的支付元素和链接验证元素 javascript 没有按照建议注入表单信息

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Accept a payment</title>
    <meta name="description" content="A demo of a payment on Stripe" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="checkout.css" />
    <script src="/checkout.js" defer></script>
   
  </head>
  <body>
  
   
   
    <!-- Display a payment form -->
    <form id="payment-form">
      <div id="link-authentication-element">
        <!--Stripe.js injects the Link Authentication Element-->
      </div>
      <div id="payment-element">
        <!--Stripe.js injects the Payment Element-->
      </div>
      <button id="submit">
        <div class="spinner hidden" id="spinner"></div>
        <span id="button-text">Pay now</span>
      </button>
      <div id="payment-message" class="hidden"></div>
    </form>
    
    <script src="https://js.stripe.com/v3/"></script>
    
    
  </body>
</html>

我的 JavaScript (checkout.js):

let elements;

initialize();
checkStatus();

document
  .querySelector("#payment-form")
  .addEventListener("submit", handleSubmit);

let emailAddress = '';
// Fetches a payment intent and captures the client secret
async function initialize() {
  const response = await fetch("/create-payment-intent", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ items }),
  });
  const { clientSecret } = await response.json();

  const appearance = {
    theme: 'stripe',
  };
  elements = stripe.elements({ appearance, clientSecret });

  const linkAuthenticationElement = elements.create("linkAuthentication");
  linkAuthenticationElement.mount("#link-authentication-element");

  linkAuthenticationElement.on('change', (event) => {
    emailAddress = event.value.email;
  });

  const paymentElementOptions = {
    layout: "tabs",
  };

  const paymentElement = elements.create("payment", paymentElementOptions);
  paymentElement.mount("#payment-element");
}

async function handleSubmit(e) {
  e.preventDefault();
  setLoading(true);

  const { error } = await stripe.confirmPayment({
    elements,
    confirmParams: {
      // Make sure to change this to your payment completion page
      return_url: "http://localhost:8080/checkout.html",
      receipt_email: emailAddress,
    },
  });

基本上我只是复制并粘贴了 Stripe 的示例代码。为了修复错误,我尝试在 HTML 页面中移动脚本并更改 SDK 版本(我使用的是 22.0.0)。我希望电子邮件、付款类型、客户信息等以浏览器的形式显示,但出现的只是立即付款按钮。

html dom stripe-payments stripe-payments-js
© www.soinside.com 2019 - 2024. All rights reserved.