Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

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

import org.apache.thrift.TException;

import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.TransactionServiceException;
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.utils.Logger;


public class PayResponseController extends BaseController{
        private static final long serialVersionUID = 1L;

        public static String AMOUNT = "amount";
        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 CANCELED = "CANCELED";
        public static String APPROVED = "APPROVED";
        public static String CAPTURED = "CAPTURED";

        PaymentServiceClient pclient = null;
        TransactionServiceClient tsc = null;
        
        String amount;
        String trackId;
        String result;
        String postdate;
        String auth;
        String ref;
        String tranId;
        String paymentId;
        String sessionId;

        String message = "Unable to process the payment. PLease try Again.";
        
        public PayResponseController(){
                super();
                try {
                        pclient = new PaymentServiceClient();
                        tsc = new TransactionServiceClient();
                } catch (Exception e) {
                        Logger.log("Could not initialize the paymentservice client", this);
                }
        }
        
        public String index() throws IOException, SecurityException{
                amount = this.request.getParameter(AMOUNT);
                paymentId = this.request.getParameter(PAYMENTID);
                tranId = this.request.getParameter(TRANID);
                result = this.request.getParameter(RESULT);
                if (result.equalsIgnoreCase(CANCELED)){
                        trackId = this.request.getParameter(TRACKID_CANCELLED);
                }else{
                        trackId = this.request.getParameter(TRACKID);
                }
                auth = this.request.getParameter(AUTH);
                ref = this.request.getParameter(REF);
                postdate = this.request.getParameter(POSTDATE);
                sessionId = this.request.getSession().getId();

                //update the payment info object
                try {
                        if(result.trim().equals(CAPTURED)){
                                String message = "Payment successful";
                                pclient.getClient().updatePaymentDetails(Long.parseLong(trackId), paymentId, sessionId, result, message, tranId, auth, ref, null, PaymentStatus.SUCCESS, null);
                                return "index";
                        }else{
                                String message = "Unable to process the payment.";
                                addActionError("Previous payment failed. Try again.");
                                pclient.getClient().updatePaymentDetails(Long.parseLong(trackId), paymentId, sessionId, result, message, tranId, auth, ref, null, PaymentStatus.FAILED, null);
                                return "failure";
                        }
                        
                } catch (NumberFormatException e) {
                        Logger.log(e.toString(), this);
                } catch (PaymentException e) {
                        
                        Logger.log(e.toString(), this);
                } catch (TException e) {
                        
                        Logger.log(e.toString(), this);
                }
                return "index";
        }
        
        public List<Order> getOrders() throws TransactionServiceException, TException{
                //FIXME
                return tsc.getClient().getOrdersForTransaction(1);
                
                
        }
        
        public String getMessage(){
                return this.message;
        }
        
}