| 6390 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.config.ConfigException;
|
|
|
4 |
import in.shop2020.datalogger.EventType;
|
|
|
5 |
import in.shop2020.payments.Attribute;
|
|
|
6 |
import in.shop2020.payments.Payment;
|
|
|
7 |
import in.shop2020.payments.PaymentException;
|
|
|
8 |
import in.shop2020.payments.PaymentStatus;
|
|
|
9 |
import in.shop2020.serving.services.CommonPaymentService;
|
|
|
10 |
import in.shop2020.thrift.clients.PaymentClient;
|
|
|
11 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
12 |
import in.shop2020.thrift.clients.UserClient;
|
|
|
13 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
|
|
14 |
import in.shop2020.utils.DataLogger;
|
|
|
15 |
|
|
|
16 |
import java.io.StringReader;
|
|
|
17 |
import java.security.MessageDigest;
|
|
|
18 |
import java.security.NoSuchAlgorithmException;
|
|
|
19 |
import java.util.ArrayList;
|
|
|
20 |
import java.util.List;
|
|
|
21 |
import java.util.Map;
|
|
|
22 |
import java.util.TreeMap;
|
|
|
23 |
|
|
|
24 |
import javax.servlet.http.HttpServletRequest;
|
|
|
25 |
import javax.xml.parsers.DocumentBuilder;
|
|
|
26 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
27 |
|
|
|
28 |
import org.apache.log4j.Logger;
|
|
|
29 |
import org.apache.thrift.TException;
|
|
|
30 |
import org.w3c.dom.*;
|
|
|
31 |
import org.xml.sax.InputSource;
|
|
|
32 |
|
|
|
33 |
@SuppressWarnings("serial")
|
|
|
34 |
public class InnovitiPayResponseController extends BaseController{
|
|
|
35 |
|
|
|
36 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
37 |
|
|
|
38 |
private static final String TXN_REF_NO = "txnRefNo";
|
|
|
39 |
private static final String AUTH_CODE = "authCode";
|
|
|
40 |
|
|
|
41 |
private static String successUrl;
|
|
|
42 |
private static String errorUrl;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* The secret key used to decode RC4 encoded data.
|
|
|
46 |
*/
|
|
|
47 |
private static String accountKey;
|
|
|
48 |
|
|
|
49 |
private static String salt;
|
|
|
50 |
|
|
|
51 |
private String redirectUrl;
|
|
|
52 |
|
|
|
53 |
static{
|
|
|
54 |
try {
|
|
|
55 |
successUrl = ConfigClient.getClient().get("ebs_success_url");
|
|
|
56 |
errorUrl = ConfigClient.getClient().get("ebs_error_url");
|
|
|
57 |
accountKey = ConfigClient.getClient().get("innoviti_account_id");
|
|
|
58 |
salt = ConfigClient.getClient().get("innoviti_secret_key");
|
|
|
59 |
} catch (ConfigException e) {
|
|
|
60 |
log.error("Unable to get success and error usr info from config server.");
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
private Map<String, String> paymentParams = new TreeMap<String, String>();
|
|
|
65 |
|
|
|
66 |
public String create() {
|
|
|
67 |
// String gatewayTxnId = request.getParameter("mihpayid");
|
|
|
68 |
// String status = request.getParameter("status");
|
|
|
69 |
// String key = request.getParameter("key");
|
|
|
70 |
// String mode = request.getParameter("mode");
|
|
|
71 |
// String txnid = request.getParameter("txnid");
|
|
|
72 |
// String amount = request.getParameter("amount");
|
|
|
73 |
// String hash = request.getParameter("hash");
|
|
|
74 |
// String bank_ref_num = request.getParameter("bank_ref_num");
|
|
|
75 |
// String PG_TYPE = request.getParameter("PG_TYPE");
|
|
|
76 |
// String Error = request.getParameter("Error");
|
|
|
77 |
// String unmappedstatus = request.getParameter("unmappedstatus");
|
|
|
78 |
//
|
|
|
79 |
|
|
|
80 |
updatePaymentParams(request.getParameter("transresponse"));
|
|
|
81 |
|
|
|
82 |
PaymentClient paymentServiceClient = null;
|
|
|
83 |
TransactionClient transactionServiceClient = null;
|
|
|
84 |
UserClient userServiceClient = null;
|
|
|
85 |
try {
|
|
|
86 |
paymentServiceClient = new PaymentClient();
|
|
|
87 |
transactionServiceClient = new TransactionClient();
|
|
|
88 |
userServiceClient = new UserClient();
|
|
|
89 |
} catch (Exception e) {
|
|
|
90 |
log.error("Unable to initialize one of the clients", e);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
long merchantPaymentId = Long.parseLong(paymentParams.get("orderId"));
|
|
|
95 |
String gatewayPaymentId = paymentParams.get("UnipayId");
|
|
|
96 |
//double amount = Double.parseDouble(paymentParams.get("amount"));
|
|
|
97 |
String gatewayTxnStatus = paymentParams.get("resCode");
|
|
|
98 |
String gatewayTxnStatusDescription = paymentParams.get("resmsg");
|
|
|
99 |
|
|
|
100 |
String authCode = paymentParams.get("authCode");
|
|
|
101 |
String txnRefNo = paymentParams.get("txnRefNo");
|
|
|
102 |
String txnDate = paymentParams.get("txnDate");
|
|
|
103 |
String txnTime = paymentParams.get("txnTime");
|
| 6432 |
rajveer |
104 |
String hash = paymentParams.get("checkSum");
|
| 6390 |
rajveer |
105 |
|
|
|
106 |
List<Attribute> attributes = new ArrayList<Attribute>();
|
|
|
107 |
attributes.add(new Attribute(TXN_REF_NO, txnRefNo));
|
|
|
108 |
attributes.add(new Attribute(AUTH_CODE, authCode));
|
|
|
109 |
|
|
|
110 |
Payment payment = null;
|
|
|
111 |
Long txnId = null;
|
|
|
112 |
try {
|
|
|
113 |
payment = paymentServiceClient.getClient().getPayment(merchantPaymentId);
|
|
|
114 |
txnId = payment.getMerchantTxnId();
|
|
|
115 |
} catch (PaymentException e1) {
|
|
|
116 |
log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);
|
|
|
117 |
} catch (TException e1) {
|
|
|
118 |
log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);
|
|
|
119 |
}
|
|
|
120 |
|
| 6432 |
rajveer |
121 |
if(!validatePaymentParams(merchantPaymentId, gatewayPaymentId, gatewayTxnStatus, gatewayTxnStatusDescription, hash)){
|
|
|
122 |
this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
|
|
|
123 |
return "index";
|
|
|
124 |
}
|
| 6390 |
rajveer |
125 |
|
|
|
126 |
if(gatewayTxnStatus.equalsIgnoreCase("00")){
|
|
|
127 |
//Update payment status as authorized if payment is authorized.
|
|
|
128 |
try {
|
|
|
129 |
paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
|
| 6958 |
rajveer |
130 |
"", gatewayTxnStatus, gatewayTxnStatusDescription, "", authCode, txnRefNo, "", PaymentStatus.SUCCESS, txnDate + " " + txnTime, attributes);
|
| 6390 |
rajveer |
131 |
} catch (PaymentException e) {
|
|
|
132 |
log.error("Unable to mark the payment as authorized", e);
|
|
|
133 |
} catch (TException e) {
|
|
|
134 |
log.error("Unable to mark the payment as authorized", e);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient, false);
|
|
|
139 |
this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
}else{
|
|
|
143 |
try {
|
|
|
144 |
paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
|
| 6432 |
rajveer |
145 |
"", gatewayTxnStatus, gatewayTxnStatusDescription, "", authCode, txnRefNo, "", PaymentStatus.FAILED, txnDate + " " + txnTime, attributes);
|
| 6390 |
rajveer |
146 |
} catch (PaymentException e) {
|
|
|
147 |
log.error("Unable to mark the payment as failed", e);
|
|
|
148 |
} catch (TException e) {
|
|
|
149 |
log.error("Unable to mark the payment as failed", e);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
|
| 12616 |
anikendra |
153 |
// DataLogger.logData(EventType.PAYMENT_FAILURE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,
|
|
|
154 |
// gatewayTxnStatus, "Payment Failed at PG");
|
| 6390 |
rajveer |
155 |
|
|
|
156 |
this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
log.info("User will be redirected to: " + this.redirectUrl);
|
|
|
160 |
return "index";
|
|
|
161 |
}
|
|
|
162 |
|
| 6432 |
rajveer |
163 |
private boolean validatePaymentParams(long merchantPaymentId, String gatewayPaymentId, String gatewayTxnStatus, String gatewayTxnStatusDescription, String hash) {
|
|
|
164 |
if(!hash.equals(getSecureHash(merchantPaymentId, gatewayPaymentId, gatewayTxnStatus, gatewayTxnStatusDescription))){
|
| 6390 |
rajveer |
165 |
// We did not request this payment or the authorised amount is different.
|
|
|
166 |
log.error("Checks and balance failed on returned data");
|
|
|
167 |
return false;
|
|
|
168 |
}
|
|
|
169 |
return true;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
|
| 6432 |
rajveer |
173 |
public String getSecureHash(long merchantPaymentId, String gatewayPaymentId, String gatewayTxnStatus, String gatewayTxnStatusDescription){
|
| 6390 |
rajveer |
174 |
try{
|
| 6432 |
rajveer |
175 |
String pass = merchantPaymentId + "|" + accountKey + "|" + gatewayPaymentId + "|" + gatewayTxnStatus + "|" + gatewayTxnStatusDescription + "|" + salt;
|
| 6390 |
rajveer |
176 |
System.out.println(pass);
|
| 6432 |
rajveer |
177 |
MessageDigest md = MessageDigest.getInstance("MD5");
|
| 6390 |
rajveer |
178 |
md.update(pass.getBytes(), 0, pass.getBytes().length);
|
|
|
179 |
byte[] mdbytes = md.digest();
|
|
|
180 |
// convert the byte to hex format method
|
|
|
181 |
StringBuffer sb = new StringBuffer();
|
|
|
182 |
for (int i = 0; i < mdbytes.length; i++) {
|
|
|
183 |
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
|
|
|
184 |
}
|
|
|
185 |
return sb.toString();
|
|
|
186 |
}catch(NoSuchAlgorithmException nsae){
|
|
|
187 |
log.error("No such algorithm exception");
|
|
|
188 |
return null;
|
|
|
189 |
}
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
private void updatePaymentParams(String xmlString){
|
|
|
193 |
System.out.println(xmlString);
|
|
|
194 |
try{
|
|
|
195 |
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
|
|
196 |
InputSource is = new InputSource();
|
|
|
197 |
is.setCharacterStream(new StringReader(xmlString));
|
|
|
198 |
|
|
|
199 |
org.w3c.dom.Document doc = db.parse(is);
|
|
|
200 |
NodeList nodes = doc.getElementsByTagName("sres");
|
|
|
201 |
|
|
|
202 |
Element element = (Element) nodes.item(0);
|
|
|
203 |
|
|
|
204 |
NodeList name = element.getElementsByTagName("orderId");
|
|
|
205 |
Element line = (Element) name.item(0);
|
|
|
206 |
System.out.println("orderId: " + getCharacterDataFromElement(line));
|
|
|
207 |
paymentParams.put("orderId", getCharacterDataFromElement(line));
|
|
|
208 |
|
|
|
209 |
name = element.getElementsByTagName("merchantId");
|
|
|
210 |
line = (Element) name.item(0);
|
|
|
211 |
System.out.println("merchantId: " + getCharacterDataFromElement(line));
|
|
|
212 |
paymentParams.put("merchantId", getCharacterDataFromElement(line));
|
|
|
213 |
|
|
|
214 |
name = element.getElementsByTagName("resCode");
|
|
|
215 |
line = (Element) name.item(0);
|
|
|
216 |
System.out.println("resCode: " + getCharacterDataFromElement(line));
|
|
|
217 |
paymentParams.put("resCode", getCharacterDataFromElement(line));
|
|
|
218 |
|
|
|
219 |
name = element.getElementsByTagName("resmsg");
|
|
|
220 |
line = (Element) name.item(0);
|
|
|
221 |
System.out.println("resmsg: " + getCharacterDataFromElement(line));
|
|
|
222 |
paymentParams.put("resmsg", getCharacterDataFromElement(line));
|
|
|
223 |
|
| 6432 |
rajveer |
224 |
name = element.getElementsByTagName("checkSum");
|
|
|
225 |
line = (Element) name.item(0);
|
|
|
226 |
System.out.println("checkSum: " + getCharacterDataFromElement(line));
|
|
|
227 |
paymentParams.put("checkSum", getCharacterDataFromElement(line));
|
| 6390 |
rajveer |
228 |
|
| 6463 |
rajveer |
229 |
nodes = element.getElementsByTagName("respEmi");
|
|
|
230 |
Element emiElement = (Element) nodes.item(0);
|
|
|
231 |
|
|
|
232 |
nodes = emiElement.getElementsByTagName("isCtx");
|
|
|
233 |
emiElement = (Element) nodes.item(0);
|
|
|
234 |
|
|
|
235 |
name = emiElement.getElementsByTagName("flag");
|
|
|
236 |
line = (Element) name.item(0);
|
|
|
237 |
System.out.println("flag: " + getCharacterDataFromElement(line));
|
|
|
238 |
paymentParams.put("flag", getCharacterDataFromElement(line));
|
|
|
239 |
|
|
|
240 |
name = emiElement.getElementsByTagName("processingFee");
|
|
|
241 |
line = (Element) name.item(0);
|
|
|
242 |
System.out.println("processingFee: " + getCharacterDataFromElement(line));
|
|
|
243 |
paymentParams.put("processingFee", getCharacterDataFromElement(line));
|
|
|
244 |
|
|
|
245 |
name = emiElement.getElementsByTagName("interest");
|
|
|
246 |
line = (Element) name.item(0);
|
|
|
247 |
System.out.println("interest: " + getCharacterDataFromElement(line));
|
|
|
248 |
paymentParams.put("interest", getCharacterDataFromElement(line));
|
|
|
249 |
|
|
|
250 |
|
| 6390 |
rajveer |
251 |
nodes = element.getElementsByTagName("respDet");
|
|
|
252 |
element = (Element) nodes.item(0);
|
|
|
253 |
|
|
|
254 |
name = element.getElementsByTagName("txnRefNo");
|
|
|
255 |
line = (Element) name.item(0);
|
|
|
256 |
System.out.println("txnRefNo: " + getCharacterDataFromElement(line));
|
|
|
257 |
paymentParams.put("txnRefNo", getCharacterDataFromElement(line));
|
|
|
258 |
|
|
|
259 |
|
|
|
260 |
name = element.getElementsByTagName("UnipayId");
|
|
|
261 |
line = (Element) name.item(0);
|
|
|
262 |
System.out.println("UnipayId: " + getCharacterDataFromElement(line));
|
|
|
263 |
paymentParams.put("UnipayId", getCharacterDataFromElement(line));
|
|
|
264 |
|
|
|
265 |
name = element.getElementsByTagName("rrnNo");
|
|
|
266 |
line = (Element) name.item(0);
|
|
|
267 |
System.out.println("rrnNo: " + getCharacterDataFromElement(line));
|
|
|
268 |
paymentParams.put("rrnNo", getCharacterDataFromElement(line));
|
|
|
269 |
|
|
|
270 |
name = element.getElementsByTagName("authCode");
|
|
|
271 |
line = (Element) name.item(0);
|
|
|
272 |
System.out.println("authCode: " + getCharacterDataFromElement(line));
|
|
|
273 |
paymentParams.put("authCode", getCharacterDataFromElement(line));
|
|
|
274 |
|
|
|
275 |
name = element.getElementsByTagName("txnDate");
|
|
|
276 |
line = (Element) name.item(0);
|
|
|
277 |
System.out.println("txnDate: " + getCharacterDataFromElement(line));
|
|
|
278 |
paymentParams.put("txnDate", getCharacterDataFromElement(line));
|
|
|
279 |
|
|
|
280 |
name = element.getElementsByTagName("txnTime");
|
|
|
281 |
line = (Element) name.item(0);
|
|
|
282 |
System.out.println("txnTime: " + getCharacterDataFromElement(line));
|
|
|
283 |
paymentParams.put("txnTime", getCharacterDataFromElement(line));
|
| 6432 |
rajveer |
284 |
|
| 6390 |
rajveer |
285 |
}catch (Exception e) {
|
|
|
286 |
// TODO: handle exception
|
|
|
287 |
// throws ParserConfigurationException, SAXException, IOException
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
public static String getCharacterDataFromElement(Element e) {
|
|
|
292 |
Node child = e.getFirstChild();
|
|
|
293 |
if (child instanceof CharacterData) {
|
|
|
294 |
CharacterData cd = (CharacterData) child;
|
|
|
295 |
return cd.getData();
|
|
|
296 |
}
|
|
|
297 |
return "";
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
public String getRedirectUrl(){
|
|
|
301 |
return this.redirectUrl;
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
@Override
|
|
|
305 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
306 |
this.request = request;
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
public Map<String, String> getPaymentParams() {
|
|
|
310 |
return paymentParams;
|
|
|
311 |
}
|
|
|
312 |
}
|