对 Stripe 的 API 请求失败(错误:无效 URL) [英] API request to Stripe fails (Error: Not a valid URL)

查看:54
本文介绍了对 Stripe 的 API 请求失败(错误:无效 URL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Node 应用程序中使用 Stripe Prebuilt Checkout Page 构建一个简单的结账页面.

I want to build a simple checkout page using the Stripe Prebuilt Checkout Page in a Node application.

我按照 Stripe 文档中的所有必要步骤进行操作,但 API 请求似乎不起作用.

I follow all the necessary steps in the Stripe docs but the API request doesn't seem to work.

server.js -

server.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!");
});

checkout.html -

checkout.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 -

When I make this request, I get the following error in the console coming from checkout.html -

错误:网址无效

我认为我没有遗漏文档中的任何内容.知道出了什么问题吗?提前致谢.

I don't think I've missed anything from the docs. Any idea what's going wrong? Thanks in advance.

推荐答案

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

You need to add real success and cancel url, please check below code:

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",
  });

这篇关于对 Stripe 的 API 请求失败(错误:无效 URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆