Rev 6062 | Rev 12788 | 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.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 org.apache.log4j.Logger;import org.apache.thrift.TException;@SuppressWarnings("serial")public class PayuPayResponseController extends BaseController{private static Logger log = Logger.getLogger(Class.class);private static final String BANK_REF_ID = "BankRefId";private static final String UNMAPPED_STATUS = "UnMappedStatus";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("payu_account_id");salt = ConfigClient.getClient().get("payu_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.getParameterMap());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("txnid"));String gatewayPaymentId = paymentParams.get("mihpayid");double amount = Double.parseDouble(paymentParams.get("amount"));String gatewayTxnStatus = paymentParams.get("status");String hash = paymentParams.get("hash");String bankRefId = paymentParams.get("bank_ref_num");String unmappedStatus = paymentParams.get("unmappedstatus");List<Attribute> attributes = new ArrayList<Attribute>();attributes.add(new Attribute(BANK_REF_ID, bankRefId));attributes.add(new Attribute(UNMAPPED_STATUS, unmappedStatus));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("SUCCESS")){//Update payment status as authorized if payment is authorized.try {paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,"", gatewayTxnStatus, "Transaction Authorized at PG", "", "", "", "", 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(Map map){for(Object key : map.keySet()){String keyStr = (String)key;String[] vals = (String[])map.get(keyStr);String value = vals[0];System.out.println("Key " + (String)key + " : " + value);paymentParams.put(keyStr, value);}}public String getRedirectUrl(){return this.redirectUrl;}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;}public Map<String, String> getPaymentParams() {return paymentParams;}}