简体   繁体   English

将反应表单中的数据传递给 expressJS 并重定向到 PayuMoney 网站以进行付款

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

Problem Description: I am trying to integrate payuMoney in a website which uses ReactJS NodeJS and express.问题描述:我正在尝试将 payuMoney 集成到使用 ReactJS NodeJS 和 express 的网站中。 I have a form for taking inputs from the user.我有一个从用户那里获取输入的表格。 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 code for the payment page. 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 代码。

What I'm trying to achieve: Take the input data from user, feed it to the backend API, where I'll add another private keys and generate a hash string.我想要实现的目标:从用户那里获取输入数据,将其提供给后端 API,在那里我将添加另一个私钥并生成 hash 字符串。 Request the PayuMoney test URL ie https://sandboxsecure.payu.in/_payment with the form and redirect to it and make the payment.使用表格请求 PayuMoney 测试 URL 即https://sandboxsecure.payu.in/_payment并重定向到它并进行付款。

I have tried three methods here.我在这里尝试了三种方法。

  1. First is directly sending data from the frontend to the test URL using <form action="https://sandboxsecure.payu.in/_payment" method="POST" > -- This case work fine but is dangerous because it would expose private keys首先是直接从前端发送数据到测试 URL 使用<form action="https://sandboxsecure.payu.in/_payment" method="POST" > -- 这种情况工作正常但很危险,因为它会暴露私有钥匙
  2. Second is sending the post request to backend API using <form action="/api/payumoney" method="POST" > -- This one redirects to the payment page but does not send the data from the form to the backend.第二个是使用<form action="/api/payumoney" method="POST" >将 post 请求发送到后端 API -- 这会重定向到支付页面,但不会将表单中的数据发送到后端。
  3. Third is using axios/fetch POST request to the "api/payumoney" using a handler function which uses e.preventDefault() -- This one sends the data to the backend and even makes a request to the PayuMoney Test URL but doesn't redirect to the payment page.第三是使用使用 e.preventDefault() 的处理程序 function 对“api/payumoney”使用 axios/fetch POST 请求——这个将数据发送到后端,甚至向 PayuMoney 测试 URL 发出请求,但没有重定向到支付页面。

App.js应用程序.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 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");
    }
})
})

I'm using proxy to have the same origin for both the client and server endpoints.我正在使用代理来为客户端和服务器端点提供相同的来源。

Thanks:)谢谢:)

Solution:解决方案:

Add body-parser添加正文解析器

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