Subversion Repositories SmartDukaan

Rev

Rev 7080 | Rev 7104 | 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)) {
98
                            errorMessage = "MaxDiscount : " + frc.getMaxDiscount() + " is less than discount amount entered";
99
                            log.warn("MaxDiscount : " + frc.getMaxDiscount() + " is less than discount amount entered");
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);
210
            setRechargeOrderId(rechargeOrder.getId());
211
 
212
        } catch (Exception e) {
213
            log.error("Unable to create recharge order", e);
214
            errorMessage = "Oops! There seems to be a problem. Please try after some time";
215
            return "recharge-redirect";
216
        }
7080 anupam.sin 217
        return "recharge-result";
7073 anupam.sin 218
    }
219
 
220
    public String getOperatorName() {
221
        String operatorName = null;
7080 anupam.sin 222
        if(deviceType.equals("1")){
223
            operatorName = HomeController.getMobileProvidersMap().get(Long.parseLong(mobileOperator)); 
7073 anupam.sin 224
        }
7080 anupam.sin 225
        if(deviceType.equals("2")){
226
            operatorName = HomeController.getDthProvidersMap().get(Long.parseLong(dthOperator));    
7073 anupam.sin 227
        }
228
        if(operatorName == null){
229
            operatorName = "N/A";   
230
        }
231
        return operatorName;
232
    }
233
 
234
    public String remoteAddr(HttpServletRequest request) {
235
        String remoteAddr = "";
236
        String x;
237
        x = request.getHeader(HEADER_X_FORWARDED_FOR);
238
        if (x != null && !x.isEmpty()) {
239
            remoteAddr = x;
240
            int idx = remoteAddr.lastIndexOf(',');
241
            if (idx > -1) {
242
                remoteAddr = remoteAddr.substring(idx + 1).trim();
243
            }
244
        } else {
245
            remoteAddr = request.getRemoteAddr();
246
        }
247
        return remoteAddr;
248
    }
249
 
250
    private String validateFields() {
251
        if(amount == null || amount == "") {
252
            log.warn("Amount received is empty or null");
253
            return "Amount cannot be empty";
254
        }
255
 
256
        if(rechargeType == null || rechargeType == "") {
257
            log.warn("rechargeType received is empty or null");
258
            return "Oops! There seems to be a problem. Please try after some time";
259
        }
260
 
7096 anupam.sin 261
        if(deviceType == null || rechargeType == "") {
262
            log.warn("rechargeType received is empty or null");
263
            return "Oops! There seems to be a problem. Please try after some time";
7073 anupam.sin 264
        }
265
 
7096 anupam.sin 266
        if (RechargeType.findByValue(Integer.parseInt(this.deviceType)) == RechargeType.DTH) {
7073 anupam.sin 267
            if (Long.parseLong(totalAmount) < 250 || Long.parseLong(totalAmount) > 2000) {
7096 anupam.sin 268
                if(rechargeType.equals("2") && getOperatorName().equals(DISHTV)) {
269
                    return "DishTv recharge amount should be between Rs.250 and Rs.2000";
270
                }
7073 anupam.sin 271
 
272
                if (!(Long.parseLong(totalAmount) >= 200 && Long.parseLong(totalAmount) < 250)) {
273
                    return "DTH recharge amount should be between Rs.200 and Rs.2000";
274
                }
275
            }
276
 
7096 anupam.sin 277
        } else if (RechargeType.findByValue(Integer.parseInt(this.deviceType)) == RechargeType.MOBILE) {
278
            if (Long.parseLong(amount) < 10 || Long.parseLong(amount) > 1000) {
7073 anupam.sin 279
                return "Recharge amount should be between Rs.10 and Rs.1000";
280
            }
281
            if (number.length() != 10) {
282
                return "Number should be of 10 digits";
283
            }
284
 
285
        } else {
7096 anupam.sin 286
            log.warn("Invalid rechargeType : " + deviceType);
7073 anupam.sin 287
            return "Oops! There seems to be a problem. Please try after some time";
288
        }
289
 
7096 anupam.sin 290
        if (mobileOperator == null || mobileOperator == "") {
291
            log.warn("Invalid operator : " + mobileOperator);
292
            return "Oops! There seems to be a problem. Please try after some time";
293
        }
294
 
295
        if (dthOperator == null || dthOperator == "") {
296
            log.warn("Invalid operator : " + dthOperator);
297
            return "Oops! There seems to be a problem. Please try after some time";
298
        }
299
 
7073 anupam.sin 300
        return "SUCCESS";
301
    }
302
 
303
    public String getAmount() {
304
        return amount;
305
    }
306
    public void setAmount(String amount) {
307
        this.amount = amount;
308
    }
309
 
310
    public void setDthamount(String amount) {
311
        this.amount = amount;
312
    }
7080 anupam.sin 313
 
7073 anupam.sin 314
    public String getNumber() {
315
        return number;
316
    }
317
    public void setNumber(String number) {
318
        this.number = number;
319
    }
320
 
321
    public void setDthnumber(String dthnumber) {
322
        this.number = dthnumber;
323
    }
324
 
325
 
326
    public String getEmail() {
327
        return email;
328
    }
329
    public void setEmail(String email) {
330
        this.email = email;
331
    }
332
 
333
    public String getRechargeType() {
334
        return rechargeType;
335
    }
336
 
337
    public void setRechargeType(String rechargeType) {
338
        this.rechargeType = rechargeType;
339
    }
340
 
341
    public void setMessage(String message) {
342
        this.message = message;
343
    }
344
 
345
    public String getMessage() {
346
        return message;
347
    }
348
 
349
    public long getRechargeOrderId() {
350
        return rechargeOrderId;
351
    }
352
 
353
    public void setRechargeOrderId(long l) {
354
        this.rechargeOrderId = l;
355
    }
356
 
357
    public String getTotalAmount() {
358
        return totalAmount;
359
    }
360
 
361
    public void setTotalAmount(String amount) {
362
        this.totalAmount  = amount;
363
 
364
    }
365
 
366
    public String getPlan() {
367
        return plan;
368
    }
369
 
370
    public void setPlan(String plan) {
371
        this.plan = plan;
372
    }
373
 
374
    public String getErrorMessage() {
375
        return errorMessage;
376
    }
377
 
378
    public void setErrorMessage(String errorMessage) {
379
        this.errorMessage = errorMessage;
380
    }
381
 
382
    public void setDiscountCode(String discountCode) {
383
        this.couponCode = discountCode;
384
    }
385
 
386
    public String getCouponCode() {
387
        return couponCode;
388
    }
389
 
390
    public void setCouponCode(String couponCode) {
391
        this.couponCode = couponCode;
392
    }
393
 
394
    public void setCouponAmount(long couponAmount) {
395
        this.couponAmount = couponAmount;
396
    }
397
 
398
    public long getCouponAmount() {
399
        return couponAmount;
400
    }
401
 
402
    public void setCouponMessage(String couponMessage) {
403
        this.couponMessage = couponMessage;
404
    }
405
 
406
    public String getCouponMessage() {
407
        return couponMessage;
408
    }
409
 
410
    public void setHash(String hash) {
411
        this.hiddenUserId = hash;
412
    }
413
 
414
    public String getHiddenUserId() {
415
        return hiddenUserId;
416
    }
417
 
418
    public String getDeviceType() {
419
        return deviceType;
420
    }
421
 
422
    public void setDeviceType(String deviceType) {
423
        this.deviceType = deviceType;
424
    }
425
 
426
    public String getStoreId() {
427
        return storeId;
428
    }
429
 
430
    public void setStoreId(String storeId) {
431
        this.storeId = storeId;
432
    }
433
 
434
    public String getCafNum() {
435
        return cafNum;
436
    }
437
 
438
    public void setCafNum(String cafNum) {
439
        this.cafNum = cafNum;
440
    }
441
 
442
    public String getSimNum() {
443
        return simNum;
444
    }
445
 
446
    public void setSimNum(String simNum) {
447
        this.simNum = simNum;
448
    }
449
 
450
    public String getName() {
451
        return name;
452
    }
453
 
454
    public void setName(String name) {
455
        this.name = name;
456
    }
7080 anupam.sin 457
 
458
    public void setAltNum(String altNum) {
459
        this.altNum = altNum;
460
    }
461
 
462
    public String getAltNum() {
463
        return altNum;
464
    }
465
 
466
    public String getMobileOperator() {
467
        return mobileOperator;
468
    }
469
 
470
    public void setMobileOperator(String mobileOperator) {
471
        this.mobileOperator = mobileOperator;
472
    }
473
 
474
    public String getDthOperator() {
475
        return dthOperator;
476
    }
477
 
478
    public void setDthOperator(String dthOperator) {
479
        this.dthOperator = dthOperator;
480
    }
481
 
482
    public void setDiscount(String discount) {
483
        if(discount == null || discount.isEmpty()) {
484
            this.discount = "0";
485
        } else {
486
            this.discount = discount;
487
        }
488
    }
489
 
490
    public String getDiscount() {
491
        return discount;
492
    }
493
 
7096 anupam.sin 494
    public void setCirclecode(String circlecode) {
495
        this.circlecode = circlecode;
7080 anupam.sin 496
    }
497
 
7096 anupam.sin 498
    public String getCirclecode() {
499
        return circlecode;
7080 anupam.sin 500
    }
7073 anupam.sin 501
}