简体   繁体   中英

How to access Node Express server using proxy once deployed to Heroku?

How do I tell my app that when it is in production, and paypal triggers the 'return_url' it should look for '/success' on the server side rather than in the client?

I have set up an express server to handle PayPal payment requests. When a post request is served to 'localhost:5000/pay' on express I handle the logic and am redirected to PayPal where the user enters their details. They are then redirected to 'localhost:5000/success' where the payment is executed using the token returned by paypal. This works fine locally, but not on heroku. The issue is the return to the '/success' page.

As you can see from the code below, paypal asks me to reference a return_url (the url served for a successful transaction). In heroku the server is not running on 'locahost:5000' so it just throws an error. Alternatively serving the actual url followed by '/success' doesn't trigger the proxy I have set up in react and so it tries to find '/success' in my client side - which of course isn't there and triggers a 404.

The code to create payment:

    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"
            }]
        }

execution of payment once redirected to server/success

    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')
                }
            });

        })

You can dynamically get your URL in express using this: How to get the full url in Express?

Just adapt it to concat the '/success' on your return_url

I ended up solving the issue by adding a routes/api/... folder in my node server. When rect made the call, it made it to api/paypal/success instead and there was no confusion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM