Subversion Repositories SmartDukaan

Rev

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