Redirect to an external url and send data as body params using express and react

Viewed 1089

I have a form in the frontend which post it's data to the API in the backend. Then, I add additional parameters i.e. private keys to it and make a JSON object. After this, I have to redirect to an external URL(Payumoney) while passing the JSON object to make a payment.

I can't add the data as query parameters as it contains sensitive information.

App.js (form)

<form action="/api/payumoney" method="POST">
    <label>
      FirstName:
      <input type="text" name="firstname" onChange={handleChange} value={formValue.firstname} />
    </label>
    <label>
      TxnID:
      <input type="text" name="txnid" onChange={handleChange} value={formValue.txnid} />
    </label>
    <label>
      Amount:
      <input type="text" name="amount" onChange={handleChange} value={formValue.amount} />
    </label>
    <label>
      ProductInfo:
      <input type="text" name="productinfo" onChange={handleChange} value={formValue.productinfo} />
    </label>
    <label>
      Email:
      <input type="email" name="email" onChange={handleChange} value={formValue.email} />
    </label>    

    <input type="submit" value="Submit" />
  </form>

index.js

app.post("/api/payumoney", urlencodedParser, (req,res) => {

    const pd = req.body;  //JSON Object that we've to parse to the external URL
    const url = "https://sandboxsecure.payu.in/_payment";

    //Here goes something that can redirect to the URL and pass the JSON object
 }

index.js (using request) -- It does the job of redirection and passing the data to it but the payment feature doesn't work (image attached)

app.post("/api/payumoney", urlencodedParser, (req,res) => {

    const pd = req.body;  //JSON Object that we've to parse to the external URL
    const url = "https://sandboxsecure.payu.in/_payment";
    request.post({
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      },
      uri: 'https://sandboxsecure.payu.in/_payment', //Testing URL
      form: pd,
    }, function (error, httpRes, body) {
      if (error){
        console.log("Error",error);
        res.status(400).json(
            {
                status: false, 
                message: error
            }
        );
     }
      if (httpRes.statusCode === 200) {
          res.send(body);
      } else if (httpRes.statusCode >= 300 && httpRes.statusCode <= 400) {
          res.redirect(httpRes.headers.location.toString());
          console.log("error 300 and 400");
    }
 })
}

After using request to parse data and redirect to Payumoney URL :

payuMoney payment website error

I'm using proxy to have the same origin for both client and server endpoints.

Thank you!

0 Answers
Related