Subversion Repositories SmartDukaan

Rev

Rev 2958 | Rev 3101 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2958 Rev 3063
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import in.shop2020.datalogger.EventType;
3
import in.shop2020.datalogger.EventType;
4
import in.shop2020.model.v1.order.Order;
4
import in.shop2020.model.v1.order.Order;
-
 
5
import in.shop2020.model.v1.user.Address;
5
import in.shop2020.model.v1.user.ShoppingCartException;
6
import in.shop2020.model.v1.user.ShoppingCartException;
6
import in.shop2020.serving.interceptors.TrackingInterceptor;
7
import in.shop2020.serving.interceptors.TrackingInterceptor;
-
 
8
import in.shop2020.serving.services.CodPaymentService;
-
 
9
import in.shop2020.serving.services.CommonPaymentService;
7
import in.shop2020.serving.services.EbsPaymentService;
10
import in.shop2020.serving.services.EbsPaymentService;
8
import in.shop2020.serving.services.HdfcPaymentService;
11
import in.shop2020.serving.services.HdfcPaymentService;
9
import in.shop2020.serving.services.IPaymentService;
12
import in.shop2020.serving.services.IPaymentService;
10
import in.shop2020.serving.utils.DesEncrypter;
13
import in.shop2020.serving.utils.DesEncrypter;
-
 
14
import in.shop2020.thrift.clients.LogisticsServiceClient;
11
import in.shop2020.thrift.clients.TransactionServiceClient;
15
import in.shop2020.thrift.clients.TransactionServiceClient;
12
import in.shop2020.thrift.clients.UserContextServiceClient;
16
import in.shop2020.thrift.clients.UserContextServiceClient;
13
import in.shop2020.utils.DataLogger;
17
import in.shop2020.utils.DataLogger;
14
 
18
 
15
import java.util.List;
19
import java.util.List;
-
 
20
import java.util.ResourceBundle;
16
 
21
 
17
import javax.servlet.http.Cookie;
22
import javax.servlet.http.Cookie;
18
 
23
 
-
 
24
import net.tanesha.recaptcha.ReCaptchaImpl;
-
 
25
import net.tanesha.recaptcha.ReCaptchaResponse;
-
 
26
 
19
import org.apache.log4j.Logger;
27
import org.apache.log4j.Logger;
20
import org.apache.struts2.convention.annotation.InterceptorRef;
28
import org.apache.struts2.convention.annotation.InterceptorRef;
21
import org.apache.struts2.convention.annotation.InterceptorRefs;
29
import org.apache.struts2.convention.annotation.InterceptorRefs;
22
import org.apache.struts2.convention.annotation.Result;
30
import org.apache.struts2.convention.annotation.Result;
23
import org.apache.struts2.convention.annotation.Results;
31
import org.apache.struts2.convention.annotation.Results;
Line 30... Line 38...
30
})
38
})
31
 
39
 
32
@Results({
40
@Results({
33
    @Result(name="shipping-redirect", type="redirectAction", 
41
    @Result(name="shipping-redirect", type="redirectAction", 
34
    		params = {"actionName" , "shipping"}),
42
    		params = {"actionName" , "shipping"}),
35
	@Result(name="ebs-pay-redirect", type="redirect", location="/ebs-pay/${paymentId}")
43
	@Result(name="ebs-pay-redirect", type="redirect", location="/ebs-pay/${paymentId}"),
-
 
44
	@Result(name="cod-redirect", type="redirect", location="/pay-success?paymentId=${paymentId}")
36
})
45
})
37
public class OrderController extends BaseController {
46
public class OrderController extends BaseController {
38
	
47
	
39
	public long getPaymentId() {
48
	public long getPaymentId() {
40
		return paymentId;
49
		return paymentId;
41
	}
50
	}
42
	
51
	
43
	private static Logger log = Logger.getLogger(Class.class);
52
	private static Logger log = Logger.getLogger(Class.class);
-
 
53
	private static final ResourceBundle resource = ResourceBundle.getBundle(OrderController.class.getName());
-
 
54
	private static final String RECAPTCHA_PRIVATE_KEY = resource.getString("recaptcha_private_key");
-
 
55
	
44
	private String id;
56
	private String id;
45
	private long txnId = 0;
57
	private long txnId = 0;
46
	
58
	
47
	private long paymentId;
59
	private long paymentId;
48
	
60
	
Line 76... Line 88...
76
    	}
88
    	}
77
    	
89
    	
78
    	
90
    	
79
    	long addressId = Long.parseLong(addressIdString);
91
    	long addressId = Long.parseLong(addressIdString);
80
    	long currentCartId = userinfo.getCartId();
92
    	long currentCartId = userinfo.getCartId();
-
 
93
    	
-
 
94
    	if(paymentOption.equals(IPaymentService.COD)){
-
 
95
    	    if(!verifyCaptcha()){
-
 
96
    	        addActionError("Invalid captcha");
-
 
97
    	        return "shipping-redirect";    
-
 
98
    	    }
-
 
99
    	    
-
 
100
    	    //Check that this address is eligible for COD shipping.
-
 
101
    	    UserContextServiceClient userServiceClient = null;
-
 
102
            try {
-
 
103
                userServiceClient = new UserContextServiceClient();
-
 
104
                in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
-
 
105
                Address address = userClient.getAddressById(addressId);
-
 
106
                
-
 
107
                LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
-
 
108
                if(!logisticsServiceClient.getClient().isCodAllowed(address.getPin())){
-
 
109
                    addActionError("Cash on Delivery is currently not available for this pincode. Please choose a different payment option.");
-
 
110
                    return "shipping-redirect";    
-
 
111
                }
-
 
112
            } catch (Exception e) {
-
 
113
                log.error("Unable to talk to the user context service.", e);
-
 
114
                addActionError("We are experiencing some problems. Please try later.");
-
 
115
                return "shipping-redirect";
-
 
116
            }
-
 
117
    	}
-
 
118
    	
81
    	try{
119
    	try {
82
    		if(!createOrders(addressId, currentCartId)){
120
    		if(!createOrders(addressId, currentCartId)){
83
    			addActionError("We are experiencing some problems. Please try later.");
121
    			addActionError("We are experiencing some problems. Please try later.");
84
    			return "shipping-redirect";
122
    			return "shipping-redirect";
85
    		}
123
    		}
86
    	}catch (Exception e) {
124
    	} catch (Exception e) {
87
    		addActionError("We are experiencing some problems. Please try later.");
125
    		addActionError("We are experiencing some problems. Please try later.");
88
    		log.error("Exception in createOrders function. Something went wrong.", e);
126
    		log.error("Exception in createOrders function. Something went wrong.", e);
89
    		return "shipping-redirect";
127
    		return "shipping-redirect";
90
		}
128
		}
91
    	
129
    	
-
 
130
    	if(paymentOption.equals(IPaymentService.COD)){
-
 
131
    	    IPaymentService codPaymentService = new CodPaymentService();
-
 
132
    	    paymentId = codPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption);
-
 
133
            if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
-
 
134
                //Very unlikely. The only possible reason can be that the payment service is down.
-
 
135
                log.error("Unable to process the COD payment.");
-
 
136
                addActionError("We are experiencing some problems. Please try later.");
-
 
137
                return "shipping-redirect";
-
 
138
            } else {
-
 
139
                CommonPaymentService.processCodTxn(txnId);
-
 
140
                return "cod-redirect";
-
 
141
            }
-
 
142
    	} else {
92
		if (paymentOption.equals(IPaymentService.HDFC_MASTERCARD) || paymentOption.equals(IPaymentService.HDFC_VISA) || paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON)) {
143
            if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD) || paymentOption.equals(IPaymentService.HDFC_VISA) || paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON)) {
93
			// User has selected Visa or MasterCard CC
144
                // User has selected Visa or MasterCard CC
94
			IPaymentService hdfcPaymentService = new HdfcPaymentService();
145
                IPaymentService hdfcPaymentService = new HdfcPaymentService();
95
			paymentId = hdfcPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption);
146
                paymentId = hdfcPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption);
96
			if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
147
                if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
97
				log.error("Unable to process payment through HDFC. Falling through to EBS.");
148
                    log.error("Unable to process payment through HDFC. Falling through to EBS.");
98
			} else {
149
                } else {
99
				this.redirectURL = ((HdfcPaymentService)hdfcPaymentService).getRedirectUrl();
150
                    this.redirectURL = ((HdfcPaymentService)hdfcPaymentService).getRedirectUrl();
100
				log.info(this.redirectURL);
151
                    log.info(this.redirectURL);
101
				return "success";
152
                    return "success";
-
 
153
                }
-
 
154
            }
-
 
155
 
-
 
156
            if(paymentOption.equals(IPaymentService.HDFC_VISA))
-
 
157
                paymentOption = IPaymentService.EBS_VISA;
-
 
158
            else if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD))
-
 
159
                paymentOption = IPaymentService.EBS_MASTERCARD;
-
 
160
            else if(paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON))
-
 
161
                paymentOption = null;           //Since we don't know the bank's name in this case, we'll let the user select the bank on the EBS page.
-
 
162
            
-
 
163
            IPaymentService ebsPaymentService = new EbsPaymentService();
-
 
164
            paymentId = ebsPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption);
-
 
165
            if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
-
 
166
                addActionError("We are experiencing some problems. Please try later.");
-
 
167
                return "shipping-redirect";
-
 
168
            } else {
-
 
169
                log.info("Successfully created payment for EBS to process. Redirecting to /ebs-pay/" + paymentId);
-
 
170
                return "ebs-pay-redirect";
-
 
171
            }    	    
102
			}
172
    	}
-
 
173
    	    
103
		}
174
    }
104
 
175
 
-
 
176
    /**
105
		if(paymentOption.equals(IPaymentService.HDFC_VISA))
177
     * Verifies if the recaptcha response matches the recaptcha challenge.
-
 
178
     * 
106
			paymentOption = IPaymentService.EBS_VISA;
179
     * @return True if the captcha was valid, false otherwise.
-
 
180
     */
107
		else if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD))
181
    private boolean verifyCaptcha() {
108
			paymentOption = IPaymentService.EBS_MASTERCARD;
182
        String remoteAddr = request.getRemoteAddr();
109
		else if(paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON))
183
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
110
			paymentOption = null;			//Since we don't know the bank's name in this case, we'll let the user select the bank on the EBS page.
184
        reCaptcha.setPrivateKey(RECAPTCHA_PRIVATE_KEY);
111
		
185
 
112
		IPaymentService ebsPaymentService = new EbsPaymentService();
186
        String challenge = request.getParameter("recaptcha_challenge_field");
113
		paymentId = ebsPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption);
187
        String uresponse = request.getParameter("recaptcha_response_field");
114
		if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
188
        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
-
 
189
 
115
			addActionError("We are experiencing some problems. Please try later.");
190
        if(reCaptchaResponse.isValid())
116
			return "shipping-redirect";
191
          return true;
117
		}else{
192
        else
118
			log.info("Successfully created payment for EBS to process. Redirecting to /ebs-pay/" + paymentId);
-
 
119
			return "ebs-pay-redirect";
193
          return false;
120
		}
-
 
121
    }
194
    }
122
    
195
    
123
	public String getId(){
196
	public String getId(){
124
		return id;
197
		return id;
125
	}
198
	}