Rev 2134 | Rev 2334 | 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.payments.Attribute;import in.shop2020.payments.Payment;import in.shop2020.payments.PaymentException;import in.shop2020.payments.PaymentService.Client;import in.shop2020.payments.PaymentStatus;import in.shop2020.serving.services.CommonPaymentService;import in.shop2020.thrift.clients.PaymentServiceClient;import in.shop2020.thrift.clients.TransactionServiceClient;import in.shop2020.thrift.clients.UserContextServiceClient;import in.shop2020.thrift.clients.config.ConfigClient;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.thrift.TException;import org.apache.log4j.Logger;public class HdfcPayResponseController implements ServletResponseAware, ServletRequestAware{private static final long serialVersionUID = 1L;private enum PaymentReturnStatus{CAPTURED("CAPTURED"),NOT_CAPTURED ("NOT CAPTURED"),CANCELLED ("CANCELLED"),DENIED_BY_RISK("DENIED BY RISK"),HOST_TIMEOUT("HOST TIMEOUT");private String value;PaymentReturnStatus(String value) {this.value = value;}public String value(){return this.value;}}HttpServletRequest request;HttpServletResponse response;//private static Logger log = LoggerFactory.getLogger(HdfcPayResponseController.class);private static Logger log = Logger.getLogger(Class.class);public static String successUrl;public static String errorUrl;public static String AMOUNT = "amt";public static String TRACKID = "trackid";public static String RESULT = "result";public static String AUTH = "auth";public static String TRANID = "tranid";public static String PAYMENTID = "paymentid";public static String REF = "ref";public static String POSTDATE = "postdate";public static String ERROR = "Error";public static String ERRORTEXT = "ErrorText";public static String UDF5 = "udf5";static{try {successUrl = ConfigClient.getClient().get("payment_success_url");errorUrl = ConfigClient.getClient().get("payment_error_url");} catch (ConfigException e) {log.error("Unable to get success and error usr info from config server.");}}String redirectUrl;public HdfcPayResponseController() {}public String create() throws IOException, SecurityException{log.info("Inside hdfc pay response Create");PaymentServiceClient paymentServiceClient = null;TransactionServiceClient transactionServiceClient = null;UserContextServiceClient userServiceClient = null;try{paymentServiceClient = new PaymentServiceClient();transactionServiceClient = new TransactionServiceClient();userServiceClient = new UserContextServiceClient();}catch(Exception e){//Nothing to worry. lets move forwardlog.error("Unable to initialize one of the clients");e.printStackTrace();}Long txnId = null;String paymentId = request.getParameter(PAYMENTID);String result = request.getParameter(RESULT);String trackId = request.getParameter(TRACKID);long merchantPaymentId = Long.parseLong(trackId);String amount = request.getParameter(AMOUNT);String errorText = request.getParameter(ERRORTEXT);//FIXME dump them somewhereString udf5=request.getParameter(UDF5);//FIXME hdfc is sending comma separated amount, which is very disappointing. May be we get more surprises moving forward.amount= amount.replace(",", "");Client paymentClient = paymentServiceClient.getClient();try {Payment payment = paymentClient.getPayment(merchantPaymentId);txnId = payment.getMerchantTxnId();if(!validatePaymentParams(paymentId, amount, udf5, payment))return "index";} catch (PaymentException e1) {log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId);e1.printStackTrace();} catch (TException e1) {log.error("Thrift exception. Check payment id "+ merchantPaymentId);e1.printStackTrace();}if (result != null && result.trim().equals(PaymentReturnStatus.CAPTURED.value())) {String message = "Payment successful";updatePaymentDetails(merchantPaymentId, message, PaymentStatus.SUCCESS, request, paymentClient);CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient);CommonPaymentService.sendTxnEmail(txnId, transactionServiceClient);this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;} else {updatePaymentDetails(merchantPaymentId, errorText, PaymentStatus.FAILED, request, paymentClient);CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;}return "index";}private boolean validatePaymentParams(String paymentId, String amount, String udf5, Payment payment) {long merchantPaymentId = payment.getPaymentId();String dbUdf5="";double dbAmount = payment.getAmount();for(Attribute attribute: payment.getAttributes()){if(attribute.getName().trim().equalsIgnoreCase(UDF5)){dbUdf5 = attribute.getValue();}}// verify 3 things: udf5, amount and paymentid//FIXME should we first dump the data and then verify these things. ??double returnedAmount = Double.parseDouble(amount);log.info(paymentId+ ":"+ payment.getGatewayPaymentId() + "\n" + returnedAmount + ":" + dbAmount + "\n" + dbUdf5 + ":" + udf5 );if(!(paymentId.equalsIgnoreCase(payment.getGatewayPaymentId()) && (Math.abs(dbAmount - returnedAmount) <= 0.50) && udf5.equalsIgnoreCase(dbUdf5))){log.error("Checks and balance failed on returned data");this.redirectUrl = errorUrl + "?paymentId="+merchantPaymentId;return false;}return true;}private void updatePaymentDetails(long merchantPaymentId, String message, PaymentStatus status, HttpServletRequest req, Client paymentClient) {String paymentId = request.getParameter(PAYMENTID);String result = request.getParameter(RESULT);String postdate = request.getParameter(POSTDATE);String tranId = request.getParameter(TRANID);String auth = request.getParameter(AUTH);String ref = request.getParameter(REF);String sessionId = request.getSession().getId();String errorNo = request.getParameter(ERROR);try {paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, status, postdate, null);} catch (PaymentException e1) {log.error("Unable to update payment details in our database." + e1.getError_code() + e1.getMessage());e1.printStackTrace();} catch (TException e1) {log.error("Unable to update payment details in our database. Thrift exception.");e1.printStackTrace();}}public String getRedirectUrl(){return this.redirectUrl;}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;for(Object param: request.getParameterMap().keySet()){System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));}}@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response;}}