Subversion Repositories SmartDukaan

Rev

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