axios未发送Stripe的帖子

ogsagwnx  于 2022-11-05  发布在  iOS
关注(0)|答案(1)|浏览(208)

我有一个问题与axios后。我的工作添加条纹付款到我的网页,但我给我axios错误404。
也许有人可以帮助我,我已经坚持了一个星期,不能只是解决它.下面是代码:

import React from 'react'
import StripeCheckout from 'react-stripe-checkout';
import { useState, useEffect } from "react";
import { useNavigate } from 'react-router-dom';
import axios from "axios";

const Pay = () => {
    const [stripeToken, setStripeToken] = useState(null)
    const navigate = useNavigate()
    const onToken = (token) => {
        setStripeToken(token);
    }
    useEffect(() => {
        const makeRequest = async () => {
            try {
                const res = await axios.post(
                    "http//:localhost:5001/api/checkout/payment",
                    {
                        tokenId: stripeToken.id,
                        amount: 6000,
                    });
                console.log(res.data)
            } catch (err) {
                console.log(err)
            }
        };
        stripeToken && makeRequest()
    }, [stripeToken]);

return (
        <div className='pay__div'>

            {stripeToken ? (<span>Processing</span>) : (
                <StripeCheckout
                    name="Kate store"
                    image=""
                    billingAddress
                    shippingAddress
                    description="Yout total is $60"
                    amount={2000}
                    token={onToken}
                    stripeKey={KEY}
                >

                    <button>Pay</button>
                </StripeCheckout>
            )}

        </div>
    )
}

export default Pay

感谢您抽出宝贵时间。
这里还有stripe.js index.js来自后端的页面
stripe.js

const router = require("express").Router();

const stripe = require("stripe")(process.env.STRIPE_KEY);

router.post("/payment", (req, res) => {
    stripe.charges.create({
        sourse: req.body.tokenId,
        amount: req.body.amount,
        currency: "usd"
    },
        (stripeErr, stripeRes) => {
            if (stripeErr) {
                res.status(500).json(stripeErr);
            } else {
                res.status(200).json(stripeRes);
            }
        });
});

module.exports = router;

和index.js

const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const userRoute = require("./routes/user");
const authRoute = require("./routes/auth");
const productRoute = require("./routes/product");
const cartRoute = require("./routes/cart");
const orderRoute = require("./routes/order");
const stripeRoute = require("./routes/stripe");

const cors = require("cors");

dotenv.config();

mongoose.connect(process.env.MONGO_URL)
    .then(() => console.log("DB Connected Successfull!"))
    .catch((err) => {
        console.log(err);
    })

app.use(cors());
app.use(express.json());
app.use("/api/user", userRoute); 
app.use("/api/auth", authRoute);
app.use("/api/product", productRoute);
app.use("/api/cart", cartRoute);
app.use("/api/order", orderRoute);
app.use("/api/checkout", stripeRoute);

app.listen(process.env.PORT || 5001, () => {
    console.log("Backend is running")
})
rfbsl7qr

rfbsl7qr1#

在index.js中将dotenv.config();移到const stripeRoute = require("./routes/stripe");之前并正常工作。

相关问题