Subversion Repositories SmartDukaan

Rev

Rev 719 | Blame | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import in.shop2020.config.ConfigException;
import in.shop2020.model.v1.order.LineItem;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.Transaction;
import in.shop2020.model.v1.order.TransactionStatus;
import in.shop2020.payments.Payment;
import in.shop2020.payments.PaymentException;
import in.shop2020.payments.PaymentStatus;
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 in.shop2020.utils.Logger;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.thrift.TException;

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;

        PaymentServiceClient pclient = null;
        TransactionServiceClient tsc = null;
        UserContextServiceClient usc = null;
        private static Log log = LogFactory.getLog(HdfcPayResponseController.class);

        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";
        String redirectUrl;
        String amount;
        String trackId;
        String result;
        String postdate;
        String auth;
        String ref;
        String tranId;
        String paymentId;
        String sessionId;
        String errorText;
        String errorNo;
        long txnId;
        long merchantPaymentId;
        Transaction transaction = null;
        
        public HdfcPayResponseController() {
                try {
                        pclient = new PaymentServiceClient();
                        tsc = new TransactionServiceClient();
                        usc = new UserContextServiceClient();
                } catch (Exception e) {
                        Logger.log("Could not initialize the paymentservice client", this);
                }
        }
        
        public String create() throws IOException, SecurityException{
                System.out.println("Inside hdfc pay response Create");
                
                result = this.request.getParameter(RESULT);
                postdate = this.request.getParameter(POSTDATE);
                tranId = this.request.getParameter(TRANID);
                auth = this.request.getParameter(AUTH);
                ref = this.request.getParameter(REF);
                amount = this.request.getParameter(AMOUNT);
                paymentId = this.request.getParameter(PAYMENTID);
                trackId = this.request.getParameter(TRACKID);

                merchantPaymentId = Long.parseLong(trackId);
                sessionId = this.request.getSession().getId();
                errorNo = request.getParameter(ERROR);
                errorText = request.getParameter(ERRORTEXT);
                //FIXME dump them somewhere
                /*
                String udf1=request.getParameter("udf1");
                String udf2=request.getParameter("udf2");
                String udf3=request.getParameter("udf3");
                String udf4=request.getParameter("udf4");
                */
                String udf5=request.getParameter("udf5");
                
                
                Payment payment = null;
                try {
                        payment = pclient.getClient().getPayment(merchantPaymentId);
                        if(Long.parseLong(paymentId) != payment.getPaymentId() && udf5.compareToIgnoreCase(payment.getAttributes().get(5).getValue())!=0){
                                this.redirectUrl = ConfigClient.getClient().get("payment_error_url") + "?paymentId="+merchantPaymentId;
                                return "index";
                        }
                } catch (PaymentException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                } catch (TException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                } catch (ConfigException e) {
                        this.redirectUrl = "http://173.230.147.250:8080/pay-error?paymentId="+merchantPaymentId;
                        e.printStackTrace();
                        return "index";
                }
                
                if(result.trim().equals(PaymentReturnStatus.CAPTURED.value())){
                        try {
                                String message = "Payment successful";
                                
                                
                                pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, PaymentStatus.SUCCESS, null);
                                txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
                                
                                tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.IN_PROCESS, "Payment received for the order");
                                
                        transaction = tsc.getClient().getTransaction(txnId);
                        
                        Map<Long,Double> items = new HashMap<Long, Double>();
                        for(Order order: transaction.getOrders()){
                                        for(LineItem lineitem: order.getLineitems()){
                                                items.put(lineitem.getItem_id(),lineitem.getQuantity());
                                        }
                                }
                                //TODO Optimize the function to send less number of data over network
                                usc.getClient().resetCart(transaction.getShoppingCartid(), items);
                                
                                
                                /* 
                                 this.redirectUrl = "http://173.230.147.250:8080/pay-response?paymentId="+paymentId+"&result="+result+"&auth="+auth+
                                "&ref="+ref+"&postdate="+postdate+"&trackid="+trackId+"&tranid="+tranId+"&udf1="+udf1+"&udf2="+udf2+"&udf3="+udf3+"&udf4="+udf4+"&udf5="+udf5+
                                "&amt="+amount;
                                */
                                
                        }catch (Exception e) {
                                log.error("Error while updating information.");
                                //FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
                                
                        }
                        try{
                                this.redirectUrl = ConfigClient.getClient().get("payment_success_url") + "?paymentId="+merchantPaymentId;
                        }catch(Exception ex){
                                this.redirectUrl = "http://173.230.147.250:8080/pay-success?paymentId="+merchantPaymentId;      
                        }
                        
                }else{
                        try{
                                pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, errorText, tranId, auth, ref, errorNo, PaymentStatus.FAILED, null);
                                txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
                                tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
                        }catch(Exception e){
                                log.error("Error while updating information.");
                                //FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
                        }
                        
                        try{
                                this.redirectUrl = ConfigClient.getClient().get("payment_error_url") + "?paymentId="+merchantPaymentId;
                        }catch(Exception ex){
                                this.redirectUrl = "http://173.230.147.250:8080/pay-error?paymentId="+merchantPaymentId;        
                        }
                        
                }
                
                return "index";
        }
        
        
        public String getRedirectUrl(){
                return this.redirectUrl;
        }

        @Override
        public void setServletRequest(HttpServletRequest request) {
                this.request = request;
                for(Object param: request.getParameterMap().keySet()){
                        System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));
                }
                
        }

        @Override
        public void setServletResponse(HttpServletResponse response) {
                this.response = response;
        }
        
}