簡體   English   中英

將反應表單中的數據傳遞給 expressJS 並重定向到 PayuMoney 網站以進行付款

[英]Passing data from a react form to expressJS and also redirecting to the PayuMoney website to make payment

問題描述:我正在嘗試將 payuMoney 集成到使用 ReactJS NodeJS 和 express 的網站中。 我有一個從用戶那里獲取輸入的表格。 What I'm trying to do is pass the data from react form to API which is in index.js where we request the test PayuMoney URL ie https://sandboxsecure.payu.in/_payment and get a body in response which is the付款頁面的 HTML 代碼。

我想要實現的目標:從用戶那里獲取輸入數據,將其提供給后端 API,在那里我將添加另一個私鑰並生成 hash 字符串。 使用表格請求 PayuMoney 測試 URL 即https://sandboxsecure.payu.in/_payment並重定向到它並進行付款。

我在這里嘗試了三種方法。

  1. 首先是直接從前端發送數據到測試 URL 使用<form action="https://sandboxsecure.payu.in/_payment" method="POST" > -- 這種情況工作正常但很危險,因為它會暴露私有鑰匙
  2. 第二個是使用<form action="/api/payumoney" method="POST" >將 post 請求發送到后端 API -- 這會重定向到支付頁面,但不會將表單中的數據發送到后端。
  3. 第三是使用使用 e.preventDefault() 的處理程序 function 對“api/payumoney”使用 axios/fetch POST 請求——這個將數據發送到后端,甚至向 PayuMoney 測試 URL 發出請求,但沒有重定向到支付頁面。

應用程序.js

function handleClick(e) {
var pd = {
  key: formValue.key,
  salt: formValue.salt,
  txnid: formValue.txnid,
  amount: formValue.amount,
  firstname: formValue.firstname,
  lastname: formValue.lastname,
  email: formValue.email,
  phone: formValue.phone,
  productinfo: formValue.productinfo,
  service_provider: formValue.service_provider,
  surl: formValue.surl,
  furl: formValue.furl,
  hash: ''
};


axios.post("/api/payumoney",{
  pd
}).then((res)=> {
  console.log(res);
}).catch((error)=>{
  console.log(error);
});
}


return (
  <div className="App">

  <form onSubmit={handleClick} method="POST" action="/api/payumoney">
    <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>
    <label>
      Phone:
      <input type="text" name="phone" onChange={handleChange} value={formValue.phone} />
    </label>
    <label>
      Hash:
      <input type="text" name="hash" onChange={handleChange} value={formValue.hash} />
    </label>
    <input type="hidden" id="key" name="key" value="MERCHANTKEYVALUE"></input>
    <input type="hidden" id="salt" name="salt" value="MERCHANTSALT"></input>
    <input type="hidden" id="surl" name="surl" value="/payment/success"></input>
    <input type="hidden" id="furl" name="furl" value="/payment/fail"></input>

    <input type="submit" value="Submit" />
  </form>
</div>
);

index.js

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

 if (!req.body.txnid || !req.body.amount || !req.body.productinfo || !req.body.firstname || !req.body.email) {
     res.status(400).json("Mandatory fields missing");
 }
var pd = req.body;

var hashString = pd.key + '|' + pd.txnid + '|' + pd.amount + '|' + pd.productinfo + '|' + pd.firstname + '|' + pd.email + '|' + '||||||||||' + pd.salt; // Your salt value
var sha = new jsSHA('SHA-512', "TEXT");
sha.update(hashString);
var hash = sha.getHash("HEX");
pd.hash = hash;

request.post({
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
    url: '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");
    }
})
})

我正在使用代理來為客戶端和服務器端點提供相同的來源。

謝謝:)

解決方案:

添加正文解析器

var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });


app.post("/api/payumoney", urlencodedParser, (req,res) => {
   console.log(req.body);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM