Rev 6463 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import in.shop2020.config.ConfigException;import in.shop2020.datalogger.EventType;import in.shop2020.payments.Attribute;import in.shop2020.payments.Payment;import in.shop2020.payments.PaymentException;import in.shop2020.payments.PaymentStatus;import in.shop2020.serving.services.CommonPaymentService;import in.shop2020.thrift.clients.PaymentClient;import in.shop2020.thrift.clients.TransactionClient;import in.shop2020.thrift.clients.UserClient;import in.shop2020.thrift.clients.config.ConfigClient;import in.shop2020.utils.DataLogger;import java.io.IOException;import java.io.StringReader;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.TreeMap;import javax.servlet.http.HttpServletRequest;import javax.swing.text.Document;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.apache.log4j.Logger;import org.apache.thrift.TException;import org.w3c.dom.*;import org.xml.sax.InputSource;import org.xml.sax.SAXException;@SuppressWarnings("serial")public class InnovitiPayResponseController extends BaseController{private static Logger log = Logger.getLogger(Class.class);private static final String TXN_REF_NO = "txnRefNo";private static final String AUTH_CODE = "authCode";private static String successUrl;private static String errorUrl;/*** The secret key used to decode RC4 encoded data.*/private static String accountKey;private static String salt;private String redirectUrl;static{try {successUrl = ConfigClient.getClient().get("ebs_success_url");errorUrl = ConfigClient.getClient().get("ebs_error_url");accountKey = ConfigClient.getClient().get("innoviti_account_id");salt = ConfigClient.getClient().get("innoviti_secret_key");} catch (ConfigException e) {log.error("Unable to get success and error usr info from config server.");}}private Map<String, String> paymentParams = new TreeMap<String, String>();public String create() {// String gatewayTxnId = request.getParameter("mihpayid");// String status = request.getParameter("status");// String key = request.getParameter("key");// String mode = request.getParameter("mode");// String txnid = request.getParameter("txnid");// String amount = request.getParameter("amount");// String hash = request.getParameter("hash");// String bank_ref_num = request.getParameter("bank_ref_num");// String PG_TYPE = request.getParameter("PG_TYPE");// String Error = request.getParameter("Error");// String unmappedstatus = request.getParameter("unmappedstatus");//updatePaymentParams(request.getParameter("transresponse"));PaymentClient paymentServiceClient = null;TransactionClient transactionServiceClient = null;UserClient userServiceClient = null;try {paymentServiceClient = new PaymentClient();transactionServiceClient = new TransactionClient();userServiceClient = new UserClient();} catch (Exception e) {log.error("Unable to initialize one of the clients", e);}long merchantPaymentId = Long.parseLong(paymentParams.get("orderId"));String gatewayPaymentId = paymentParams.get("UnipayId");//double amount = Double.parseDouble(paymentParams.get("amount"));String gatewayTxnStatus = paymentParams.get("resCode");String gatewayTxnStatusDescription = paymentParams.get("resmsg");String authCode = paymentParams.get("authCode");String txnRefNo = paymentParams.get("txnRefNo");String txnDate = paymentParams.get("txnDate");String txnTime = paymentParams.get("txnTime");List<Attribute> attributes = new ArrayList<Attribute>();attributes.add(new Attribute(TXN_REF_NO, txnRefNo));attributes.add(new Attribute(AUTH_CODE, authCode));Payment payment = null;Long txnId = null;try {payment = paymentServiceClient.getClient().getPayment(merchantPaymentId);txnId = payment.getMerchantTxnId();} catch (PaymentException e1) {log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);} catch (TException e1) {log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);}// if(!validatePaymentParams(amount, payment, hash)){// this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;// return "index";// }if(gatewayTxnStatus.equalsIgnoreCase("00")){//Update payment status as authorized if payment is authorized.try {paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,"", gatewayTxnStatus, gatewayTxnStatusDescription, "", "", "", "", PaymentStatus.AUTHORIZED, "", attributes);} catch (PaymentException e) {log.error("Unable to mark the payment as authorized", e);} catch (TException e) {log.error("Unable to mark the payment as authorized", e);}CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient, false);this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;}else{try {paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,"", gatewayTxnStatus, "Payment Failed at PG", "", "", "", "", PaymentStatus.FAILED, "", attributes);} catch (PaymentException e) {log.error("Unable to mark the payment as failed", e);} catch (TException e) {log.error("Unable to mark the payment as failed", e);}CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);DataLogger.logData(EventType.PAYMENT_FAILURE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,gatewayTxnStatus, "Payment Failed at PG");this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;}log.info("User will be redirected to: " + this.redirectUrl);return "index";}private boolean validatePaymentParams(double returnedAmount, Payment payment, String hash){if(!(payment != null && Math.abs(payment.getAmount() - returnedAmount) <= 0.50 && hash.equals(getSecureHash()))){// We did not request this payment or the authorised amount is different.log.error("Checks and balance failed on returned data");return false;}return true;}public String getSecureHash(){try{String pass = salt + "|" + paymentParams.get("status") + "|||||||||||" + paymentParams.get("email") + "|" + paymentParams.get("firstname") + "|" + paymentParams.get("productinfo") + "|" + paymentParams.get("amount") + "|" + paymentParams.get("txnid") + "|" + accountKey;System.out.println(pass);MessageDigest md = MessageDigest.getInstance("SHA-512");md.update(pass.getBytes(), 0, pass.getBytes().length);byte[] mdbytes = md.digest();// convert the byte to hex format methodStringBuffer sb = new StringBuffer();for (int i = 0; i < mdbytes.length; i++) {sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));}return sb.toString();}catch(NoSuchAlgorithmException nsae){log.error("No such algorithm exception");return null;}}private void updatePaymentParams(String xmlString){System.out.println(xmlString);try{DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();InputSource is = new InputSource();is.setCharacterStream(new StringReader(xmlString));org.w3c.dom.Document doc = db.parse(is);NodeList nodes = doc.getElementsByTagName("sres");Element element = (Element) nodes.item(0);NodeList name = element.getElementsByTagName("orderId");Element line = (Element) name.item(0);System.out.println("orderId: " + getCharacterDataFromElement(line));paymentParams.put("orderId", getCharacterDataFromElement(line));name = element.getElementsByTagName("merchantId");line = (Element) name.item(0);System.out.println("merchantId: " + getCharacterDataFromElement(line));paymentParams.put("merchantId", getCharacterDataFromElement(line));name = element.getElementsByTagName("resCode");line = (Element) name.item(0);System.out.println("resCode: " + getCharacterDataFromElement(line));paymentParams.put("resCode", getCharacterDataFromElement(line));name = element.getElementsByTagName("resmsg");line = (Element) name.item(0);System.out.println("resmsg: " + getCharacterDataFromElement(line));paymentParams.put("resmsg", getCharacterDataFromElement(line));nodes = element.getElementsByTagName("respDet");element = (Element) nodes.item(0);name = element.getElementsByTagName("txnRefNo");line = (Element) name.item(0);System.out.println("txnRefNo: " + getCharacterDataFromElement(line));paymentParams.put("txnRefNo", getCharacterDataFromElement(line));name = element.getElementsByTagName("UnipayId");line = (Element) name.item(0);System.out.println("UnipayId: " + getCharacterDataFromElement(line));paymentParams.put("UnipayId", getCharacterDataFromElement(line));name = element.getElementsByTagName("rrnNo");line = (Element) name.item(0);System.out.println("rrnNo: " + getCharacterDataFromElement(line));paymentParams.put("rrnNo", getCharacterDataFromElement(line));name = element.getElementsByTagName("authCode");line = (Element) name.item(0);System.out.println("authCode: " + getCharacterDataFromElement(line));paymentParams.put("authCode", getCharacterDataFromElement(line));name = element.getElementsByTagName("txnDate");line = (Element) name.item(0);System.out.println("txnDate: " + getCharacterDataFromElement(line));paymentParams.put("txnDate", getCharacterDataFromElement(line));name = element.getElementsByTagName("txnTime");line = (Element) name.item(0);System.out.println("txnTime: " + getCharacterDataFromElement(line));paymentParams.put("txnTime", getCharacterDataFromElement(line));}catch (Exception e) {// TODO: handle exception// throws ParserConfigurationException, SAXException, IOException}}public static String getCharacterDataFromElement(Element e) {Node child = e.getFirstChild();if (child instanceof CharacterData) {CharacterData cd = (CharacterData) child;return cd.getData();}return "";}public String getRedirectUrl(){return this.redirectUrl;}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;}public Map<String, String> getPaymentParams() {return paymentParams;}}