【问题标题】:How to access Node Express server using proxy once deployed to Heroku?部署到 Heroku 后如何使用代理访问 Node Express 服务器?
【发布时间】:2019-05-23 11:45:01
【问题描述】:

我如何告诉我的应用当它在生产中并且贝宝触发“return_url”时它应该在服务器端而不是在客户端寻找“/success”?

我已经设置了一个快递服务器来处理 PayPal 付款请求。当 post 请求通过快递发送到“localhost:5000/pay”时,我处理逻辑并被重定向到用户输入详细信息的 PayPal。然后将它们重定向到“localhost:5000/success”,使用 paypal 返回的令牌执行付款。这在本地工作正常,但在 heroku 上不行。问题是返回到“/success”页面。

从下面的代码可以看出,paypal 要求我引用一个 return_url(为成功交易提供的 url)。在 heroku 中,服务器没有在 'locahost:5000' 上运行,所以它只是抛出一个错误。或者,提供后跟“/success”的实际 url 不会触发我在 react 中设置的代理,因此它会尝试在我的客户端找到“/success”——这当然不存在并触发 404。

创建付款的代码:

    const create_payment_json = {

            "intent": "sale",
            "payer": {
                "payment_method": "paypal"
            },
            "redirect_urls": {
                "return_url": `http://localhost:5000/success`,
                "cancel_url": `http://localhost:5000/cancel`
            },
            "transactions": [{
                "item_list": {
                    "items": content
                },
                "amount": {
                    "currency": "USD",
                    "total": productSelectTotal,
                    "details":{
                        "subtotal":productSubTotal,
                        "shipping":shipping
                    }
                },
                "description": "first tea added to the store"
            }]
        }

重定向到服务器/成功后执行付款

    app.get('/success', (req, res)=> {
            const payerId = req.query.PayerID
            const paymentId = req.query.paymentId

            const execute_payment_json = {
                "payer_id": payerId,
                "transactions": [{
                    "item_list": {
                    "items": content
                },
                    "amount": {
                        "currency":"USD",
                        "total": productSelectTotal,
                        "details":{
                            "subtotal":productSubTotal,
                            "shipping":shipping
                        }
                    }
                }]
            }

            paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
                if (error) {
                    console.log(error.response);
                    throw error;
                } else {
                    console.log(JSON.stringify(payment));
                    res.send('Payment Successful')
                }
            });

        })

【问题讨论】:

    标签: node.js reactjs express heroku


    【解决方案1】:

    我最终通过在我的节点服务器中添加一个 routes/api/... 文件夹解决了这个问题。当 rect 进行调用时,它改为 api/paypal/success 并且没有混淆。

    【讨论】:

      【解决方案2】:

      您可以使用以下方式动态获取您的 URL:How to get the full url in Express?

      只需调整它以连接您的 return_url 上的“/成功”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-22
        • 1970-01-01
        • 2017-02-23
        • 2022-01-09
        • 2018-02-09
        • 2022-01-04
        • 2020-07-23
        • 2018-03-16
        相关资源
        最近更新 更多