Subversion Repositories SmartDukaan

Rev

Rev 7104 | Rev 7138 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7073 anupam.sin 1
package in.shop2020.recharge.controllers;
2
 
7080 anupam.sin 3
import in.shop2020.model.v1.order.FRC;
7073 anupam.sin 4
import in.shop2020.model.v1.order.RechargePlan;
5
import in.shop2020.model.v1.order.RechargeTransaction;
6
import in.shop2020.model.v1.order.RechargeType;
7096 anupam.sin 7
import in.shop2020.model.v1.order.TelecomCircle;
7073 anupam.sin 8
import in.shop2020.thrift.clients.TransactionClient;
9
 
10
import java.math.BigInteger;
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.Random;
14
 
15
import javax.mail.internet.AddressException;
16
import javax.mail.internet.InternetAddress;
17
import javax.servlet.http.HttpServletRequest;
18
 
19
import org.apache.log4j.Logger;
20
import org.apache.struts2.convention.annotation.Result;
21
import org.apache.struts2.convention.annotation.Results;
22
 
23
@Results({
7096 anupam.sin 24
    @Result(name="recharge-redirect", type="redirectAction", params = {"actionName" , "home", "errorMsg", "${errorMessage}"}),
7080 anupam.sin 25
    @Result(name="recharge-result", type="redirectAction", params = {"actionName" , "recharge-result", "rechargeId", "${rechargeOrderId}"})
7073 anupam.sin 26
})
27
 
28
public class ConfirmController extends BaseController{
29
 
30
    /**
31
     * 
32
     */
33
    private static final long serialVersionUID = 1L;
34
    private long rechargeOrderId = 0;
35
    private String amount = "";
36
    private String number = "";
37
    private String email = "";
38
    private String rechargeType = "";
39
    private String deviceType = "";
7080 anupam.sin 40
    private String mobileOperator = "";
41
    private String dthOperator = "";
7073 anupam.sin 42
    private String storeId = "";
43
    private String cafNum = "";
44
    private String simNum = "";
7080 anupam.sin 45
    private String altNum = "";
7073 anupam.sin 46
    private String name = "";
47
    private String plan = "";
7080 anupam.sin 48
    private String discount = "";
7096 anupam.sin 49
    private String circlecode = "";
7073 anupam.sin 50
 
51
    private String message = "";
52
    private String errorMessage = "";
53
    private String totalAmount = "";
54
    private String couponCode = "";
55
    private String couponMessage = "";
56
    private String hiddenUserId = "";
57
    private long couponAmount = 0;
58
    private static final BigInteger key = new BigInteger("0333910847013829827302347601486730417451");
59
 
60
    private static final Random random = new Random();
61
    private static final int LENGTH = 10;
62
    private static final String HEADER_X_FORWARDED_FOR = "X-FORWARDED-FOR";
7096 anupam.sin 63
    private static final String DISHTV = "DISH TV";
7073 anupam.sin 64
    private static Logger log = Logger.getLogger(Class.class);
65
 
66
    public String index() {
67
        return "index";
68
    }
69
 
70
    public String create() {
7096 anupam.sin 71
        try {
72
            String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
73
            if(loginStatus == null || !loginStatus.equals("TRUE")){
74
                return "authfail";
75
            }
76
 
77
            String result = validateFields();
78
            if(!result.equals("SUCCESS")) {
79
                errorMessage = result;
80
                return "recharge-redirect";
81
            }
82
            TransactionClient tcl = new TransactionClient();
83
            boolean discountRemove = true;
84
 
85
            Long circleId = 0l;
86
            if(circlecode != null && !circlecode.equals("")) {
87
                TransactionClient tcl1 = new TransactionClient();
88
                TelecomCircle circle = tcl1.getClient().getTelecomCircle(-1, circlecode);
89
                if(circle != null) {
90
                    circleId = circle.getId();
91
                }
92
            }
93
 
94
            if(rechargeType.equals("2")) {
95
                for (FRC frc : tcl.getClient().getFRCs(circleId, Long.parseLong(mobileOperator))) {
96
                    if(frc.getDenomination() == Long.parseLong(amount)) {
97
                        if(frc.getMaxDiscount() < Long.parseLong(discount)) {
7104 anupam.sin 98
                            errorMessage = "Maximum discount possible is Rs. " + frc.getMaxDiscount();
99
                            log.warn("Maximum discount possible is Rs. " + frc.getMaxDiscount());
7096 anupam.sin 100
                            return "recharge-redirect";
101
                        } else {
102
                            discountRemove = false;
103
                        }
104
                    }
105
                }
106
 
107
                if(discountRemove) {
108
                    setDiscount("0");
109
                }
110
            }
111
        } catch (Exception e) {
112
            log.warn("Unable to get frc for operatorId : " + mobileOperator, e);
113
        }
7073 anupam.sin 114
        return index();
115
    }
116
 
117
    public String createRecharge(){
118
        try {
7096 anupam.sin 119
            String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
120
            if(loginStatus == null || !loginStatus.equals("TRUE")){
121
                return "authfail";
122
            }
123
 
124
            String result = validateFields();
125
            if(!result.equals("SUCCESS")) {
126
                errorMessage = result;
127
                return "recharge-redirect";
128
            }
129
 
7073 anupam.sin 130
            TransactionClient tc = new TransactionClient();
7080 anupam.sin 131
            List<RechargePlan> plans = tc.getClient().getPlansForOperator(Long.parseLong(mobileOperator));
7073 anupam.sin 132
            List<String> planNameList = new ArrayList<String>();
133
            if (plans == null || plans.isEmpty()) {
134
                setPlan("");
135
            } else {
136
                for (RechargePlan tempPlan : plans) {
137
                    planNameList.add(tempPlan.getName());
138
                }
139
                if (!planNameList.contains(plan)) {
7080 anupam.sin 140
                    errorMessage = "OperatorId : " + mobileOperator + " and plan : " + plan + " do not match";
141
                    log.warn("OperatorId : " + mobileOperator + " and plan : " + plan + " do not match");
7073 anupam.sin 142
                    return "recharge-redirect";
143
                }
144
            }
145
 
146
            RechargeTransaction rechargeOrder = new RechargeTransaction();
7096 anupam.sin 147
 
7080 anupam.sin 148
            rechargeOrder.setAmount(Long.parseLong(amount));
7073 anupam.sin 149
            rechargeOrder.setEmail(email);
150
            rechargeOrder.setDeviceNum(number.trim());
151
            rechargeOrder.setPlan(plan);
7096 anupam.sin 152
            rechargeOrder.setAlternateNumber(altNum);
153
            rechargeOrder.setStoreId(Long.parseLong((String) request.getSession().getAttribute("STORE_ID")));
154
            rechargeOrder.setIpAddress(remoteAddr(request));
155
            rechargeOrder.setDeviceType(RechargeType.findByValue((int) Long.parseLong(deviceType)));
156
            rechargeOrder.setName(name);
157
            rechargeOrder.setIsFrc(Long.parseLong(rechargeType) == 1 ? false : true);
158
            rechargeOrder.setCafNum(cafNum);
159
            rechargeOrder.setSimNum(simNum);
160
 
7080 anupam.sin 161
            if(deviceType.equals("1")) {
162
                rechargeOrder.setOperatorId(Long.parseLong(mobileOperator));
163
            } else if (deviceType.equals("2")) {
164
                rechargeOrder.setOperatorId(Long.parseLong(dthOperator));
165
            }
166
 
7096 anupam.sin 167
            rechargeOrder.setPaymentAmount(Long.parseLong(amount) - Long.parseLong(discount));
7073 anupam.sin 168
 
7096 anupam.sin 169
            Long circleId = 0l;
170
            if(circlecode != null && !circlecode.equals("")) {
171
                TransactionClient tcl1 = new TransactionClient();
172
                TelecomCircle circle = tcl1.getClient().getTelecomCircle(-1, circlecode);
173
                if(circle != null) {
174
                    circleId = circle.getId();
175
                    if(rechargeOrder.isIsFrc()) {
176
                        Long storeCircle = (Long.parseLong((String) request.getSession().getAttribute("CIRCLE_ID")));
177
                        if(storeCircle == circle.getId()) {
178
                            rechargeOrder.setCircleId(storeCircle);
179
                        } else {
180
                            errorMessage = "FRC is not possible because this number is of : "  + circle.getName() + " circle.";
181
                            log.warn("FRC is not possible because this number is of : "  + circle.getName() + " circle & storeCircle : " + storeCircle);
182
                            return "recharge-redirect";
183
                        }
184
                    } else {
185
                        rechargeOrder.setCircleId(circle.getId());
186
                    }
187
                } else {
188
                    rechargeOrder.setCircleId(0);
189
                }
190
            }
191
 
7080 anupam.sin 192
            rechargeOrder.setDiscount(0);
193
            TransactionClient tcl = new TransactionClient();
194
            if(rechargeOrder.isIsFrc()) {
7096 anupam.sin 195
                for (FRC frc : tcl.getClient().getFRCs(circleId, rechargeOrder.getOperatorId())) {
7080 anupam.sin 196
                    if(frc.getDenomination() == rechargeOrder.getAmount()) {
197
                        if(frc.getMaxDiscount() < Long.parseLong(discount)) {
198
                            errorMessage = "MaxDiscount : " + frc.getMaxDiscount() + " is less than discount amount entered";
199
                            log.warn("MaxDiscount : " + frc.getMaxDiscount() + " is less than discount amount entered");
200
                            return "recharge-redirect";
201
                        } else {
202
                            rechargeOrder.setDiscount(Double.parseDouble(discount));
203
                        }
204
                    }
205
                }
206
            }
207
 
7073 anupam.sin 208
            TransactionClient tc1 = new TransactionClient();
209
            rechargeOrder = tc1.getClient().createRechargeTransaction(rechargeOrder);
7118 rajveer 210
            HomeController.hotspotStores.put(rechargeOrder.getId(), tc1.getClient().getHotspotStore(rechargeOrder.getId(), ""));
7073 anupam.sin 211
            setRechargeOrderId(rechargeOrder.getId());
212
 
213
        } catch (Exception e) {
214
            log.error("Unable to create recharge order", e);
215
            errorMessage = "Oops! There seems to be a problem. Please try after some time";
216
            return "recharge-redirect";
217
        }
7080 anupam.sin 218
        return "recharge-result";
7073 anupam.sin 219
    }
220
 
221
    public String getOperatorName() {
222
        String operatorName = null;
7080 anupam.sin 223
        if(deviceType.equals("1")){
224
            operatorName = HomeController.getMobileProvidersMap().get(Long.parseLong(mobileOperator)); 
7073 anupam.sin 225
        }
7080 anupam.sin 226
        if(deviceType.equals("2")){
227
            operatorName = HomeController.getDthProvidersMap().get(Long.parseLong(dthOperator));    
7073 anupam.sin 228
        }
229
        if(operatorName == null){
230
            operatorName = "N/A";   
231
        }
232
        return operatorName;
233
    }
234
 
235
    public String remoteAddr(HttpServletRequest request) {
236
        String remoteAddr = "";
237
        String x;
238
        x = request.getHeader(HEADER_X_FORWARDED_FOR);
239
        if (x != null && !x.isEmpty()) {
240
            remoteAddr = x;
241
            int idx = remoteAddr.lastIndexOf(',');
242
            if (idx > -1) {
243
                remoteAddr = remoteAddr.substring(idx + 1).trim();
244
            }
245
        } else {
246
            remoteAddr = request.getRemoteAddr();
247
        }
248
        return remoteAddr;
249
    }
250
 
251
    private String validateFields() {
252
        if(amount == null || amount == "") {
253
            log.warn("Amount received is empty or null");
254
            return "Amount cannot be empty";
255
        }
256
 
257
        if(rechargeType == null || rechargeType == "") {
258
            log.warn("rechargeType received is empty or null");
259
            return "Oops! There seems to be a problem. Please try after some time";
260
        }
261
 
7096 anupam.sin 262
        if(deviceType == null || rechargeType == "") {
263
            log.warn("rechargeType received is empty or null");
264
            return "Oops! There seems to be a problem. Please try after some time";
7073 anupam.sin 265
        }
266
 
7096 anupam.sin 267
        if (RechargeType.findByValue(Integer.parseInt(this.deviceType)) == RechargeType.DTH) {
7073 anupam.sin 268
            if (Long.parseLong(totalAmount) < 250 || Long.parseLong(totalAmount) > 2000) {
7096 anupam.sin 269
                if(rechargeType.equals("2") && getOperatorName().equals(DISHTV)) {
270
                    return "DishTv recharge amount should be between Rs.250 and Rs.2000";
271
                }
7073 anupam.sin 272
 
273
                if (!(Long.parseLong(totalAmount) >= 200 && Long.parseLong(totalAmount) < 250)) {
274
                    return "DTH recharge amount should be between Rs.200 and Rs.2000";
275
                }
276
            }
277
 
7096 anupam.sin 278
        } else if (RechargeType.findByValue(Integer.parseInt(this.deviceType)) == RechargeType.MOBILE) {
279
            if (Long.parseLong(amount) < 10 || Long.parseLong(amount) > 1000) {
7073 anupam.sin 280
                return "Recharge amount should be between Rs.10 and Rs.1000";
281
            }
282
            if (number.length() != 10) {
283
                return "Number should be of 10 digits";
284
            }
285
 
286
        } else {
7096 anupam.sin 287
            log.warn("Invalid rechargeType : " + deviceType);
7073 anupam.sin 288
            return "Oops! There seems to be a problem. Please try after some time";
289
        }
290
 
7096 anupam.sin 291
        if (mobileOperator == null || mobileOperator == "") {
292
            log.warn("Invalid operator : " + mobileOperator);
293
            return "Oops! There seems to be a problem. Please try after some time";
294
        }
295
 
296
        if (dthOperator == null || dthOperator == "") {
297
            log.warn("Invalid operator : " + dthOperator);
298
            return "Oops! There seems to be a problem. Please try after some time";
299
        }
300
 
7073 anupam.sin 301
        return "SUCCESS";
302
    }
303
 
304
    public String getAmount() {
305
        return amount;
306
    }
307
    public void setAmount(String amount) {
308
        this.amount = amount;
309
    }
310
 
311
    public void setDthamount(String amount) {
312
        this.amount = amount;
313
    }
7080 anupam.sin 314
 
7073 anupam.sin 315
    public String getNumber() {
316
        return number;
317
    }
318
    public void setNumber(String number) {
319
        this.number = number;
320
    }
321
 
322
    public void setDthnumber(String dthnumber) {
323
        this.number = dthnumber;
324
    }
325
 
326
 
327
    public String getEmail() {
328
        return email;
329
    }
330
    public void setEmail(String email) {
331
        this.email = email;
332
    }
333
 
334
    public String getRechargeType() {
335
        return rechargeType;
336
    }
337
 
338
    public void setRechargeType(String rechargeType) {
339
        this.rechargeType = rechargeType;
340
    }
341
 
342
    public void setMessage(String message) {
343
        this.message = message;
344
    }
345
 
346
    public String getMessage() {
347
        return message;
348
    }
349
 
350
    public long getRechargeOrderId() {
351
        return rechargeOrderId;
352
    }
353
 
354
    public void setRechargeOrderId(long l) {
355
        this.rechargeOrderId = l;
356
    }
357
 
358
    public String getTotalAmount() {
359
        return totalAmount;
360
    }
361
 
362
    public void setTotalAmount(String amount) {
363
        this.totalAmount  = amount;
364
 
365
    }
366
 
367
    public String getPlan() {
368
        return plan;
369
    }
370
 
371
    public void setPlan(String plan) {
372
        this.plan = plan;
373
    }
374
 
375
    public String getErrorMessage() {
376
        return errorMessage;
377
    }
378
 
379
    public void setErrorMessage(String errorMessage) {
380
        this.errorMessage = errorMessage;
381
    }
382
 
383
    public void setDiscountCode(String discountCode) {
384
        this.couponCode = discountCode;
385
    }
386
 
387
    public String getCouponCode() {
388
        return couponCode;
389
    }
390
 
391
    public void setCouponCode(String couponCode) {
392
        this.couponCode = couponCode;
393
    }
394
 
395
    public void setCouponAmount(long couponAmount) {
396
        this.couponAmount = couponAmount;
397
    }
398
 
399
    public long getCouponAmount() {
400
        return couponAmount;
401
    }
402
 
403
    public void setCouponMessage(String couponMessage) {
404
        this.couponMessage = couponMessage;
405
    }
406
 
407
    public String getCouponMessage() {
408
        return couponMessage;
409
    }
410
 
411
    public void setHash(String hash) {
412
        this.hiddenUserId = hash;
413
    }
414
 
415
    public String getHiddenUserId() {
416
        return hiddenUserId;
417
    }
418
 
419
    public String getDeviceType() {
420
        return deviceType;
421
    }
422
 
423
    public void setDeviceType(String deviceType) {
424
        this.deviceType = deviceType;
425
    }
426
 
427
    public String getStoreId() {
428
        return storeId;
429
    }
430
 
431
    public void setStoreId(String storeId) {
432
        this.storeId = storeId;
433
    }
434
 
435
    public String getCafNum() {
436
        return cafNum;
437
    }
438
 
439
    public void setCafNum(String cafNum) {
440
        this.cafNum = cafNum;
441
    }
442
 
443
    public String getSimNum() {
444
        return simNum;
445
    }
446
 
447
    public void setSimNum(String simNum) {
448
        this.simNum = simNum;
449
    }
450
 
451
    public String getName() {
452
        return name;
453
    }
454
 
455
    public void setName(String name) {
456
        this.name = name;
457
    }
7080 anupam.sin 458
 
459
    public void setAltNum(String altNum) {
460
        this.altNum = altNum;
461
    }
462
 
463
    public String getAltNum() {
464
        return altNum;
465
    }
466
 
467
    public String getMobileOperator() {
468
        return mobileOperator;
469
    }
470
 
471
    public void setMobileOperator(String mobileOperator) {
472
        this.mobileOperator = mobileOperator;
473
    }
474
 
475
    public String getDthOperator() {
476
        return dthOperator;
477
    }
478
 
479
    public void setDthOperator(String dthOperator) {
480
        this.dthOperator = dthOperator;
481
    }
482
 
483
    public void setDiscount(String discount) {
484
        if(discount == null || discount.isEmpty()) {
485
            this.discount = "0";
486
        } else {
487
            this.discount = discount;
488
        }
489
    }
490
 
491
    public String getDiscount() {
492
        return discount;
493
    }
494
 
7096 anupam.sin 495
    public void setCirclecode(String circlecode) {
496
        this.circlecode = circlecode;
7080 anupam.sin 497
    }
498
 
7096 anupam.sin 499
    public String getCirclecode() {
500
        return circlecode;
7080 anupam.sin 501
    }
7073 anupam.sin 502
}