Subversion Repositories SmartDukaan

Rev

Rev 6108 | Rev 6131 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6050 anupam.sin 1
package in.shop2020.serving.controllers;
2
 
6108 amar.kumar 3
import in.shop2020.utils.DataLogger;
6091 anupam.sin 4
import in.shop2020.datalogger.EventType;
6050 anupam.sin 5
import in.shop2020.model.v1.order.OrderType;
6
import in.shop2020.model.v1.order.RechargeCoupon;
7
import in.shop2020.model.v1.order.RechargeOrder;
8
import in.shop2020.model.v1.order.RechargeOrderStatus;
9
import in.shop2020.model.v1.order.RechargeType;
10
import in.shop2020.model.v1.order.TransactionServiceException;
6057 anupam.sin 11
import in.shop2020.model.v1.order.UserWallet;
6050 anupam.sin 12
import in.shop2020.model.v1.user.Sex;
13
import in.shop2020.model.v1.user.User;
14
import in.shop2020.model.v1.user.UserContextException;
15
import in.shop2020.serving.interceptors.TrackingInterceptor;
16
import in.shop2020.serving.utils.DesEncrypter;
17
import in.shop2020.serving.utils.Utils;
18
import in.shop2020.thrift.clients.HelperClient;
19
import in.shop2020.thrift.clients.TransactionClient;
20
import in.shop2020.thrift.clients.UserClient;
21
import in.shop2020.utils.HelperServiceException;
6091 anupam.sin 22
import in.shop2020.utils.Mail;
6050 anupam.sin 23
 
24
import java.util.ArrayList;
25
import java.util.List;
6057 anupam.sin 26
import java.util.Map;
6091 anupam.sin 27
import java.util.Random;
6050 anupam.sin 28
 
29
import javax.servlet.http.Cookie;
30
 
31
import org.apache.log4j.Logger;
32
import org.apache.struts2.convention.annotation.Result;
33
import org.apache.struts2.convention.annotation.Results;
34
import org.apache.thrift.TException;
35
import org.apache.thrift.transport.TTransportException;
36
 
37
@SuppressWarnings({ "serial", "serial" })
38
@Results({
6091 anupam.sin 39
    @Result(name="recharge-pay-options-redirect", type="redirectAction", params = {"actionName" , "recharge-pay-options", "rechargeOrderId", "${rechargeOrderId}", "amount", "${amount}", "userId", "${userId}"}),
6103 amit.gupta 40
    @Result(name="create-recharge-redirect", type="redirectAction", params = {"actionName" , "wallet-only-payment", "rechargeOrderId", "${rechargeOrderId}"}),
41
    @Result(name="recharge-redirect", type="redirectAction", params = {"actionName" , "recharge", "error", "ServiceDown"})
6050 anupam.sin 42
})
43
 
44
public class ConfirmController extends BaseController{
45
 
46
    /**
47
     * 
48
     */
49
    private static final long serialVersionUID = 1L;
6057 anupam.sin 50
    private long rechargeOrderId = 0;
6050 anupam.sin 51
    private String amount = "";
6086 anupam.sin 52
    private String walletAmountUsed = "0";
6050 anupam.sin 53
    private String operator = "";
54
    private String number = "";
55
    private String email = "";
56
    private String rechargeType = "";
57
    private DesEncrypter desEncrypter = new DesEncrypter("saholic");
58
    private List<String> couponIds = null;
59
    private List<RechargeCoupon> coupons = null;
6058 anupam.sin 60
    private String userId;
6057 anupam.sin 61
    private String message = "";
6070 anupam.sin 62
    private String totalAmount = "";
6091 anupam.sin 63
    private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
64
    private static final Random random = new Random();
65
    private static final int LENGTH = 10;
6050 anupam.sin 66
    private static Logger log = Logger.getLogger(Class.class);
67
 
68
    public String index() {
69
        return "index";
70
    }
71
 
72
    public String create() {
6103 amit.gupta 73
    	if(getProvider().equals("-1")){
6108 amar.kumar 74
    		DataLogger.logData(EventType.PROVIDER_FETCH_FAILURE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
75
    				rechargeType.toString(), number, operator);
6103 amit.gupta 76
    		return "recharge-redirect";
77
    	}
6086 anupam.sin 78
        setTotalAmount(amount);
6057 anupam.sin 79
        if(userinfo.isLoggedIn()) {
6124 anupam.sin 80
            setUserId("" + userinfo.getUserId());
6057 anupam.sin 81
            try {
82
            TransactionClient tc = new TransactionClient();
83
            UserWallet wallet = tc.getClient().getUserWallet(userinfo.getUserId());
84
            long amountA = 0;
85
            if(wallet.getAmount() == 0){
86
                setMessage("Your RechargeWallet is empty.");
87
            } else if ((amountA = wallet.getAmount() - Long.parseLong(amount)) >= 0) {
88
                setAmount("0");
89
                setMessage("You now have Rs. " + amountA + " left in your RechargeWallet.");
6070 anupam.sin 90
                setWalletAmountUsed("" + (wallet.getAmount() - amountA));
6057 anupam.sin 91
            } else {
92
                setAmount("" + (0-amountA));
93
                setMessage("You have used all the amount in your RechargeWallet.");
6070 anupam.sin 94
                setWalletAmountUsed(("" + wallet.getAmount()));
6057 anupam.sin 95
            }
96
 
97
 
98
            } catch (Exception e) {
99
                log.error("Unable to get user wallet", e);
100
            }
6124 anupam.sin 101
        } else {
102
            long tempUserId = createUserAndSendMail(email);
103
            if(tempUserId == -1)
104
            {
105
                return "recharge-redirect";
106
            } else {
107
                setUserId("" + tempUserId);
108
            }
6057 anupam.sin 109
        }
6050 anupam.sin 110
        return index();
111
    }
112
 
6103 amit.gupta 113
    public String getProvider(){
114
    	TransactionClient tcl;
115
    	try {
116
    		tcl = new TransactionClient();
117
    		return tcl.getClient().getServiceProviderForDevice(RechargeType.findByValue(Integer.parseInt(this.rechargeType)), number) + "";
118
    	} catch (Exception e) {
119
    		log.error("Unable to get service provider for Device number " + number + " and rechargeType : " +  rechargeType, e);
120
    	}
121
    	return 0 + "";
122
 
123
    }
124
 
6070 anupam.sin 125
    private void setTotalAmount(String amount2) {
126
        this.totalAmount  = amount2;
127
 
128
    }
129
 
6050 anupam.sin 130
    public String createRecharge(){
131
        try {
6058 anupam.sin 132
            TransactionClient tc = new TransactionClient();
6050 anupam.sin 133
            RechargeOrder rechargeOrder = new RechargeOrder();
6070 anupam.sin 134
            rechargeOrder.setTotalAmount(Long.parseLong(amount) + Long.parseLong(getWalletAmountUsed()));
6050 anupam.sin 135
            rechargeOrder.setUserEmailId(email);
6058 anupam.sin 136
            rechargeOrder.setUserId(Long.parseLong(userId));
6050 anupam.sin 137
            rechargeOrder.setDeviceNumber(number);
138
            rechargeOrder.setOperatorId(Long.parseLong(operator));
6057 anupam.sin 139
            rechargeOrder.setRechargeType(RechargeType.findByValue(Integer.parseInt(rechargeType)));
6050 anupam.sin 140
            rechargeOrder.setStatus(RechargeOrderStatus.PAYMENT_PENDING);
141
            rechargeOrder.setOrderType(OrderType.B2C);
6070 anupam.sin 142
            rechargeOrder.setWalletAmount(Long.parseLong(getWalletAmountUsed()));
6050 anupam.sin 143
 
144
            rechargeOrder = tc.getClient().createRechargeOrder(rechargeOrder);
6057 anupam.sin 145
            setRechargeOrderId(rechargeOrder.getId());
6050 anupam.sin 146
 
147
        } catch (TransactionServiceException e) {
148
            // TODO Auto-generated catch block
149
            e.printStackTrace();
150
        } catch (TException e) {
151
            // TODO Auto-generated catch block
152
            e.printStackTrace();
153
        }
154
        if(amount.equals("0")) {
155
            return "create-recharge-redirect";
156
        } else {
157
            return "recharge-pay-options-redirect";
158
        }
159
    }
160
 
6091 anupam.sin 161
    private static String generateNewPassword() {
162
        char[] buf = new char[LENGTH];
163
        for (int i = 0; i < buf.length; i++) {
164
            buf[i] = chars.charAt(random.nextInt(chars.length()));
165
        }
166
        return new String(buf);
167
    }
168
 
169
    private long createUserAndSendMail(String email) {
170
        User user = null;
171
        String password = null;
172
        try{
173
        UserClient ucl = new UserClient();
174
        user = ucl.getClient().getUserByEmail(email);
6124 anupam.sin 175
        if(user.getUserId() == -1) {
6091 anupam.sin 176
            user.setEmail(email);
177
            password = generateNewPassword();
178
            String encryptedPassword = desEncrypter.encrypt(password);
179
            user.setPassword(encryptedPassword);
180
            user.setCommunicationEmail(email);
181
            UserClient userContextServiceClient = new UserClient();
182
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
183
            user = userClient.createUser(user);
6124 anupam.sin 184
 
185
            List<String> toList = new ArrayList<String>();
186
            toList.add(email);
187
            HelperClient helperServiceClient = null;
188
                helperServiceClient = new HelperClient();
189
                in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
190
                Mail mail = new Mail();
191
                mail.setSubject("Registration successful");
192
                mail.setTo(toList);
193
                mail.setData("Your have successfully registered with Saholic.com. Your user name is: " + email + " and your password is: " +  password);
194
                client.sendMail(mail);
6091 anupam.sin 195
            }
196
        }catch (UserContextException ux){
197
            return -1;               
198
        } catch (TTransportException e) {
199
            return -1;
200
        } catch (TException e) {
201
            return -1;
6124 anupam.sin 202
        } catch (Exception e) {
203
            log.error("Unexpected error while mailing the new password");
204
            return -1;
6091 anupam.sin 205
        }
206
        return user.getUserId();
207
    }
208
 
6050 anupam.sin 209
    public String temp() {
210
        try {
211
            UserClient ucl = new UserClient();
212
            User user = ucl.getClient().getUserByEmail(email);
213
            if(user == null) {
214
                user = new User();
215
                String password = null;
216
                boolean isValid = true;
217
 
218
                /**
219
                 * TODO : Generate password
220
                 * password = this.request.getParameter("txtPassword");
221
                 * 
222
                 */
223
 
224
 
225
                if(!Utils.isValidEmail(email))  {
226
                    addActionError("Please enter valid email address.");
227
                    isValid = false;
228
                }
229
 
230
                user.setEmail(email);
231
                String encryptedPassword = desEncrypter.encrypt(password);
232
                user.setPassword(encryptedPassword);
233
                user.setCommunicationEmail(email);
234
                Cookie sourceCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_COOKIE);
235
                if (sourceCookie != null) {
236
                    DesEncrypter des = new DesEncrypter(TrackingInterceptor.ENCRIPTION_STRING);
237
                    String sourceCookieVal = des.decrypt(sourceCookie.getValue());
238
                    user.setSource(sourceCookieVal);
239
                }
240
 
241
                Cookie sourceTimeCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_TIME_COOKIE);
242
                long sourceTime = 0;
243
                if (sourceTimeCookie != null) {
244
                    try {
245
                        sourceTime = Long.parseLong(sourceTimeCookie.getValue());
246
                    }
247
                    catch (Exception e) {
248
                        log.warn("Unable to parse session src time cookie.");
249
                    }
250
                    user.setSourceStartTime(sourceTime);
251
                }
252
 
253
                user.setSex(Sex.WONT_SAY);
254
 
255
 
256
                UserClient userContextServiceClient;
257
                userContextServiceClient = new UserClient();
258
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
259
                user = userClient.createUser(user);
260
                HelperClient hc = new HelperClient();
261
                List<String> mailingList = new ArrayList<String>();
262
                mailingList.add(email);
263
                hc.getClient().saveUserEmailForSending(mailingList, "help@saholic.com", "Login Details", "/*body*/", "/*source*/", "/*emailType*/", null, null);
264
            }
6057 anupam.sin 265
            //setUserId("" + user.getUserId());
6050 anupam.sin 266
 
267
 
268
            /*****************************
269
             * 
270
             * TODO : Handle Wallets
271
             * 
272
             */
273
 
274
 
275
            //TransactionClient tc = new TransactionClient();
276
            //setCoupons(tc.getClient().getRechargeCoupons(user.getUserId(), RechargeCouponStatus.ACTIVE));
277
        } catch (TTransportException e) {
278
            // TODO Auto-generated catch block
279
            e.printStackTrace();
280
        } catch (UserContextException e) {
281
            // TODO Auto-generated catch block
282
            e.printStackTrace();
283
        } catch (TException e) {
284
            // TODO Auto-generated catch block
285
            e.printStackTrace();
286
        } catch (HelperServiceException e) {
287
            // TODO Auto-generated catch block
288
            e.printStackTrace();
289
        }
290
        return index();
291
    }
292
 
6057 anupam.sin 293
    public String getOperatorName() {
294
        try {
295
        TransactionClient tc = new TransactionClient();
296
        Map<Long, String> providers = tc.getClient().getServiceProviders(RechargeType.findByValue(Integer.parseInt(rechargeType)));
297
        return providers.get(Long.parseLong(operator));
298
        } catch(Exception e) {
299
        log.error("Unable to get operator name");
300
        }
301
        return "N/A";
302
    }
303
 
6050 anupam.sin 304
    public String getAmount() {
305
        return amount;
306
    }
307
    public void setAmount(String amount) {
308
        this.amount = amount;
309
    }
6073 amit.gupta 310
 
311
    public void setDthamount(String amount) {
312
    	this.amount = amount;
313
    }
6050 anupam.sin 314
    public String getOperator() {
315
        return operator;
316
    }
317
    public void setOperator(String operator) {
318
        this.operator = operator;
319
    }
320
    public String getNumber() {
321
        return number;
322
    }
323
    public void setNumber(String number) {
324
        this.number = number;
325
    }
6073 amit.gupta 326
 
327
    public void setDthnumber(String dthnumber) {
328
    	this.number = dthnumber;
329
    }
330
 
331
 
6050 anupam.sin 332
    public String getEmail() {
333
        return email;
334
    }
335
    public void setEmail(String email) {
336
        this.email = email;
337
    }
338
 
339
    public void setCouponIds(List<String> couponIds) {
340
        this.couponIds = couponIds;
341
    }
342
 
343
    public List<String> getCouponIds() {
344
        return couponIds;
345
    }
346
 
347
    public void setCoupons(List<RechargeCoupon> coupons) {
348
        this.coupons = coupons;
349
    }
350
 
351
    public List<RechargeCoupon> getCoupons() {
352
        return coupons;
353
    }
354
 
355
    public String getRechargeType() {
356
        return rechargeType;
357
    }
358
 
359
    public void setRechargeType(String rechargeType) {
360
        this.rechargeType = rechargeType;
361
    }
362
 
6058 anupam.sin 363
    public String getUserId() {
6050 anupam.sin 364
        return userId;
365
    }
366
 
6058 anupam.sin 367
    public void setUserId(String userId) {
368
        this.userId = userId;
6050 anupam.sin 369
    }
370
 
6057 anupam.sin 371
    public boolean isLoggedIn() {
372
        return userinfo.isLoggedIn();
373
    }
374
 
375
    public void setMessage(String message) {
376
        this.message = message;
377
    }
378
 
379
    public String getMessage() {
380
        return message;
381
    }
382
 
383
    public long getRechargeOrderId() {
384
        return rechargeOrderId;
385
    }
386
 
387
    public void setRechargeOrderId(long l) {
388
        this.rechargeOrderId = l;
389
    }
6070 anupam.sin 390
 
391
    public String getTotalAmount() {
392
        return totalAmount;
393
    }
394
 
395
    public String getWalletAmountUsed() {
396
        return walletAmountUsed;
397
    }
398
 
399
    public void setWalletAmountUsed(String walletAmountUsed) {
400
        this.walletAmountUsed = walletAmountUsed;
401
    }
6057 anupam.sin 402
 
6050 anupam.sin 403
}