Subversion Repositories SmartDukaan

Rev

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