Subversion Repositories SmartDukaan

Rev

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