Subversion Repositories SmartDukaan

Rev

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