Subversion Repositories SmartDukaan

Rev

Rev 6060 | Rev 6353 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
419 rajveer 1
package in.shop2020.serving.controllers;
2
 
2263 vikas 3
import in.shop2020.datalogger.EventType;
2183 vikas 4
import in.shop2020.model.v1.order.Order;
4325 mandeep.dh 5
import in.shop2020.model.v1.order.TransactionServiceException;
3063 chandransh 6
import in.shop2020.model.v1.user.Address;
894 rajveer 7
import in.shop2020.model.v1.user.ShoppingCartException;
2817 vikas 8
import in.shop2020.serving.interceptors.TrackingInterceptor;
3063 chandransh 9
import in.shop2020.serving.services.CodPaymentService;
10
import in.shop2020.serving.services.CommonPaymentService;
1905 chandransh 11
import in.shop2020.serving.services.EbsPaymentService;
3616 chandransh 12
import in.shop2020.serving.services.HdfcEmiPaymentService;
1318 rajveer 13
import in.shop2020.serving.services.HdfcPaymentService;
1905 chandransh 14
import in.shop2020.serving.services.IPaymentService;
6060 rajveer 15
import in.shop2020.serving.services.PayuPaymentService;
6050 anupam.sin 16
import in.shop2020.serving.utils.DesEncrypter;
3212 vikas 17
import in.shop2020.serving.utils.Utils;
3126 rajveer 18
import in.shop2020.thrift.clients.TransactionClient;
19
import in.shop2020.thrift.clients.UserClient;
2511 vikas 20
import in.shop2020.utils.DataLogger;
517 rajveer 21
 
3394 vikas 22
import java.util.ArrayList;
2419 vikas 23
import java.util.List;
24
 
832 rajveer 25
import org.apache.log4j.Logger;
822 vikas 26
import org.apache.struts2.convention.annotation.InterceptorRef;
27
import org.apache.struts2.convention.annotation.InterceptorRefs;
419 rajveer 28
import org.apache.struts2.convention.annotation.Result;
29
import org.apache.struts2.convention.annotation.Results;
517 rajveer 30
import org.apache.thrift.TException;
4325 mandeep.dh 31
import org.apache.thrift.transport.TTransportException;
4609 phani.kuma 32
import nl.captcha.Captcha;
419 rajveer 33
 
2159 chandransh 34
@SuppressWarnings("serial")
822 vikas 35
@InterceptorRefs({
36
    @InterceptorRef("myDefault"),
37
    @InterceptorRef("login")
38
})
39
 
419 rajveer 40
@Results({
3101 chandransh 41
    @Result(name="shipping-redirect", type="redirectAction", params = {"actionName" , "shipping"}),
5716 anupam.sin 42
    @Result(name="proceed-to-pay-redirect", type="redirectAction", params = {"actionName" , "proceed-to-pay", "deliveryLocation", "${deliveryLocation}"}),
3063 chandransh 43
	@Result(name="ebs-pay-redirect", type="redirect", location="/ebs-pay/${paymentId}"),
6060 rajveer 44
	@Result(name="payu-pay-redirect", type="redirect", location="/payu-pay/${paymentId}"),
3063 chandransh 45
	@Result(name="cod-redirect", type="redirect", location="/pay-success?paymentId=${paymentId}")
419 rajveer 46
})
650 rajveer 47
public class OrderController extends BaseController {
419 rajveer 48
 
1905 chandransh 49
	public long getPaymentId() {
50
		return paymentId;
51
	}
419 rajveer 52
 
832 rajveer 53
	private static Logger log = Logger.getLogger(Class.class);
3063 chandransh 54
 
507 rajveer 55
	private String id;
712 rajveer 56
	private long txnId = 0;
650 rajveer 57
 
1905 chandransh 58
	private long paymentId;
59
 
1318 rajveer 60
	private String redirectURL;
5716 anupam.sin 61
	private String deliveryLocation = "";
681 rajveer 62
 
419 rajveer 63
	public OrderController(){
507 rajveer 64
		super();
419 rajveer 65
	}
66
 
507 rajveer 67
    // GET /order/ orderid
68
    public String show() {
69
    	log.info("id=" + id);
822 vikas 70
    	htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
4815 phani.kuma 71
    	htmlSnippets.put("ORDER_DETAILS", pageLoader.getOrderDetailsHtml(Long.parseLong(id), userinfo));
507 rajveer 72
    	return "show";
419 rajveer 73
    }
517 rajveer 74
 
572 chandransh 75
    // POST /order/
76
    public String create(){
786 rajveer 77
    	String addressIdString = this.request.getParameter("addressid");
78
    	if(addressIdString == null){
894 rajveer 79
    		addActionError("Please specify shipping address to continue.");
3101 chandransh 80
    		return "proceed-to-pay-redirect";
786 rajveer 81
    	}
2159 chandransh 82
 
83
    	String paymentOption = request.getParameter("payment_option");
84
    	log.info("Payment Option Selected: " + paymentOption);
85
    	if(paymentOption == null || paymentOption.equals("")){
86
    		addActionError("Please select a payment method to continue.");
3101 chandransh 87
    		return "proceed-to-pay-redirect";
2159 chandransh 88
    	}
89
 
90
 
3101 chandransh 91
    	long addressId;
92
    	try {
93
    	    addressId = Long.parseLong(addressIdString);   
94
    	} catch(NumberFormatException nfe){
95
    	    log.error("Unable to get address id", nfe);
96
    	    addActionError("Invalid address. Please add an address to the cart.");
97
    	    return "shipping-redirect";
98
    	}
99
 
572 chandransh 100
    	long currentCartId = userinfo.getCartId();
5326 rajveer 101
    	long currentUserId = userinfo.getUserId();
3063 chandransh 102
 
103
    	if(paymentOption.equals(IPaymentService.COD)){
104
    	    if(!verifyCaptcha()){
105
    	        addActionError("Invalid captcha");
4815 phani.kuma 106
    	        log.info("Invalid captcha error msg has been added");
3101 chandransh 107
    	        return "proceed-to-pay-redirect";    
3063 chandransh 108
    	    }
109
 
6335 rajveer 110
    	    if(deliveryLocation.equals("myLocation")){
111
	    	    //Check that this address is eligible for COD shipping.
112
	    	    UserClient userServiceClient = null;
113
	            try {
114
	                userServiceClient = new UserClient();
115
	                in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
116
	                Address address = userClient.getAddressById(addressId);
117
 
118
	                if(!userClient.showCODOption(currentCartId, sourceId, address.getPin())){
119
	                    addActionError("Cash on Delivery is currently not available for this pincode. Please choose a different payment option.");
120
	                    return "proceed-to-pay-redirect";    
121
	                }
122
	            } catch (Exception e) {
123
	                log.error("Unable to talk to the user context service.", e);
124
	                addActionError("We are experiencing some problems. Please try later.");
125
	                return "proceed-to-pay-redirect";
126
	            }
127
    	    }
3063 chandransh 128
    	}
129
 
130
    	try {
5326 rajveer 131
    		if(!createOrders(addressId, currentCartId, currentUserId)) {
1128 rajveer 132
    			addActionError("We are experiencing some problems. Please try later.");
3101 chandransh 133
    			return "proceed-to-pay-redirect";
894 rajveer 134
    		}
3063 chandransh 135
    	} catch (Exception e) {
1128 rajveer 136
    		addActionError("We are experiencing some problems. Please try later.");
2296 chandransh 137
    		log.error("Exception in createOrders function. Something went wrong.", e);
3101 chandransh 138
    		return "proceed-to-pay-redirect";
693 rajveer 139
		}
2159 chandransh 140
 
4325 mandeep.dh 141
    	if(paymentOption.equals(IPaymentService.COD)) {
3063 chandransh 142
    	    IPaymentService codPaymentService = new CodPaymentService();
3561 rajveer 143
    	    paymentId = codPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption, sourceId);
3063 chandransh 144
            if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
145
                //Very unlikely. The only possible reason can be that the payment service is down.
146
                log.error("Unable to process the COD payment.");
147
                addActionError("We are experiencing some problems. Please try later.");
3101 chandransh 148
                return "proceed-to-pay-redirect";
3063 chandransh 149
            } else {
150
                CommonPaymentService.processCodTxn(txnId);
151
                return "cod-redirect";
152
            }
3616 chandransh 153
    	} else if(paymentOption.equals(IPaymentService.HDFC_EMI)){
154
    	    IPaymentService hdfcEmiPaymentService = new HdfcEmiPaymentService();
155
            paymentId = hdfcEmiPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption, sourceId);
156
            if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
157
                addActionError("We are experiencing some problems. Please try later.");
158
                log.error("Unable to process payment through HDFC EMI.");
159
                return "proceed-to-pay-redirect";
160
            } else {
3617 chandransh 161
                this.redirectURL = ((HdfcEmiPaymentService)hdfcEmiPaymentService).getRedirectUrl();
3616 chandransh 162
                log.info(this.redirectURL);
163
                return "success";
164
            }
3063 chandransh 165
    	} else {
5538 anupam.sin 166
    		if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || 
167
    		        paymentOption.equals(IPaymentService.HDFC_VISA) || paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON)) {
168
                // User has selected Visa or MasterCard
3063 chandransh 169
                IPaymentService hdfcPaymentService = new HdfcPaymentService();
3561 rajveer 170
                paymentId = hdfcPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption, sourceId);
3063 chandransh 171
                if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
172
                    log.error("Unable to process payment through HDFC. Falling through to EBS.");
173
                } else {
174
                    this.redirectURL = ((HdfcPaymentService)hdfcPaymentService).getRedirectUrl();
175
                    log.info(this.redirectURL);
176
                    return "success";
177
                }
178
            }
2199 chandransh 179
 
3063 chandransh 180
            if(paymentOption.equals(IPaymentService.HDFC_VISA))
181
                paymentOption = IPaymentService.EBS_VISA;
5538 anupam.sin 182
            else if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT))
3063 chandransh 183
                paymentOption = IPaymentService.EBS_MASTERCARD;
184
            else if(paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON))
185
                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.
186
 
6060 rajveer 187
            if(paymentOption.equals(IPaymentService.PAYU_CC)){
188
            	paymentOption = IPaymentService.PAYU_CC;
189
            	IPaymentService payuPaymentService = new PayuPaymentService();
190
            	paymentId = payuPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption, sourceId);
191
            	return "payu-pay-redirect";
192
            }
193
 
3063 chandransh 194
            IPaymentService ebsPaymentService = new EbsPaymentService();
3561 rajveer 195
            paymentId = ebsPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption, sourceId);
3063 chandransh 196
            if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
197
                addActionError("We are experiencing some problems. Please try later.");
3616 chandransh 198
                log.error("Unable to process payment through EBS.");
3101 chandransh 199
                return "proceed-to-pay-redirect";
3063 chandransh 200
            } else {
201
                log.info("Successfully created payment for EBS to process. Redirecting to /ebs-pay/" + paymentId);
202
                return "ebs-pay-redirect";
203
            }    	    
204
    	}
205
 
572 chandransh 206
    }
3063 chandransh 207
 
208
    /**
209
     * Verifies if the recaptcha response matches the recaptcha challenge.
210
     * 
211
     * @return True if the captcha was valid, false otherwise.
212
     */
213
    private boolean verifyCaptcha() {
4609 phani.kuma 214
        String cookieCaptchaAnswer = getCookie(Captcha.NAME, true, "saholic");
215
    	String captchaReceived = (String) request.getParameter("captcha_response_field");
216
 
217
    	if (captchaReceived.equalsIgnoreCase(cookieCaptchaAnswer)) {
218
			try {
219
				DataLogger.logData(EventType.CAPTCHA_SUCCESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail());
220
			} catch (Exception e1) {
221
	        	log.warn("Unable to log orders through the datalogger", e1);
222
	        }
223
			return true;
224
		} 
225
		else {
226
			try {
227
				DataLogger.logData(EventType.CAPTCHA_FAILED, getSessionId(), userinfo.getUserId(), userinfo.getEmail());
228
			} catch (Exception e1) {
229
	        	log.warn("Unable to log orders through the datalogger", e1);
230
	        }
231
			return false;
232
		}
3063 chandransh 233
    }
572 chandransh 234
 
507 rajveer 235
	public String getId(){
236
		return id;
237
	}
419 rajveer 238
 
507 rajveer 239
	public void setId(String id){
240
		this.id = id;
241
	}
419 rajveer 242
 
507 rajveer 243
	public String getMyaccountHeaderSnippet(){
244
		return htmlSnippets.get("MYACCOUNT_HEADER");
245
	}
246
 
247
	public String getOrderDetailsSnippet(){
248
		return htmlSnippets.get("ORDER_DETAILS");
249
	}
250
 
712 rajveer 251
	public long getTxn(){
252
		return this.txnId;
517 rajveer 253
	}
894 rajveer 254
 
255
	/**
256
	 * 
257
	 * @param addressId
258
	 * @param currentCartId
259
	 * @return
260
	 */
5326 rajveer 261
	private boolean createOrders(long addressId, long currentCartId, long currentUserId){
3126 rajveer 262
		UserClient userServiceClient = null;
894 rajveer 263
		try {
3126 rajveer 264
			userServiceClient = new UserClient();
894 rajveer 265
		} catch (Exception e) {
2296 chandransh 266
			log.error("Unable to talk to the user context service.", e);
894 rajveer 267
			return false;
268
		}
269
 
270
		in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
271
		try {
272
			userClient.addAddressToCart(currentCartId, addressId);
273
		} catch (ShoppingCartException e1) {
2296 chandransh 274
			log.error("Not able to set address in the cart.", e1);
894 rajveer 275
			return false;
276
		} catch (TException e1) {
2296 chandransh 277
			log.error("Thrift exception while setting address in cart.", e1);
894 rajveer 278
			return false;
279
		}
280
 
281
 
282
		try {
3561 rajveer 283
			String errorMsg = userClient.validateCart(currentCartId, sourceId); 
1466 ankur.sing 284
			if(!errorMsg.isEmpty()){
285
				addActionError(errorMsg);
894 rajveer 286
				return false;
287
			}
288
		} catch (ShoppingCartException e1) {
2296 chandransh 289
			log.error("Error while validating shopping cart.", e1);
894 rajveer 290
			return false;
291
		} catch (TException e) {
2296 chandransh 292
			log.error("Thrift exception while validating cart.", e);
894 rajveer 293
			return false;
294
		}
295
 
296
 
297
		try {
3859 vikas 298
		    String sessionSrc = getCookie(TrackingInterceptor.SESSION_SRC_COOKIE, true, TrackingInterceptor.ENCRIPTION_STRING);
299
		    String firstSrc = getCookie(TrackingInterceptor.SRC_COOKIE, true, TrackingInterceptor.ENCRIPTION_STRING);
2817 vikas 300
 
3859 vikas 301
		    long sessionTime = 0;
302
            try {
303
                sessionTime = Long.parseLong(getCookie(TrackingInterceptor.SESSION_SRC_TIME_COOKIE, false, null));
304
            } catch (Exception e) {
305
                log.warn("Unable to parse session src time cookie.", e);
2817 vikas 306
            }
3859 vikas 307
            long firstSrcTime = 0;
308
            try {
309
                firstSrcTime = Long.parseLong(getCookie(TrackingInterceptor.SRC_TIME_COOKIE, false, null));
310
            } catch (Exception e) {
311
                log.warn("Unable to parse session src time cookie.", e);
312
            }
5326 rajveer 313
            txnId = userClient.createOrders(currentCartId, sessionSrc, sessionTime, firstSrc, firstSrcTime, currentUserId);
894 rajveer 314
		} catch (ShoppingCartException e1) {
2296 chandransh 315
			log.error("Error while creating orders from cart.", e1);
894 rajveer 316
			return false;
317
		} catch (TException e) {
2296 chandransh 318
			log.error("Thrift exception while creating orders from cart.", e);
894 rajveer 319
			return false;
320
		}
321
 
3126 rajveer 322
		TransactionClient transactionServiceClient = null;
4325 mandeep.dh 323
 
2183 vikas 324
        try {
3126 rajveer 325
            transactionServiceClient = new TransactionClient();
2183 vikas 326
            List<Order> orders = transactionServiceClient.getClient().getOrdersForTransaction(txnId, userinfo.getUserId());
327
            for (Order order : orders) {
3394 vikas 328
                List<Order> tmpOrders = new ArrayList<Order>();
329
                tmpOrders.add(order);
330
                String itemIdString = Utils.getItemIdStringFromOrders(tmpOrders);
3209 vikas 331
                DataLogger.logData(EventType.ORDER_CREATION, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), 
3212 vikas 332
                        Long.toString(order.getId()), Long.toString(currentCartId), itemIdString);
2183 vikas 333
            }
334
        } catch (Exception e1) {
2296 chandransh 335
        	log.warn("Unable to log orders through the datalogger", e1);
2183 vikas 336
        }
894 rajveer 337
 
338
		return true;
339
	}
4325 mandeep.dh 340
 
341
	/**
342
	 * This method updates address of a given order.
343
	 *
344
	 * @return
345
	 */
346
	public String modifyAddress() {
347
        long orderId   = Long.parseLong(request.getParameter("orderId"));
348
        long addressId = Long.valueOf(request.getParameter("addressId"));
349
 
350
		try {
351
		    TransactionClient transactionServiceClient = new TransactionClient();
352
			in.shop2020.model.v1.order.TransactionService.Client transactionClient
353
			    = transactionServiceClient.getClient();			
354
			transactionClient.updateShipmentAddress(orderId, addressId);
355
		} catch (TTransportException e) {
356
		    log.error("Could not update address: " + addressId + " for orderId: " + orderId, e);
357
		} catch (NumberFormatException e) {
358
		    log.error("Could not update address: " + addressId + " for orderId: " + orderId, e);
359
		} catch (TransactionServiceException e) {
360
		    log.error("Could not update address: " + addressId + " for orderId: " + orderId, e);
361
		} catch (TException e) {
362
		    log.error("Could not update address: " + addressId + " for orderId: " + orderId, e);
363
		}			
364
 
365
		return "index";
366
	}
367
 
1318 rajveer 368
	public String getRedirectURL(){
369
		return this.redirectURL;
370
	}
5716 anupam.sin 371
 
372
    public String getDeliveryLocation() {
373
        return deliveryLocation;
374
    }
375
 
376
    public void setDeliveryLocation(String deliveryLocation) {
377
        this.deliveryLocation = deliveryLocation;
378
    }
6050 anupam.sin 379
 
380
    public static void main(String[] args) {
381
        DesEncrypter d = new DesEncrypter("shop2020");
382
        System.out.println(d.decrypt("pmkcWEzhf4IFRLyHce89Cg"));
383
    }
419 rajveer 384
}