reactjs 在Stripe上提交付款信息后无法重定向到成功页面,在本地工作,但不是在HEROKU上,(“无法获取/成功”)

i7uq4tfw  于 2023-06-05  发布在  React
关注(0)|答案(1)|浏览(366)

我试图在Stripe上提交付款信息后生成一个成功页面,但在Heroku上部署时不起作用。本地一切正常,我被重定向到成功页面。
在提交付款信息后,它只是重定向到一个页面,上面写着“无法获取/成功”。
我正在使用MERN堆栈。
我使用的是一个测试密钥,任何人都可以从条纹文档中获得,所以没有必要将其隐藏在ENV文件中。
以下是Heroku日志:

at=info method=GET path="/success? 
session_id=cs_test_a1d94XAhzPyyubwBg7Lk3FXKmpViqUYK6UGzcqON3HAdD5ELFUINJyOVsX" 
host=thawing-headland-75590.herokuapp.com request_id=7aff0f42-cd34-4d12-b5b3- 
858ff60e16de fwd="174.112.119.39" dyno=web.1 connect=0ms service=1ms status=404 
bytes=390 protocol=https.

下面是控制台中显示的内容:

constants.override.js:1 
 GET https://thawing-headland-75590.herokuapp.com/success?session_id=cs_test_a1jXbh7ZPggtKlX0840DpYCfTZXr5uZp5soNzvQoCfGRVBVEpmtzfR9lPj 404 (Not Found)
a.handleSuccess @   constants.override.js:1
a   @   js.stripe.com/v3/fin…36134c4204a1d0.js:1
setTimeout (async)      
(anonymous) @   js.stripe.com/v3/fin…36134c4204a1d0.js:1
value   @   constants.override.js:1
Lr  @   content.js:1
t.unstable_runWithPriority  @   content.js:1
Ot  @   content.js:1
jr  @   content.js:1
Sr  @   content.js:1
(anonymous) @   content.js:1
t.unstable_runWithPriority  @   content.js:1
Ot  @   content.js:1
At  @   content.js:1
Nt  @   content.js:1
_r  @   content.js:1
notify  @   content.js:1
notifyNestedSubs    @   content.js:1
n   @   content.js:1
l   @   content.js:1
(anonymous) @   constants.override.js:1
(anonymous) @   content.js:1
dispatch    @   content.js:1
(anonymous) @   constants.override.js:1.

这是我的server.js

const express = require("express");
        const { ApolloServer } = require("apollo-server-express");
        const path = require("path");

        const { typeDefs, resolvers } = require("./schemas");
        const { authMiddleware } = require("./utils/auth");
        const db = require("./config/connection");

        const PORT = process.env.PORT || 3001;
        const app = express();
        const server = new ApolloServer({
          typeDefs,
          resolvers,
          context: authMiddleware
        });

        app.use(express.urlencoded({ extended: false }));
        app.use(express.json());

     
        app.use("/images", express.static(path.join(__dirname, "../client/images")));

        if (process.env.NODE_ENV === "production") {
          app.use(express.static(path.join(__dirname, "../client/build")));
        }

        app.get("/", (req, res) => {
          res.sendFile(path.join(__dirname, "../client/build/index.html"));
        });

      
        const startApolloServer = async (typeDefs, resolvers) => {
          await server.start();
          server.applyMiddleware({ app });

          db.once("open", () => {
            app.listen(PORT, () => {
              console.log(`API server running on port ${PORT}!`);
              console.log(`Use GraphQL at http://localhost:${PORT}${server.graphqlPath}`);
            })
          })
          };

          startApolloServer(typeDefs, resolvers);

这里是我的resolver.js

const { AuthenticationError } = require("apollo-server-express");
        const { User, Product, Console, Order } = require("../models");
        const { signToken } = require("../utils/auth");
        const stripe = require("stripe")("sk_test_123");

        const resolvers = {
          Query: {
            consoles: async () => {
              return await Console.find();
            },
            products: async (parent, { console, name }) => {
              const params = {};

              if (console) {
                params.console = console;
              }

              if (name) {
                params.name = {
                  $regex: name
                };
              }

              return await Product.find(params).populate("console");
            },
            product: async (parent, { _id }) => {
              return await Product.findById(_id).populate("console");
            },
            user: async (parent, args, context) => {
              if (context.user) {
                const user = await User.findById(context.user._id).populate({
                  path: "orders.products",
                  populate: "console"
                });

                user.orders.sort((a, b) => b.purchaseDate - a.purchaseDate);

                return user;
              }

              throw new AuthenticationError("Not logged in");
            },
            order: async (parent, { _id }, context) => {
              if (context.user) {
                const user = await User.findById(context.user._id).populate({
                  path: "orders.products",
                  populate: "console"
                });

                return user.orders.id(_id);
              }

              throw new AuthenticationError("Not logged in");
            }, 
            checkout: async (parent, args, context) => {
              const url = new URL(context.headers.referer).origin;
              const order = new Order({ products: args.products });
              const line_items = [];
              const { products } = await order.populate("products");

              for (let i = 0; i < products.length; i++) {
            
                const product = await stripe.products.create({
                  name: products[i].name, 
                  description: products[i].description,
                  images: [`${url}/images/${products[i].image}`]
                });

                const price = await stripe.prices.create({
                  product: product.id, 
                  unit_amount: products[i].price * 100, 
                  currency: "usd", 
                });

              
                line_items.push({
                  price: price.id, 
                  quantity: 1
                });
              }

              const session = await stripe.checkout.sessions.create({
                payment_method_types: ["card"], 
                line_items, 
                mode: "payment", 
                success_url: `${url}/success?session_id={CHECKOUT_SESSION_ID}`,
                cancel_url: `${url}/`
              });
              return { session: session.id };
            }

就像我之前说的,在本地一切都很好,所以我有点困惑为什么它在Heroku上不起作用。本网站的其他部分在Heroku上运行良好。它只是从付款到成功,在那里有一个问题。
先谢谢你了!

5lhxktic

5lhxktic1#

现在起作用了在错误的方向上寻找了许多小时之后,我需要做的就是在我的server.js文件中添加以下内容:

app.get("/success", (req, res) => {
  res.sendFile(path.join(__dirname, "../client/build/index.html"));
});

相关问题