[英]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并重定向到它并进行付款。
我在这里尝试了三种方法。
<form action="https://sandboxsecure.payu.in/_payment" method="POST" >
-- 这种情况工作正常但很危险,因为它会暴露私有钥匙<form action="/api/payumoney" method="POST" >
将 post 请求发送到后端 API -- 这会重定向到支付页面,但不会将表单中的数据发送到后端。应用程序.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.