Subversion Repositories SmartDukaan

Rev

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