Subversion Repositories SmartDukaan

Rev

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