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