对 Stripe 的 API 请求失败(错误:不是有效的 URL)

2024-03-24

我想在 Node 应用程序中使用 Stripe 预构建结帐页面构建一个简单的结帐页面。

我遵循 Stripe 文档中的所有必要步骤,但 API 请求似乎不起作用。

服务器.js-

const express = require("express");
const stripe = require("stripe")(
  "<mySecretKey>"
);

const app = express();

app.get("/checkout-sucess", (req, res) => {
  res.send("<h1>Success</h1>");
});

app.get("/checkout-cancel", (req, res) => {
  res.send("<h1>Cancelled</h1>");
});

app.post("/create-checkout-session", async (req, res) => {
  const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    line_items: [
      {
        price_data: {
          currency: "inr",
          product_data: {
            name: "Cewa",
          },
          unit_amount: 200,
        },
        quantity: 1,
      },
    ],
    mode: "payment",
    success_url: "/checkout-success",
    cancel_url: "/checkout-cancel",
  });

  res.json({ id: session.id });
});

app.listen(4242, () => {
  console.log("Server is live on Port 4242!");
});

结帐.html -

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Stripe API Test</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
    <script src="https://js.stripe.com/v3/"></script>
  </head>
  <body>
    <button
      type="button"
      id="checkout-button"
      style="
        padding: 20px;
        background-color: beige;
        cursor: pointer;
        outline: none;
      "
    >
      Checkout
    </button>
  </body>

  <script type="text/javascript">
    var stripe = Stripe(
      "<myPublishableAPIKey"
    );
    var checkoutButton = document.getElementById("checkout-button");
    checkoutButton.addEventListener("click", function () {
      fetch("http://localhost:4242/create-checkout-session", {
        method: "POST",
      })
        .then(function (response) {
          return response.json();
        })
        .then(function (session) {
          return stripe.redirectToCheckout({ sessionId: session.id });
        })
        .then(function (result) {
          // If redirectToCheckout fails due to a browser or network
          // error, you should display the localized error message to your
          // customer using error.message.
          if (result.error) {
            alert(result.error.message);
          }
        })
        .catch(function (error) {
          console.error("Error:", error);
        });
    });
  </script>
</html>

当我发出此请求时,我在控制台中收到以下错误checkout.html -

错误:不是有效的 URL

我不认为我错过了文档中的任何内容。知道出了什么问题吗?提前致谢。


需要添加真正成功和取消的url,请检查以下代码:

const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    line_items: [
      {
        price_data: {
          currency: "inr",
          product_data: {
            name: "Cewa",
          },
          unit_amount: 200,
        },
        quantity: 1,
      },
    ],
    mode: "payment",
    success_url: "http://sitename.com/checkout-success",
    cancel_url: "http://sitename.com/checkout-cancel",
  });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对 Stripe 的 API 请求失败(错误:不是有效的 URL) 的相关文章