Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

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

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.thrift.TException;

import in.shop2020.model.v1.order.LineItem;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.TransactionServiceException;
import in.shop2020.model.v1.order.TransactionStatus;
import in.shop2020.model.v1.user.ShoppingCartException;
import in.shop2020.payments.PaymentException;
import in.shop2020.payments.PaymentStatus;
import in.shop2020.serving.utils.Utils;
import in.shop2020.thrift.clients.PaymentServiceClient;
import in.shop2020.thrift.clients.TransactionServiceClient;
import in.shop2020.thrift.clients.UserContextServiceClient;
import in.shop2020.utils.Logger;

@Results({
    @Result(name="redirect", type="redirectAction", 
                params = {"actionName" , "login"}),
    @Result(name="shipping-redirect", type="redirectAction", 
                params = {"actionName" , "shipping"})
                
})

public class PayResponseController extends BaseController{
        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;
                }
        }

        public static String AMOUNT = "amt";
        public static String TRACKID = "trackid";
        public static String TRACKID_CANCELLED = "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";
        
        PaymentServiceClient pclient = null;
        TransactionServiceClient tsc = null;
        UserContextServiceClient usc = null;
        
        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;
        List<Order> orders = null;

        /*
         *      pw.println("RESULT="+result);
                        pw.println("PDATE="+postdate);
                        pw.println("TXNID="+tranid);
                        pw.println("AUTH="+auth);
                        pw.println("TRACKID="+trackid);
                        pw.println("REF="+ref);
                        pw.println("AMT="+amount);
                        
                        pw.flush();
                */      
                        
        String message = "Unable to process the payment. PLease try Again.";
        
        public PayResponseController(){
                super();
                try {
                        pclient = new PaymentServiceClient();
                        tsc = new TransactionServiceClient();
                        usc = new UserContextServiceClient();
                } catch (Exception e) {
                        Logger.log("Could not initialize the paymentservice client", this);
                }
        }
        
        public String index() throws IOException, SecurityException{
        if(!userinfo.isLoggedIn()){
                setRedirectUrl();
                return "redirect";
        }

        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);
                if (result.equalsIgnoreCase(PaymentReturnStatus.CANCELLED.value())){
                        trackId = this.request.getParameter(TRACKID_CANCELLED);
                }else{
                        trackId = this.request.getParameter(TRACKID);
                }
                merchantPaymentId = Long.parseLong(trackId);
                sessionId = this.request.getSession().getId();
                errorNo = request.getParameter(ERROR);
                errorText = request.getParameter(ERRORTEXT);
                /*
                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");
                */
                //update the payment info object
                try {
                        if(result.trim().equals(PaymentReturnStatus.CAPTURED.value())){
                                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");
                                
                        orders = tsc.getClient().getOrdersForTransaction(txnId);
                        Map<Long,Double> items = new HashMap<Long, Double>();
                        
                                for(Order order: orders){
                                        for(LineItem lineitem: order.getLineitems()){
                                                items.put(lineitem.getItem_id(),lineitem.getQuantity());
                                        }
                                }
                                usc.getClient().resetCart(userinfo.getCartId(), items);
                                //TODO Optimize the function to send less number of data over network
                                userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));

                                return "index";
                        }else{
                                String message = "Unable to process the payment.";
                                addActionError("Previous payment failed. Try again.");
                                
                                txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
                                tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
                                
                                pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, errorText, tranId, auth, ref, errorNo, PaymentStatus.FAILED, null);
                                return "shipping-redirect";
                        }
                        
                }
                catch (NumberFormatException e) {
                        Logger.log(e.toString(), this);
                } catch (PaymentException e) {
                        Logger.log(e.toString(), this);
                } catch (TException e) {
                        Logger.log(e.toString(), this);
                } catch (TransactionServiceException e) {
                        Logger.log(e.toString(), this);
                } catch (ShoppingCartException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return "index";
        }
        
        public List<Order> getOrders(){
                return this.orders;
        }
        
        public String getMessage(){
                return this.message;
        }
        
}