Subversion Repositories SmartDukaan

Rev

Rev 13524 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3101 chandransh 1
package in.shop2020.serving.controllers;
2
 
3209 vikas 3
import in.shop2020.datalogger.EventType;
6390 rajveer 4
import in.shop2020.model.v1.order.EmiScheme;
5
import in.shop2020.model.v1.order.TransactionService.Client;
3101 chandransh 6
import in.shop2020.model.v1.user.Address;
7
import in.shop2020.model.v1.user.Cart;
6302 amit.gupta 8
import in.shop2020.model.v1.user.PromotionService;
20278 aman.kumar 9
import in.shop2020.serving.services.IPaymentService;
3101 chandransh 10
import in.shop2020.serving.utils.FormattingUtils;
20278 aman.kumar 11
import in.shop2020.serving.utils.PaymentUtils;
3209 vikas 12
import in.shop2020.serving.utils.Utils;
6302 amit.gupta 13
import in.shop2020.thrift.clients.PromotionClient;
6390 rajveer 14
import in.shop2020.thrift.clients.TransactionClient;
3126 rajveer 15
import in.shop2020.thrift.clients.UserClient;
3209 vikas 16
import in.shop2020.utils.DataLogger;
3101 chandransh 17
 
6407 amit.gupta 18
import java.util.ArrayList;
3101 chandransh 19
import java.util.Collection;
7853 amit.gupta 20
import java.util.HashMap;
6390 rajveer 21
import java.util.List;
7853 amit.gupta 22
import java.util.Map;
3101 chandransh 23
import java.util.ResourceBundle;
24
 
6309 amit.gupta 25
import org.apache.commons.lang.StringUtils;
3101 chandransh 26
import org.apache.log4j.Logger;
27
import org.apache.struts2.convention.annotation.InterceptorRef;
28
import org.apache.struts2.convention.annotation.InterceptorRefs;
29
import org.apache.struts2.convention.annotation.Result;
30
import org.apache.struts2.convention.annotation.Results;
6390 rajveer 31
import org.apache.thrift.TException;
32
import org.apache.thrift.transport.TTransportException;
3101 chandransh 33
 
6407 amit.gupta 34
import com.google.gson.Gson;
35
 
3101 chandransh 36
@SuppressWarnings("serial")
20278 aman.kumar 37
@InterceptorRefs({ @InterceptorRef("myDefault"), @InterceptorRef("login") })
3101 chandransh 38
 
20278 aman.kumar 39
@Results({ @Result(name = "shipping-redirect", type = "redirectAction", params = { "actionName", "shipping" }) })
3101 chandransh 40
 
41
public class ProceedToPayController extends BaseController {
42
 
20278 aman.kumar 43
	private static Logger logger = Logger.getLogger(ProceedToPayController.class);
44
	private static final ResourceBundle resource = ResourceBundle.getBundle(ProceedToPayController.class.getName());
45
	private static final boolean SHOW_EBS_TEST_GATEWAY = Boolean
46
			.parseBoolean(resource.getString("show_ebs_test_gateway"));
3101 chandransh 47
 
20278 aman.kumar 48
	private static List<EmiScheme> emiSchemes;
49
	private static String emiSchemesInJSON;
50
	private boolean hasGiftVoucher = false;
51
	private long addressId = -1;
52
	private String totalAmount = "";
53
	private double totalAmountD = 0l;
54
	private boolean showCodOption = true;
55
	private boolean showEmiOption = false;
56
	private String errorMsg = "";
57
	private String deliveryLocation = ""; // This could be set as myLocation or
58
											// HotSpot
59
	private boolean showStorePickUpOption = false;
60
	private long hotSpotAddressId = 1;
61
	private boolean amountZero = false;
3101 chandransh 62
 
20278 aman.kumar 63
	public String index() {
64
		return processPaymentOptions();
65
	}
66
 
67
	public String create() {
68
		String addressIdString = this.request.getParameter("addressid");
69
		if (addressIdString == null) {
70
			addActionError("Please specify shipping address to continue.");
71
			return "shipping-redirect";
72
		}
73
 
74
		try {
75
			addressId = Long.parseLong(addressIdString);
76
		} catch (NumberFormatException nfe) {
77
			logger.error("Unable to parse address id", nfe);
78
			addActionError("Please specify shipping address to continue.");
79
			return "shipping-redirect";
80
		}
81
		return processPaymentOptions();
82
	}
83
 
84
	private String processPaymentOptions() {
85
		if (emiSchemes == null) {
86
			populateEmiSchemes();
87
		}
88
 
89
		String showEmiOptionStr = this.request.getParameter("showEmiOption");
90
		if (showEmiOptionStr != null) {
91
			try {
92
				showEmiOption = Boolean.parseBoolean(showEmiOptionStr);
93
			} catch (Exception e) {
94
				logger.info("A non-boolean value passed for showing EMI option");
95
			}
96
		}
97
 
98
		UserClient userContextServiceClient = null;
99
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
100
 
101
		Address address;
102
		String itemIdString = "";
103
		try {
104
			userContextServiceClient = new UserClient();
105
			userClient = userContextServiceClient.getClient();
106
 
107
			long cartId = userinfo.getCartId();
108
			if (deliveryLocation.equals("myLocation")) {
109
				userClient.addStoreToCart(cartId, 0);
110
			}
111
			// Validate the cart to ensure that we are not accepting payment for
112
			// an invalid order.
113
			errorMsg = userClient.validateCart(cartId, sourceId).get(0);
114
 
115
			Cart cart = userClient.getCart(cartId);
116
			String couponCode = cart.getCouponCode();
117
			logger.info("Coupon: " + couponCode);
118
 
119
			setTotalAmount(cart);
120
			itemIdString = Utils.getItemIdStringInCart(cart);
121
 
122
			PromotionService.Client pc = new PromotionClient().getClient();
123
			/*
124
			 * If a gift voucher is used such that all of the amount is paid by
125
			 * the gift voucher then we do not need set the hasGiftVoucher flag.
126
			 */
127
			if (StringUtils.isNotEmpty(couponCode) && pc.isGiftVoucher(couponCode) && getTotalAmountL() > 0) {
128
				hasGiftVoucher = true;
129
			}
130
 
131
			if (!hasGiftVoucher && deliveryLocation.equals("HotSpot")) {
132
				userClient.addStoreToCart(cartId, hotSpotAddressId);
133
				showStorePickUpOption = true;
134
			}
135
 
136
			// Get the address to check if COD option is available for this
137
			// address.
138
			if (addressId == -1) {
139
				addressId = cart.getAddressId();
140
			}
141
			address = userClient.getAddressById(addressId);
142
 
143
			try {
144
				showCodOption = userClient.showCODOption(cartId, sourceId, address.getPin());
145
			} catch (Exception e) {
146
				logger.error("Error while checking if COD is available for: " + showCodOption, e);
147
				showCodOption = false; // Not a critical error, proceeding with
148
										// defensive behaviour.
149
			}
150
 
151
			showStorePickUpOption = showStorePickUpOption && showCodOption;
152
 
153
			// DataLogger.logData(EventType.PROCEED_TO_PAY, getSessionId(),
154
			// userinfo.getUserId(), userinfo.getEmail(),
155
			// Long.toString(cartId), itemIdString);
156
		} catch (Exception e) {
157
			logger.error("Error while either validating the cart or getting the address", e);
158
			addActionError("We are experiencing some problem. Please try again.");
159
			return "shipping-redirect";
160
		}
161
 
162
		Collection<String> actionErrors = getActionErrors();
163
		if (actionErrors != null && !actionErrors.isEmpty()) {
164
			for (String str : actionErrors) {
165
				errorMsg += str + "<BR/>";
166
			}
167
		}
168
 
169
		return "index";
170
	}
171
 
172
	public static void populateEmiSchemes() {
173
		try {
6390 rajveer 174
			Client tClient = new TransactionClient().getClient();
175
			emiSchemes = tClient.getAvailableEmiSchemes();
176
		} catch (TTransportException e) {
177
			logger.error("Error while getting EMI schemes: ", e);
178
		} catch (TException e) {
179
			logger.error("Error while getting EMI schemes: ", e);
180
		}
20278 aman.kumar 181
	}
3101 chandransh 182
 
20278 aman.kumar 183
	private void setTotalAmount(Cart cart) {
184
		String couponCode = cart.getCouponCode();
185
		if (couponCode == null || "".equals(couponCode))
186
			totalAmountD = cart.getTotalPrice();
187
		else
188
			totalAmountD = cart.getDiscountedPrice();
3101 chandransh 189
 
20278 aman.kumar 190
		FormattingUtils formattingUtils = new FormattingUtils();
191
		totalAmount = formattingUtils.formatPrice(totalAmountD);
192
		if (totalAmountD == 0) {
193
			amountZero = true;
194
		}
195
	}
3616 chandransh 196
 
20278 aman.kumar 197
	public long getAddressId() {
198
		return this.addressId;
199
	}
200
 
201
	public String getErrorMsg() {
202
		logger.info("added error msg:" + this.errorMsg);
203
		return this.errorMsg;
204
	}
205
 
206
	public boolean shouldShowCodOption() {
207
		return showCodOption;
208
	}
209
 
210
	public boolean shouldShowEmiOption() {
211
		return showEmiOption;
212
	}
213
 
214
	public String getTotalAmount() {
215
		return totalAmount;
216
	}
217
 
218
	public double getTotalAmountL() {
219
		return totalAmountD;
220
	}
221
 
222
	public boolean shouldShowEbsTestGateway() {
223
		return SHOW_EBS_TEST_GATEWAY;
224
	}
225
 
3903 varun.gupt 226
	@Override
227
	public String getHeaderSnippet() {
228
		String url = request.getQueryString();
229
		if (url == null) {
230
			url = "";
231
		} else {
232
			url = "?" + url;
233
		}
234
		url = request.getRequestURI() + url;
20278 aman.kumar 235
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url, 0,
236
				false, userinfo.isPrivateDealUser());
3903 varun.gupt 237
	}
5716 anupam.sin 238
 
20278 aman.kumar 239
	public String getDeliveryLocation() {
240
		return deliveryLocation;
241
	}
5716 anupam.sin 242
 
20278 aman.kumar 243
	public void setDeliveryLocation(String deliveryLocation) {
244
		this.deliveryLocation = deliveryLocation;
245
	}
5716 anupam.sin 246
 
20278 aman.kumar 247
	public long getHotSpotAddressId() {
248
		return hotSpotAddressId;
249
	}
5716 anupam.sin 250
 
20278 aman.kumar 251
	public void setHotSpotAddressId(long hotSpotAddressId) {
252
		this.hotSpotAddressId = hotSpotAddressId;
253
	}
5716 anupam.sin 254
 
20278 aman.kumar 255
	public boolean shouldShowStorePickUpOption() {
256
		return showStorePickUpOption;
257
	}
7862 anupam.sin 258
 
20278 aman.kumar 259
	public String getJSONEmiSchemes() {
260
		getEmiSchemes();
261
		Double totalAmount = getTotalAmountL();
262
		List<EmiScheme> schemes = new ArrayList<EmiScheme>();
263
		for (EmiScheme emiScheme : emiSchemes) {
264
			if (emiScheme.getMinAmount() <= totalAmount) {
265
				schemes.add(emiScheme);
266
			}
267
		}
268
		Gson gson = new Gson();
269
		return gson.toJson(schemes);
270
	}
7862 anupam.sin 271
 
20278 aman.kumar 272
	public String getJSONEmiDiscountSchemes() {
273
		try {
274
			PromotionService.Client pc = new PromotionClient().getClient();
275
			Gson gson = new Gson();
276
			return gson.toJson(pc.getEmiDiscount(userinfo.getCartId()));
277
		} catch (Exception e) {
278
			logger.warn("Could not retrive emi based promotions for cart: " + userinfo.getCartId() + ".\n" + e);
279
			return "{}";
280
		}
281
	}
282
 
283
	public static List<EmiScheme> getEmiSchemes() {
284
		if (emiSchemes != null) {
285
			return emiSchemes;
286
		} else {
287
			populateEmiSchemes();
288
			return emiSchemes;
289
		}
290
	}
291
 
292
	public static long getGatewayId(String paymentOptions) {
293
		String payCode = "";
294
		// checking for emis
295
		if (paymentOptions.startsWith(PaymentUtils.PAYMENT_TYPE.EMI.toString())) {
296
			payCode = paymentOptions.replace(PaymentUtils.PAYMENT_TYPE.EMI.toString(), "");
297
			for (EmiScheme scheme : emiSchemes) {
298
				if (scheme.getId() == Long.parseLong(payCode)) {
299
					return scheme.getGatewayId();
300
				}
301
			}
302
		}
303
 
304
		// As of on august 2 2016 all the debit,credit and netbanking payments
305
		// have to be redirected to payu.
306
		// so returning 13 as payment gateway
307
		// this can be re-written for other business logic
308
		if (paymentOptions.startsWith(PaymentUtils.PAYMENT_TYPE.CC.toString())) {
309
			// payCode =
310
			// paymentOptions.replace(PaymentUtils.PAYMENT_TYPE.CC.toString(),
311
			// "");
312
			return 5;
313
 
314
		}
315
		if (paymentOptions.startsWith(PaymentUtils.PAYMENT_TYPE.DC.toString())) {
316
			// payCode =
317
			// paymentOptions.replace(PaymentUtils.PAYMENT_TYPE.CC.toString(),
318
			// "");
319
			return 5;
320
		}
321
		if (paymentOptions.startsWith(PaymentUtils.PAYMENT_TYPE.NB.toString())) {
322
			// payCode =
323
			// paymentOptions.replace(PaymentUtils.PAYMENT_TYPE.CC.toString(),
324
			// "");
325
			return 5;
326
		}
327
		if (paymentOptions.startsWith(PaymentUtils.PAYMENT_TYPE.WAL.toString())) {
328
			payCode = paymentOptions.replace(PaymentUtils.PAYMENT_TYPE.WAL.toString(), "");
329
			if (PaymentUtils.PAYU_CC.equals(payCode))
330
				return 5;
331
			return 5;
332
		}
333
		if (paymentOptions.startsWith(PaymentUtils.PAYMENT_TYPE.CAS.toString())) {
334
			return 18;
335
		}
336
		if (paymentOptions.startsWith(PaymentUtils.PAYMENT_TYPE.COD.toString())) {
337
			payCode = paymentOptions.replace(PaymentUtils.PAYMENT_TYPE.COD.toString(), "");
338
			if (payCode.equals(IPaymentService.COD)) {
339
				return 4;
340
			}
341
			if (payCode.equals(IPaymentService.COUPON)) {
342
				return 17;
343
			}
344
		}
345
 
346
		return 0;
347
	}
348
 
349
	public static double getInterestRate(long emiSchemeId) {
350
		for (EmiScheme scheme : emiSchemes) {
351
			if (scheme.getId() == emiSchemeId) {
352
				return scheme.getInterestRate();
353
			}
354
		}
355
		return 0;
356
	}
357
 
358
	public void setAmountZero(boolean amountZero) {
359
		this.amountZero = amountZero;
360
	}
361
 
362
	public boolean isAmountZero() {
363
		return amountZero;
364
	}
365
 
366
	public static String getEmiSchemesInJSON() {
367
		if (emiSchemesInJSON == null) {
368
			List<Map<String, Object>> schemeList = new ArrayList<Map<String, Object>>();
369
			for (EmiScheme scheme : getEmiSchemes()) {
370
				Map<String, Object> arrayMap = new HashMap<String, Object>();
371
				arrayMap.put("bankId", scheme.getBankId());
372
				arrayMap.put("bankName", scheme.getBankName());
373
				arrayMap.put("chargeType", scheme.getChargeType());
374
				arrayMap.put("chargeValue", scheme.getChargeValue());
375
				arrayMap.put("gatewayId", scheme.getGatewayId());
376
				arrayMap.put("minAmount", scheme.getMinAmount());
377
				arrayMap.put("tenure", scheme.getTenure());
378
				arrayMap.put("tenureDescription", scheme.getTenureDescription());
379
				arrayMap.put("id", scheme.getId());
380
				arrayMap.put("interestRate", scheme.getInterestRate());
381
				schemeList.add(arrayMap);
382
			}
383
			return new Gson().toJson(schemeList);
384
		} else {
385
			return emiSchemesInJSON;
386
		}
387
	}
388
 
389
	public boolean isPrivateDealUser() {
390
		return userinfo.isPrivateDealUser();
391
	}
3903 varun.gupt 392
}