Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

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

import in.shop2020.config.ConfigException;
import in.shop2020.payments.Attribute;
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.config.ConfigClient;



import com.aciworldwide.commerce.gateway.plugins.NotEnoughDataException;
import com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe;
@Results({
        @Result(name="redirect", type="redirectAction", 
                        params = {"actionName" , "${url}"})
})

public class HdfcPayController extends BaseController {
        
        private static final long serialVersionUID = 1L;
        
        PaymentServiceClient paymentServiceClient = null;
        private String redirectURL;
        private enum ActionType{
                PURCHASE("1"),
                AUTH ("4");
                private String value;
                ActionType(String value) {
                        this.value = value;
                }
                public String value(){
                        return this.value;
                }
        }
        
        public HdfcPayController(){
                try {
                        paymentServiceClient = new PaymentServiceClient();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
        
        public String getRedirectURL(){
                return this.redirectURL;
        }
        
        public String index(){
                String orderDetails = "";
                String email = "";
                String contactNumber = "";
                String billingAddress = "";
                String merchantInfo = "";
                String amount;
                long txnId;
                
                
                System.out.println("amount:" + request.getParameter("amount"));
                System.out.println("amount:" + request.getParameter("txnid"));
                System.out.println("amount:" + request.getParameter("paymentid"));
                
                try{
                        amount = request.getParameter("amount");
                }catch(Exception e){
                        amount = (new Double(Utils.getPaymentAmount(userinfo.getCartId()))).toString();
                }
                txnId = Long.parseLong(request.getParameter("txnid"));
                        
                
                
//          Random rnd = new Random(System.currentTimeMillis());
//              String trackId = String.valueOf(Math.abs(rnd.nextLong()));      //Merchant must generate the Track Id
                
                String merchantPaymentId = request.getParameter("paymentid");   
                
                session.setAttribute("trackId",merchantPaymentId);
//              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(ActionType.PURCHASE.value());                    //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(merchantPaymentId);
                

                orderDetails = Utils.getOrderDetails(txnId);
                email = Utils.getEmailId(userinfo.getUserId());
                contactNumber = Utils.getContactNumber(txnId);
                billingAddress = Utils.getBillingAddress(txnId);
                
                String regex = "[^a-zA-Z0-9\\s\\-\\@\\/\\.]";
                String replacement = " ";
                
                orderDetails = orderDetails.replaceAll(regex, replacement);
                email = email.replaceAll(regex, replacement);
                contactNumber = replacement.replaceAll(regex, replacement);
                billingAddress = billingAddress.replaceAll(regex, replacement);
                if(orderDetails.length() > 250){
                        orderDetails = orderDetails.substring(0, 249);
                }
                if(billingAddress.length() > 250){
                        billingAddress = billingAddress.substring(0, 249);
                }
                Random random = new Random();
                merchantInfo = ""+random.nextLong(); 
                
                
//              UDF 1 - Order details
//              UDF 2 - Email ID
//              UDF 3 - Contact Number. 
//              UDF 4 - Billing Address
//              UDF 5 - Merchant specific
                
                
        System.out.println("udf1:"  + orderDetails);
        System.out.println("udf2:"  + email);
        System.out.println("udf3:"  + contactNumber);
        System.out.println("udf4:"  + billingAddress);
        System.out.println("udf5:"  + merchantInfo);
        
        
                pipe.setUdf1(orderDetails);
                pipe.setUdf2(email);
                pipe.setUdf3(contactNumber);
                pipe.setUdf4(billingAddress);
                pipe.setUdf5(merchantInfo);
                
                List<Attribute> attributes = new ArrayList<Attribute>();
                Attribute attribute1 = new Attribute();
                Attribute attribute2 = new Attribute();
                Attribute attribute3 = new Attribute();
                Attribute attribute4 = new Attribute();
                Attribute attribute5 = new Attribute();
                attribute1.setName("udf1");
                attribute1.setValue(orderDetails);
                attribute2.setName("udf2");
                attribute2.setValue(email);
                attribute3.setName("udf3");
                attribute3.setValue(contactNumber);
                attribute4.setName("udf4");
                attribute4.setValue(billingAddress);
                attribute5.setName("udf5");
                attribute5.setValue(merchantInfo);
                attributes.add(attribute1);
                attributes.add(attribute2);
                attributes.add(attribute3);
                attributes.add(attribute4);
                attributes.add(attribute5);
                
                try {
                        if(pipe.performPaymentInitialization() != e24PaymentPipe.SUCCESS) 
                                {
                                        System.out.println("Error sending Payment Initialization Request: ");
                                        paymentServiceClient.getClient().updatePaymentDetails(Long.parseLong(merchantPaymentId), null, "", "", pipe.getErrorMsg(), "", "", "", "", PaymentStatus.INIT, attributes);
                                        response.sendRedirect(response.encodeRedirectURL( errorURL + "?ErrorText="+pipe.getErrorMsg()+"&paymentId="+merchantPaymentId));
                                        redirectURL = errorURL + "?ErrorText="+pipe.getErrorMsg();
                                        response.setHeader("Location", redirectURL);
                                        response.setStatus(HttpServletResponse.SC_FOUND);
                                }
                        else
                                {
                                        String paymentID = pipe.getPaymentId();
                                        // Update of payment information
                                        paymentServiceClient.getClient().updatePaymentDetails(Long.parseLong(merchantPaymentId), paymentID, "", "", "", "", "", "", "", PaymentStatus.INIT, attributes);
                                        
                                        String payURL = pipe.getPaymentPage();
                                        redirectURL = payURL + "?PaymentID=" + paymentID;
                                        response.sendRedirect(response.encodeRedirectURL( redirectURL ));
                                        
                                        response.setHeader("Location", redirectURL);
                                        response.setStatus(HttpServletResponse.SC_FOUND);
                                }
                        return "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" ));
                redirectURL = errorURL + "?ErrorText=Error while initializing payment.";
                return "success";
        }
        
        String getUrl(){
                return this.redirectURL;
        }

}