Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对条带化的API请求失败(错误:不是有效的URL)_Javascript_Node.js_Express_Stripe Payments_Payment Gateway - Fatal编程技术网

Javascript 对条带化的API请求失败(错误:不是有效的URL)

Javascript 对条带化的API请求失败(错误:不是有效的URL),javascript,node.js,express,stripe-payments,payment-gateway,Javascript,Node.js,Express,Stripe Payments,Payment Gateway,我想在节点应用程序中使用条带预构建的签出页面构建一个简单的签出页面 我遵循Stripe文档中所有必要的步骤,但API请求似乎不起作用 server.js- const express = require("express"); const stripe = require("stripe")( "<mySecretKey>" ); const app = express(); app.get("/check

我想在节点应用程序中使用条带预构建的签出页面构建一个简单的签出页面

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

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!");
});
const express=require(“express”);
常量条带=需要(“条带”)(
""
);
常量app=express();
app.get(“/checkout success)”,(请求、回复)=>{
res.send(“成功”);
});
app.get(“/checkout cancel)”,(请求、回复)=>{
res.send(“取消”);
});
app.post(“/create checkout session”),异步(req,res)=>{
const session=wait stripe.checkout.sessions.create({
付款方式类型:[“卡”],
行项目:[
{
价格数据:{
货币:“印度卢比”,
产品数据:{
名称:“Cewa”,
},
单位金额:200,
},
数量:1,
},
],
方式:“付款”,
成功\u url:“/签出成功”,
取消\u url:“/签出取消”,
});
res.json({id:session.id});
});
app.listen(4242,()=>{
log(“服务器位于端口4242上!”);
});
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>

条带API测试
结账
var stripe=stripe(

“成功url
和取消url字段必须是可在internet上访问的真实url。
/checkout success
无效,您必须输入完整url,例如
https://your-site.com/checkout-success

您需要添加真正的成功并取消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",
  });

感谢您的帮助!成功url应该是post还是get?成功url和取消url都是get请求。只要您提供完整的url,如何在localhost
localhost
中进行测试就可以了。例如
success\u url:'http://localhost:3000/success