Subversion Repositories SmartDukaan

Rev

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