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