重定向到成功网址 - Stripe

问题描述 投票:0回答:1

我的代码遇到问题。

我正在 Stripe 上构建结账会话,我想在付款成功后重定向客户。

但是我收到 Cannot get /success

这是我的代码:

将参数发送到 stripe 的按钮 \/

const handleStripe = () =>{
            await fetch('/create-checkout-session', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(lista)
                    }).then((res)=>{
                        return res.json();
                    }).then((response)=>{
                        if(response){
                            return window.location.href = response.url
                        }
                    })
                }

服务器.js

require('dotenv').config()
const stripe = require('stripe')(process.env.REACT_APP_STRIPE_SECRET_KEY);
const express = require('express');
const app = express();
app.use(express.json())
app.use(express.static('public'));

const success = 'http://localhost:3000/success';
const cancel = 'http://localhost:3000/cancel';
const YOUR_DOMAIN = 'http://localhost:3000';

app.post('/create-checkout-session', async (req, res) => {
  const lineItems = req.body
  console.log(req.body)
  const session = await stripe.checkout.sessions.create({
    line_items: lineItems ,
    mode: 'payment',
    success_url: `${success}`
    }),
    cancel_url: `${cancel}`,
  });
  return res.json({url: session.url});
});

app.listen(3000, () => console.log('Running on port 3000'));

我尝试更改我的 server.js 来修复它,但我找不到任何解决方案

reactjs node.js express stripe-payments payment-gateway
1个回答
0
投票

您是否有在

http://localhost:3000/success
路径上提供服务的页面?根据您到目前为止共享的代码,您似乎正在端口
:3000
上运行服务器,但它没有任何处理
/success
路由上的请求的函数定义。

结账会话完成后,它会尝试重定向到该页面/路线,但找不到任何内容。因此出现了错误。您需要确保重定向的路径有效。

如果您不确定,可以参考 YouTube 教程:https://www.youtube.com/watch?v=VQ5jccnZ2Ow

© www.soinside.com 2019 - 2024. All rights reserved.