Subversion Repositories SmartDukaan

Rev

Rev 6131 | Rev 6206 | 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 = "";
6178 anupam.sin 63
    private String plan = "";
6091 anupam.sin 64
    private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
65
    private static final Random random = new Random();
66
    private static final int LENGTH = 10;
6050 anupam.sin 67
    private static Logger log = Logger.getLogger(Class.class);
68
 
69
    public String index() {
70
        return "index";
71
    }
72
 
73
    public String create() {
6103 amit.gupta 74
    	if(getProvider().equals("-1")){
6108 amar.kumar 75
    		DataLogger.logData(EventType.PROVIDER_FETCH_FAILURE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
76
    				rechargeType.toString(), number, operator);
6103 amit.gupta 77
    		return "recharge-redirect";
78
    	}
6086 anupam.sin 79
        setTotalAmount(amount);
6057 anupam.sin 80
        if(userinfo.isLoggedIn()) {
6124 anupam.sin 81
            setUserId("" + userinfo.getUserId());
6057 anupam.sin 82
            try {
83
            TransactionClient tc = new TransactionClient();
84
            UserWallet wallet = tc.getClient().getUserWallet(userinfo.getUserId());
85
            long amountA = 0;
86
            if(wallet.getAmount() == 0){
87
                setMessage("Your RechargeWallet is empty.");
88
            } else if ((amountA = wallet.getAmount() - Long.parseLong(amount)) >= 0) {
89
                setAmount("0");
90
                setMessage("You now have Rs. " + amountA + " left in your RechargeWallet.");
6070 anupam.sin 91
                setWalletAmountUsed("" + (wallet.getAmount() - amountA));
6057 anupam.sin 92
            } else {
93
                setAmount("" + (0-amountA));
94
                setMessage("You have used all the amount in your RechargeWallet.");
6070 anupam.sin 95
                setWalletAmountUsed(("" + wallet.getAmount()));
6057 anupam.sin 96
            }
97
 
98
 
99
            } catch (Exception e) {
100
                log.error("Unable to get user wallet", e);
101
            }
6124 anupam.sin 102
        } else {
103
            long tempUserId = createUserAndSendMail(email);
104
            if(tempUserId == -1)
105
            {
106
                return "recharge-redirect";
107
            } else {
108
                setUserId("" + tempUserId);
109
            }
6057 anupam.sin 110
        }
6050 anupam.sin 111
        return index();
112
    }
113
 
6103 amit.gupta 114
    public String getProvider(){
115
    	TransactionClient tcl;
116
    	try {
117
    		tcl = new TransactionClient();
118
    		return tcl.getClient().getServiceProviderForDevice(RechargeType.findByValue(Integer.parseInt(this.rechargeType)), number) + "";
119
    	} catch (Exception e) {
120
    		log.error("Unable to get service provider for Device number " + number + " and rechargeType : " +  rechargeType, e);
121
    	}
122
    	return 0 + "";
123
 
124
    }
125
 
6070 anupam.sin 126
    private void setTotalAmount(String amount2) {
127
        this.totalAmount  = amount2;
128
 
129
    }
130
 
6050 anupam.sin 131
    public String createRecharge(){
132
        try {
6058 anupam.sin 133
            TransactionClient tc = new TransactionClient();
6050 anupam.sin 134
            RechargeOrder rechargeOrder = new RechargeOrder();
6070 anupam.sin 135
            rechargeOrder.setTotalAmount(Long.parseLong(amount) + Long.parseLong(getWalletAmountUsed()));
6050 anupam.sin 136
            rechargeOrder.setUserEmailId(email);
6058 anupam.sin 137
            rechargeOrder.setUserId(Long.parseLong(userId));
6050 anupam.sin 138
            rechargeOrder.setDeviceNumber(number);
6178 anupam.sin 139
            rechargeOrder.setPlan(plan);
6050 anupam.sin 140
            rechargeOrder.setOperatorId(Long.parseLong(operator));
6057 anupam.sin 141
            rechargeOrder.setRechargeType(RechargeType.findByValue(Integer.parseInt(rechargeType)));
6050 anupam.sin 142
            rechargeOrder.setStatus(RechargeOrderStatus.PAYMENT_PENDING);
143
            rechargeOrder.setOrderType(OrderType.B2C);
6070 anupam.sin 144
            rechargeOrder.setWalletAmount(Long.parseLong(getWalletAmountUsed()));
6050 anupam.sin 145
 
146
            rechargeOrder = tc.getClient().createRechargeOrder(rechargeOrder);
6057 anupam.sin 147
            setRechargeOrderId(rechargeOrder.getId());
6050 anupam.sin 148
 
6178 anupam.sin 149
        } catch (Exception e) {
150
            log.error("Unable to create recharge order", e);
151
            return "recharge-redirect";
6050 anupam.sin 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);
6124 anupam.sin 174
        if(user.getUserId() == -1) {
6091 anupam.sin 175
            user.setEmail(email);
176
            password = generateNewPassword();
177
            String encryptedPassword = desEncrypter.encrypt(password);
178
            user.setPassword(encryptedPassword);
179
            user.setCommunicationEmail(email);
180
            UserClient userContextServiceClient = new UserClient();
181
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
182
            user = userClient.createUser(user);
6124 anupam.sin 183
 
184
            List<String> toList = new ArrayList<String>();
185
            toList.add(email);
186
            HelperClient helperServiceClient = null;
187
                helperServiceClient = new HelperClient();
188
                in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
189
                Mail mail = new Mail();
6131 anupam.sin 190
                mail.setSubject("Saholic Registration successful");
6124 anupam.sin 191
                mail.setTo(toList);
192
                mail.setData("Your have successfully registered with Saholic.com. Your user name is: " + email + " and your password is: " +  password);
193
                client.sendMail(mail);
6091 anupam.sin 194
            }
195
        }catch (UserContextException ux){
196
            return -1;               
197
        } catch (TTransportException e) {
198
            return -1;
199
        } catch (TException e) {
200
            return -1;
6124 anupam.sin 201
        } catch (Exception e) {
202
            log.error("Unexpected error while mailing the new password");
203
            return -1;
6091 anupam.sin 204
        }
205
        return user.getUserId();
206
    }
207
 
6057 anupam.sin 208
    public String getOperatorName() {
209
        try {
210
        TransactionClient tc = new TransactionClient();
211
        Map<Long, String> providers = tc.getClient().getServiceProviders(RechargeType.findByValue(Integer.parseInt(rechargeType)));
212
        return providers.get(Long.parseLong(operator));
213
        } catch(Exception e) {
214
        log.error("Unable to get operator name");
215
        }
216
        return "N/A";
217
    }
218
 
6050 anupam.sin 219
    public String getAmount() {
220
        return amount;
221
    }
222
    public void setAmount(String amount) {
223
        this.amount = amount;
224
    }
6073 amit.gupta 225
 
226
    public void setDthamount(String amount) {
227
    	this.amount = amount;
228
    }
6050 anupam.sin 229
    public String getOperator() {
230
        return operator;
231
    }
232
    public void setOperator(String operator) {
233
        this.operator = operator;
234
    }
235
    public String getNumber() {
236
        return number;
237
    }
238
    public void setNumber(String number) {
239
        this.number = number;
240
    }
6073 amit.gupta 241
 
242
    public void setDthnumber(String dthnumber) {
243
    	this.number = dthnumber;
244
    }
245
 
246
 
6050 anupam.sin 247
    public String getEmail() {
248
        return email;
249
    }
250
    public void setEmail(String email) {
251
        this.email = email;
252
    }
253
 
254
    public void setCouponIds(List<String> couponIds) {
255
        this.couponIds = couponIds;
256
    }
257
 
258
    public List<String> getCouponIds() {
259
        return couponIds;
260
    }
261
 
262
    public void setCoupons(List<RechargeCoupon> coupons) {
263
        this.coupons = coupons;
264
    }
265
 
266
    public List<RechargeCoupon> getCoupons() {
267
        return coupons;
268
    }
269
 
270
    public String getRechargeType() {
271
        return rechargeType;
272
    }
273
 
274
    public void setRechargeType(String rechargeType) {
275
        this.rechargeType = rechargeType;
276
    }
277
 
6058 anupam.sin 278
    public String getUserId() {
6050 anupam.sin 279
        return userId;
280
    }
281
 
6058 anupam.sin 282
    public void setUserId(String userId) {
283
        this.userId = userId;
6050 anupam.sin 284
    }
285
 
6057 anupam.sin 286
    public boolean isLoggedIn() {
287
        return userinfo.isLoggedIn();
288
    }
289
 
290
    public void setMessage(String message) {
291
        this.message = message;
292
    }
293
 
294
    public String getMessage() {
295
        return message;
296
    }
297
 
298
    public long getRechargeOrderId() {
299
        return rechargeOrderId;
300
    }
301
 
302
    public void setRechargeOrderId(long l) {
303
        this.rechargeOrderId = l;
304
    }
6070 anupam.sin 305
 
306
    public String getTotalAmount() {
307
        return totalAmount;
308
    }
309
 
310
    public String getWalletAmountUsed() {
311
        return walletAmountUsed;
312
    }
313
 
314
    public void setWalletAmountUsed(String walletAmountUsed) {
315
        this.walletAmountUsed = walletAmountUsed;
316
    }
6178 anupam.sin 317
 
318
    public String getPlan() {
319
        return plan;
320
    }
321
 
322
    public void setPlan(String plan) {
323
        this.plan = plan;
324
    }
6057 anupam.sin 325
 
6050 anupam.sin 326
}