【问题标题】:Why isn't the "axios.post("/api/payment_intents", method able to locate my payment intents?为什么 "axios.post("/api/payment_intents", 方法不能定位我的付款意图?
【发布时间】:2020-09-11 17:45:03
【问题描述】:

我的目标是在 React JS 网络应用程序中使用 Stripe 处理测试付款。当我输入测试卡信息时,我收到一个 404 错误并显示以下消息:“POST /api/payment_intents 404(未找到)”。为什么 "axios.post("/api/payment_intents", 方法不能定位我的付款意图?

[https://github.com/ChicagoJoe1991/saas-template][1]

import Stripe from "stripe";

const stripe = new Stripe(process.env.REACT_APP_SECRET_KEY);

export default async (req, res) => {
  if (req.method === "POST") {
    try {
      const { amount } = req.body;
        const paymentIntent = await stripe.paymentIntents.create({
        amount,
        currency: "usd",
      });

      res.status(200).send(paymentIntent.client_secret);
    } catch (err) {
      res.status(500).json({ statusCode: 500, message: err.message });
    }
  } else {
    res.setHeader("Allow", "POST");
    res.status(405).end("Method Not Allowed");
  }
};


try {
  const { data: clientSecret } = await axios.post("/api/payment_intents", {
    amount: price * 100,
  });

  const paymentMethodReq = await stripe.createPaymentMethod({
    type: "card",
    card: cardElement,
    billing_details: billingDetails,
  });

  if (paymentMethodReq.error) {
    setCheckoutError(paymentMethodReq.error.message);
    setProcessingTo(false);
    return;
  }

  const { error } = await stripe.confirmCardPayment(clientSecret, {
    payment_method: paymentMethodReq.paymentMethod.id,
  });

  if (error) {
    setCheckoutError(error.message);
    setProcessingTo(false);
    return;
  }

  onSuccessfulCheckout();
} catch (err) {
  setCheckoutError(err.message);
}

};

【问题讨论】:

  • 你设置正确的axios baseURL了吗?还可以直接使用 cURL 发出 POST 请求,以验证路由是否正确公开。

标签: reactjs axios stripe-payments http-status-code-404


【解决方案1】:

你没有设置baseURL

{ axios.post("/api/payment_intents", }

您必须添加一些服务器路径,例如 localhost 或您托管 API 的任何位置。

【讨论】:

    猜你喜欢
    • 2017-07-19
    • 2015-05-19
    • 1970-01-01
    • 2022-01-10
    • 2019-12-30
    • 2011-09-16
    • 2017-12-23
    • 2020-06-01
    • 2021-03-17
    相关资源
    最近更新 更多