Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.apache.struts2.util.ServletContextAware;
import org.apache.thrift.TException;

import in.shop2020.config.ConfigException;
import in.shop2020.model.v1.user.AuthenticationException;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.model.v1.user.UserContextService.Client;
import in.shop2020.payments.PaymentException;
import in.shop2020.payments.PaymentService;
import in.shop2020.serving.services.UserSessionInfo;
import in.shop2020.serving.utils.Utils;
import in.shop2020.thrift.clients.PaymentServiceClient;
import in.shop2020.thrift.clients.UserContextServiceClient;
import in.shop2020.thrift.clients.UserProfileClient;
import in.shop2020.thrift.clients.config.ConfigClient;



import com.aciworldwide.commerce.gateway.plugins.NotEnoughDataException;
import com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe;
import com.opensymphony.xwork2.ModelDriven;
/*
@Results({
    @Result(name="success", type="redirectAction", params = {"actionName" , 
                "pay"})
})
*/
public class PayController extends BaseController implements ParameterAware, ServletContextAware {

        Map<String, String[]> reqparams;
        
        private String id;
        PaymentServiceClient paymentServiceClient = null;
        private String redirectURL;
        
        private ServletContext servletContext;
        
        public PayController(){
                
                try {
                        paymentServiceClient = new PaymentServiceClient();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
        
        public String getRedirectURL(){
                return this.redirectURL;
        }
        
        public HttpHeaders show(){
                return new DefaultHttpHeaders("lfail");

        }
        
        public HttpHeaders create(){
                String orderDetails = "";
                String email = "";
                String contactNumber = "";
                String billingAddress = "";
                String merchantInfo = "";
                String amount;
                try{
                        amount = this.reqparams.get("amount")[0];
                }catch(Exception e){
                        amount = (new Double(Utils.getPaymentAmount(userinfo.getCartId()))).toString();
                }
                //String amount = this.request.getParameter("amount");
                session.setAttribute("amount", amount);
                // May be we can use some utils function to get the prices in cart but it is better to get price from post form
                
                long paymentId = 0;
                in.shop2020.payments.PaymentService.Client client = paymentServiceClient.getClient();
        
                
                try {
                        paymentId = client.createPayment(userinfo.getUserId(), userinfo.getCartId(), Double.parseDouble(amount), 1);
                } catch (NumberFormatException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                } catch (PaymentException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                } catch (TException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
                
                
                
                
//          Random rnd = new Random(System.currentTimeMillis());
//              String trackId = String.valueOf(Math.abs(rnd.nextLong()));      //Merchant must generate the Track Id
                
                String trackId = (new Long(paymentId)).toString();
                        
                
                session.setAttribute("trackId",trackId);
//              String InstituteID=request.getParameter("InstituteID"); //Must be 1 for the merchant having only one bank(mandatory must be initilized)
        
                
                //Following is the code which initilize e24PaymentPipe with proper value
                e24PaymentPipe pipe=new e24PaymentPipe();
                String resourceFilePath = "";
                String aliasName = "";
                String responseURL = "";
                String errorURL = "";
                try {
                        resourceFilePath = ConfigClient.getClient().get("payment_resource_file_path");
                        aliasName  = ConfigClient.getClient().get("payment_alias_name");
                        responseURL = ConfigClient.getClient().get("payment_response_url");
                        errorURL = ConfigClient.getClient().get("payment_error_url");
                } catch (ConfigException e1) {
                        // TODO Auto-generated catch block
                        resourceFilePath = "/home/rajveer/shop2020/payments/resource/";
                        aliasName = "90001194";
                        
                        responseURL = "http://74.207.248.175:8080/JSP-Pages/HRedirect.jsp";
                        errorURL = "http://74.207.248.175:8080/JSP-Pages/HError.jsp";
//                      responseURL = "http://securemtp.fssnet.co.in/MerchantDemo/viren/TESTTEST/HRedirect.jsp";
//                      errorURL = "http://securemtp.fssnet.co.in/MerchantDemo/viren/TESTTEST/HError.jsp";
                        e1.printStackTrace();
                }
                
                pipe.setResourcePath(resourceFilePath); //mandatory 
                String as = pipe.getResourcePath();
                System.out.println("Resource="+as+ "<br>");
                
                pipe.setAlias(aliasName);                       //mandatory 
                String ab=pipe.getAlias();
                System.out.println("Alias="+ab+ "<br>");

                pipe.setAction("1");                    //mandatory 
                String ac=pipe.getAction();
                System.out.println("Action="+ac+ "<br>");

                pipe.setResponseURL( responseURL );     //mandatory
                String at=pipe.getResponseURL();
                System.out.println("ResponseURL="+at+ "<br>");
        
                pipe.setErrorURL( errorURL );           //mandatory 
        String ak=pipe.getErrorURL();
        System.out.println("ErrorURL="+ak+ "<br>");
        

                pipe.setAmt(amount);            
                String ap=pipe.getAmt();
                System.out.println("Amt="+ap);

                pipe.setCurrency("356");
                String a=pipe.getCurrency();
                System.out.println("Currency="+a+ "<br>");

                pipe.setLanguage("USA");
                String p=pipe.getLanguage();
                System.out.println("Language="+p+ "<br>");
        
                pipe.setTrackId(trackId);
                

                orderDetails = Utils.getOrderDetails(userinfo.getCartId());
                email = Utils.getEmailId(userinfo.getUserId());
                contactNumber = Utils.getContactNumber(userinfo.getCartId());
                billingAddress = Utils.getBillingAddress(userinfo.getCartId());
                merchantInfo = "Shop2020";
                
                
//              UDF 1 - Order details
//              UDF 2 - Email ID
//              UDF 3 - Contact Number. 
//              UDF 4 - Billing Address
//              UDF 5 - Merchant specific
                /*
                pipe.setUdf1(orderDetails);
                pipe.setUdf2(email);
                pipe.setUdf3(contactNumber);
                pipe.setUdf4(billingAddress);
        pipe.setUdf5(merchantInfo);
                 */
                
                try {
                        if(pipe.performPaymentInitialization() != e24PaymentPipe.SUCCESS) 
                                {
                                        System.out.println("Error sending Payment Initialization Request: ");
                                        response.sendRedirect(response.encodeRedirectURL( errorURL + "?ErrorText="+pipe.getErrorMsg()));
                                        redirectURL = errorURL + "?ErrorText="+pipe.getErrorMsg();
                                        response.setHeader("Location", redirectURL);
                                        response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
                                }
                        else
                                {
                                        String PaymentID = pipe.getPaymentId();
                                        String payURL = pipe.getPaymentPage();
                                        response.sendRedirect(response.encodeRedirectURL( payURL + "?PaymentID=" + PaymentID ));
                                        
                                        redirectURL = payURL + "?PaymentID=" + PaymentID;
                                        response.setHeader("Location", redirectURL);
                                        response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
                                }
                        return new DefaultHttpHeaders("success");
                } catch (NotEnoughDataException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                //response.sendRedirect(response.encodeRedirectURL( "www.yahoo.com" ));
                
                return new DefaultHttpHeaders("fail");
        }
        
        public String getId(){
                return id;
        }
        
        public void setId(String id){
                this.id = id;
        }

        @Override
        public void setParameters(Map<String, String[]> reqmap) {
                //log.info("setParameters:" + reqmap);
                
                this.reqparams = reqmap;
        }
        
        @Override
        public void setServletContext(ServletContext servletContext) {
                this.servletContext = servletContext;
        }
        
}