Subversion Repositories SmartDukaan

Rev

Rev 6335 | Rev 6390 | 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
 
6353 rajveer 110
	        //Check that this address is eligible for COD shipping.
111
    	    UserClient userServiceClient = null;
112
            try {
113
                userServiceClient = new UserClient();
114
                in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
115
                Address address = userClient.getAddressById(addressId);
116
 
117
                if(!userClient.showCODOption(currentCartId, sourceId, address.getPin())){
118
                    addActionError("Cash on Delivery is currently not available for this pincode. Please choose a different payment option.");
119
                    return "proceed-to-pay-redirect";    
120
                }
121
            } catch (Exception e) {
122
                log.error("Unable to talk to the user context service.", e);
123
                addActionError("We are experiencing some problems. Please try later.");
124
                return "proceed-to-pay-redirect";
125
            }
3063 chandransh 126
    	}
127
 
128
    	try {
5326 rajveer 129
    		if(!createOrders(addressId, currentCartId, currentUserId)) {
1128 rajveer 130
    			addActionError("We are experiencing some problems. Please try later.");
3101 chandransh 131
    			return "proceed-to-pay-redirect";
894 rajveer 132
    		}
3063 chandransh 133
    	} catch (Exception e) {
1128 rajveer 134
    		addActionError("We are experiencing some problems. Please try later.");
2296 chandransh 135
    		log.error("Exception in createOrders function. Something went wrong.", e);
3101 chandransh 136
    		return "proceed-to-pay-redirect";
693 rajveer 137
		}
2159 chandransh 138
 
4325 mandeep.dh 139
    	if(paymentOption.equals(IPaymentService.COD)) {
3063 chandransh 140
    	    IPaymentService codPaymentService = new CodPaymentService();
3561 rajveer 141
    	    paymentId = codPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption, sourceId);
3063 chandransh 142
            if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
143
                //Very unlikely. The only possible reason can be that the payment service is down.
144
                log.error("Unable to process the COD payment.");
145
                addActionError("We are experiencing some problems. Please try later.");
3101 chandransh 146
                return "proceed-to-pay-redirect";
3063 chandransh 147
            } else {
148
                CommonPaymentService.processCodTxn(txnId);
149
                return "cod-redirect";
150
            }
3616 chandransh 151
    	} else if(paymentOption.equals(IPaymentService.HDFC_EMI)){
152
    	    IPaymentService hdfcEmiPaymentService = new HdfcEmiPaymentService();
153
            paymentId = hdfcEmiPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption, sourceId);
154
            if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
155
                addActionError("We are experiencing some problems. Please try later.");
156
                log.error("Unable to process payment through HDFC EMI.");
157
                return "proceed-to-pay-redirect";
158
            } else {
3617 chandransh 159
                this.redirectURL = ((HdfcEmiPaymentService)hdfcEmiPaymentService).getRedirectUrl();
3616 chandransh 160
                log.info(this.redirectURL);
161
                return "success";
162
            }
3063 chandransh 163
    	} else {
5538 anupam.sin 164
    		if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || 
165
    		        paymentOption.equals(IPaymentService.HDFC_VISA) || paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON)) {
166
                // User has selected Visa or MasterCard
3063 chandransh 167
                IPaymentService hdfcPaymentService = new HdfcPaymentService();
3561 rajveer 168
                paymentId = hdfcPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption, sourceId);
3063 chandransh 169
                if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
170
                    log.error("Unable to process payment through HDFC. Falling through to EBS.");
171
                } else {
172
                    this.redirectURL = ((HdfcPaymentService)hdfcPaymentService).getRedirectUrl();
173
                    log.info(this.redirectURL);
174
                    return "success";
175
                }
176
            }
2199 chandransh 177
 
3063 chandransh 178
            if(paymentOption.equals(IPaymentService.HDFC_VISA))
179
                paymentOption = IPaymentService.EBS_VISA;
5538 anupam.sin 180
            else if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT))
3063 chandransh 181
                paymentOption = IPaymentService.EBS_MASTERCARD;
182
            else if(paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON))
183
                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
 
6060 rajveer 185
            if(paymentOption.equals(IPaymentService.PAYU_CC)){
186
            	paymentOption = IPaymentService.PAYU_CC;
187
            	IPaymentService payuPaymentService = new PayuPaymentService();
188
            	paymentId = payuPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption, sourceId);
189
            	return "payu-pay-redirect";
190
            }
191
 
3063 chandransh 192
            IPaymentService ebsPaymentService = new EbsPaymentService();
3561 rajveer 193
            paymentId = ebsPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption, sourceId);
3063 chandransh 194
            if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
195
                addActionError("We are experiencing some problems. Please try later.");
3616 chandransh 196
                log.error("Unable to process payment through EBS.");
3101 chandransh 197
                return "proceed-to-pay-redirect";
3063 chandransh 198
            } else {
199
                log.info("Successfully created payment for EBS to process. Redirecting to /ebs-pay/" + paymentId);
200
                return "ebs-pay-redirect";
201
            }    	    
202
    	}
203
 
572 chandransh 204
    }
3063 chandransh 205
 
206
    /**
207
     * Verifies if the recaptcha response matches the recaptcha challenge.
208
     * 
209
     * @return True if the captcha was valid, false otherwise.
210
     */
211
    private boolean verifyCaptcha() {
4609 phani.kuma 212
        String cookieCaptchaAnswer = getCookie(Captcha.NAME, true, "saholic");
213
    	String captchaReceived = (String) request.getParameter("captcha_response_field");
214
 
215
    	if (captchaReceived.equalsIgnoreCase(cookieCaptchaAnswer)) {
216
			try {
217
				DataLogger.logData(EventType.CAPTCHA_SUCCESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail());
218
			} catch (Exception e1) {
219
	        	log.warn("Unable to log orders through the datalogger", e1);
220
	        }
221
			return true;
222
		} 
223
		else {
224
			try {
225
				DataLogger.logData(EventType.CAPTCHA_FAILED, getSessionId(), userinfo.getUserId(), userinfo.getEmail());
226
			} catch (Exception e1) {
227
	        	log.warn("Unable to log orders through the datalogger", e1);
228
	        }
229
			return false;
230
		}
3063 chandransh 231
    }
572 chandransh 232
 
507 rajveer 233
	public String getId(){
234
		return id;
235
	}
419 rajveer 236
 
507 rajveer 237
	public void setId(String id){
238
		this.id = id;
239
	}
419 rajveer 240
 
507 rajveer 241
	public String getMyaccountHeaderSnippet(){
242
		return htmlSnippets.get("MYACCOUNT_HEADER");
243
	}
244
 
245
	public String getOrderDetailsSnippet(){
246
		return htmlSnippets.get("ORDER_DETAILS");
247
	}
248
 
712 rajveer 249
	public long getTxn(){
250
		return this.txnId;
517 rajveer 251
	}
894 rajveer 252
 
253
	/**
254
	 * 
255
	 * @param addressId
256
	 * @param currentCartId
257
	 * @return
258
	 */
5326 rajveer 259
	private boolean createOrders(long addressId, long currentCartId, long currentUserId){
3126 rajveer 260
		UserClient userServiceClient = null;
894 rajveer 261
		try {
3126 rajveer 262
			userServiceClient = new UserClient();
894 rajveer 263
		} catch (Exception e) {
2296 chandransh 264
			log.error("Unable to talk to the user context service.", e);
894 rajveer 265
			return false;
266
		}
267
 
268
		in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
269
		try {
270
			userClient.addAddressToCart(currentCartId, addressId);
271
		} catch (ShoppingCartException e1) {
2296 chandransh 272
			log.error("Not able to set address in the cart.", e1);
894 rajveer 273
			return false;
274
		} catch (TException e1) {
2296 chandransh 275
			log.error("Thrift exception while setting address in cart.", e1);
894 rajveer 276
			return false;
277
		}
278
 
279
 
280
		try {
3561 rajveer 281
			String errorMsg = userClient.validateCart(currentCartId, sourceId); 
1466 ankur.sing 282
			if(!errorMsg.isEmpty()){
283
				addActionError(errorMsg);
894 rajveer 284
				return false;
285
			}
286
		} catch (ShoppingCartException e1) {
2296 chandransh 287
			log.error("Error while validating shopping cart.", e1);
894 rajveer 288
			return false;
289
		} catch (TException e) {
2296 chandransh 290
			log.error("Thrift exception while validating cart.", e);
894 rajveer 291
			return false;
292
		}
293
 
294
 
295
		try {
3859 vikas 296
		    String sessionSrc = getCookie(TrackingInterceptor.SESSION_SRC_COOKIE, true, TrackingInterceptor.ENCRIPTION_STRING);
297
		    String firstSrc = getCookie(TrackingInterceptor.SRC_COOKIE, true, TrackingInterceptor.ENCRIPTION_STRING);
2817 vikas 298
 
3859 vikas 299
		    long sessionTime = 0;
300
            try {
301
                sessionTime = Long.parseLong(getCookie(TrackingInterceptor.SESSION_SRC_TIME_COOKIE, false, null));
302
            } catch (Exception e) {
303
                log.warn("Unable to parse session src time cookie.", e);
2817 vikas 304
            }
3859 vikas 305
            long firstSrcTime = 0;
306
            try {
307
                firstSrcTime = Long.parseLong(getCookie(TrackingInterceptor.SRC_TIME_COOKIE, false, null));
308
            } catch (Exception e) {
309
                log.warn("Unable to parse session src time cookie.", e);
310
            }
5326 rajveer 311
            txnId = userClient.createOrders(currentCartId, sessionSrc, sessionTime, firstSrc, firstSrcTime, currentUserId);
894 rajveer 312
		} catch (ShoppingCartException e1) {
2296 chandransh 313
			log.error("Error while creating orders from cart.", e1);
894 rajveer 314
			return false;
315
		} catch (TException e) {
2296 chandransh 316
			log.error("Thrift exception while creating orders from cart.", e);
894 rajveer 317
			return false;
318
		}
319
 
3126 rajveer 320
		TransactionClient transactionServiceClient = null;
4325 mandeep.dh 321
 
2183 vikas 322
        try {
3126 rajveer 323
            transactionServiceClient = new TransactionClient();
2183 vikas 324
            List<Order> orders = transactionServiceClient.getClient().getOrdersForTransaction(txnId, userinfo.getUserId());
325
            for (Order order : orders) {
3394 vikas 326
                List<Order> tmpOrders = new ArrayList<Order>();
327
                tmpOrders.add(order);
328
                String itemIdString = Utils.getItemIdStringFromOrders(tmpOrders);
3209 vikas 329
                DataLogger.logData(EventType.ORDER_CREATION, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), 
3212 vikas 330
                        Long.toString(order.getId()), Long.toString(currentCartId), itemIdString);
2183 vikas 331
            }
332
        } catch (Exception e1) {
2296 chandransh 333
        	log.warn("Unable to log orders through the datalogger", e1);
2183 vikas 334
        }
894 rajveer 335
 
336
		return true;
337
	}
4325 mandeep.dh 338
 
339
	/**
340
	 * This method updates address of a given order.
341
	 *
342
	 * @return
343
	 */
344
	public String modifyAddress() {
345
        long orderId   = Long.parseLong(request.getParameter("orderId"));
346
        long addressId = Long.valueOf(request.getParameter("addressId"));
347
 
348
		try {
349
		    TransactionClient transactionServiceClient = new TransactionClient();
350
			in.shop2020.model.v1.order.TransactionService.Client transactionClient
351
			    = transactionServiceClient.getClient();			
352
			transactionClient.updateShipmentAddress(orderId, addressId);
353
		} catch (TTransportException e) {
354
		    log.error("Could not update address: " + addressId + " for orderId: " + orderId, e);
355
		} catch (NumberFormatException e) {
356
		    log.error("Could not update address: " + addressId + " for orderId: " + orderId, e);
357
		} catch (TransactionServiceException e) {
358
		    log.error("Could not update address: " + addressId + " for orderId: " + orderId, e);
359
		} catch (TException e) {
360
		    log.error("Could not update address: " + addressId + " for orderId: " + orderId, e);
361
		}			
362
 
363
		return "index";
364
	}
365
 
1318 rajveer 366
	public String getRedirectURL(){
367
		return this.redirectURL;
368
	}
5716 anupam.sin 369
 
370
    public String getDeliveryLocation() {
371
        return deliveryLocation;
372
    }
373
 
374
    public void setDeliveryLocation(String deliveryLocation) {
375
        this.deliveryLocation = deliveryLocation;
376
    }
6050 anupam.sin 377
 
378
    public static void main(String[] args) {
379
        DesEncrypter d = new DesEncrypter("shop2020");
380
        System.out.println(d.decrypt("pmkcWEzhf4IFRLyHce89Cg"));
381
    }
419 rajveer 382
}