Subversion Repositories SmartDukaan

Rev

Rev 6058 | Rev 6073 | 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
 
3
import in.shop2020.model.v1.order.OrderType;
4
import in.shop2020.model.v1.order.RechargeCoupon;
5
import in.shop2020.model.v1.order.RechargeOrder;
6
import in.shop2020.model.v1.order.RechargeOrderStatus;
7
import in.shop2020.model.v1.order.RechargeType;
8
import in.shop2020.model.v1.order.TransactionServiceException;
6057 anupam.sin 9
import in.shop2020.model.v1.order.UserWallet;
6050 anupam.sin 10
import in.shop2020.model.v1.user.Sex;
11
import in.shop2020.model.v1.user.User;
12
import in.shop2020.model.v1.user.UserContextException;
13
import in.shop2020.serving.interceptors.TrackingInterceptor;
14
import in.shop2020.serving.utils.DesEncrypter;
15
import in.shop2020.serving.utils.Utils;
16
import in.shop2020.thrift.clients.HelperClient;
17
import in.shop2020.thrift.clients.TransactionClient;
18
import in.shop2020.thrift.clients.UserClient;
19
import in.shop2020.utils.HelperServiceException;
20
 
21
import java.util.ArrayList;
22
import java.util.List;
6057 anupam.sin 23
import java.util.Map;
6050 anupam.sin 24
 
25
import javax.servlet.http.Cookie;
26
 
27
import org.apache.log4j.Logger;
28
import org.apache.struts2.convention.annotation.Result;
29
import org.apache.struts2.convention.annotation.Results;
30
import org.apache.thrift.TException;
31
import org.apache.thrift.transport.TTransportException;
32
 
33
@SuppressWarnings({ "serial", "serial" })
34
@Results({
6057 anupam.sin 35
    @Result(name="recharge-pay-options-redirect", type="redirectAction", params = {"actionName" , "recharge-pay-options", "rechargeOrderId", "${rechargeOrderId}", "amount", "${amount}"}),
36
    @Result(name="create-recharge-redirect", type="redirectAction", params = {"actionName" , "wallet-only-payment", "rechargeOrderId", "${rechargeOrderId}"})
6050 anupam.sin 37
})
38
 
39
public class ConfirmController extends BaseController{
40
 
41
    /**
42
     * 
43
     */
44
    private static final long serialVersionUID = 1L;
6057 anupam.sin 45
    private long rechargeOrderId = 0;
6050 anupam.sin 46
    private String amount = "";
6070 anupam.sin 47
    private String walletAmountUsed = "";
6050 anupam.sin 48
    private String operator = "";
49
    private String number = "";
50
    private String email = "";
51
    private String rechargeType = "";
52
    private DesEncrypter desEncrypter = new DesEncrypter("saholic");
53
    private List<String> couponIds = null;
54
    private List<RechargeCoupon> coupons = null;
6058 anupam.sin 55
    private String userId;
6057 anupam.sin 56
    private String message = "";
6070 anupam.sin 57
    private String totalAmount = "";
6050 anupam.sin 58
    private static Logger log = Logger.getLogger(Class.class);
59
 
60
    public String index() {
61
        return "index";
62
    }
63
 
64
    public String create() {
6057 anupam.sin 65
 
66
        if(userinfo.isLoggedIn()) {
67
            try {
68
            TransactionClient tc = new TransactionClient();
69
            UserWallet wallet = tc.getClient().getUserWallet(userinfo.getUserId());
70
            long amountA = 0;
6070 anupam.sin 71
            setTotalAmount(amount);
6057 anupam.sin 72
            if(wallet.getAmount() == 0){
73
                setMessage("Your RechargeWallet is empty.");
74
            } else if ((amountA = wallet.getAmount() - Long.parseLong(amount)) >= 0) {
75
                setAmount("0");
76
                setMessage("You now have Rs. " + amountA + " left in your RechargeWallet.");
6070 anupam.sin 77
                setWalletAmountUsed("" + (wallet.getAmount() - amountA));
6057 anupam.sin 78
            } else {
79
                setAmount("" + (0-amountA));
80
                setMessage("You have used all the amount in your RechargeWallet.");
6070 anupam.sin 81
                setWalletAmountUsed(("" + wallet.getAmount()));
6057 anupam.sin 82
            }
83
 
84
 
85
            } catch (Exception e) {
86
                log.error("Unable to get user wallet", e);
87
            }
88
        }
6050 anupam.sin 89
        return index();
90
 
91
    }
92
 
6070 anupam.sin 93
    private void setTotalAmount(String amount2) {
94
        this.totalAmount  = amount2;
95
 
96
    }
97
 
6050 anupam.sin 98
    public String createRecharge(){
6057 anupam.sin 99
        if(userinfo.isLoggedIn()) {
6058 anupam.sin 100
            setUserId("" + userinfo.getUserId());
6057 anupam.sin 101
        }
6058 anupam.sin 102
 
6050 anupam.sin 103
        try {
6058 anupam.sin 104
            TransactionClient tc = new TransactionClient();
6050 anupam.sin 105
            RechargeOrder rechargeOrder = new RechargeOrder();
6070 anupam.sin 106
            rechargeOrder.setTotalAmount(Long.parseLong(amount) + Long.parseLong(getWalletAmountUsed()));
6050 anupam.sin 107
            rechargeOrder.setUserEmailId(email);
6058 anupam.sin 108
            rechargeOrder.setUserId(Long.parseLong(userId));
6050 anupam.sin 109
            rechargeOrder.setDeviceNumber(number);
110
            rechargeOrder.setOperatorId(Long.parseLong(operator));
6057 anupam.sin 111
            rechargeOrder.setRechargeType(RechargeType.findByValue(Integer.parseInt(rechargeType)));
6050 anupam.sin 112
            rechargeOrder.setStatus(RechargeOrderStatus.PAYMENT_PENDING);
113
            rechargeOrder.setOrderType(OrderType.B2C);
6070 anupam.sin 114
            rechargeOrder.setWalletAmount(Long.parseLong(getWalletAmountUsed()));
6050 anupam.sin 115
 
116
            rechargeOrder = tc.getClient().createRechargeOrder(rechargeOrder);
6057 anupam.sin 117
            setRechargeOrderId(rechargeOrder.getId());
6050 anupam.sin 118
 
119
        } catch (TransactionServiceException e) {
120
            // TODO Auto-generated catch block
121
            e.printStackTrace();
122
        } catch (TException e) {
123
            // TODO Auto-generated catch block
124
            e.printStackTrace();
125
        }
126
        if(amount.equals("0")) {
127
            return "create-recharge-redirect";
128
        } else {
129
            return "recharge-pay-options-redirect";
130
        }
131
    }
132
 
133
    public String temp() {
134
        try {
135
            UserClient ucl = new UserClient();
136
            User user = ucl.getClient().getUserByEmail(email);
137
            if(user == null) {
138
                user = new User();
139
                String password = null;
140
                boolean isValid = true;
141
 
142
                /**
143
                 * TODO : Generate password
144
                 * password = this.request.getParameter("txtPassword");
145
                 * 
146
                 */
147
 
148
 
149
                if(!Utils.isValidEmail(email))  {
150
                    addActionError("Please enter valid email address.");
151
                    isValid = false;
152
                }
153
 
154
                user.setEmail(email);
155
                String encryptedPassword = desEncrypter.encrypt(password);
156
                user.setPassword(encryptedPassword);
157
                user.setCommunicationEmail(email);
158
                Cookie sourceCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_COOKIE);
159
                if (sourceCookie != null) {
160
                    DesEncrypter des = new DesEncrypter(TrackingInterceptor.ENCRIPTION_STRING);
161
                    String sourceCookieVal = des.decrypt(sourceCookie.getValue());
162
                    user.setSource(sourceCookieVal);
163
                }
164
 
165
                Cookie sourceTimeCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_TIME_COOKIE);
166
                long sourceTime = 0;
167
                if (sourceTimeCookie != null) {
168
                    try {
169
                        sourceTime = Long.parseLong(sourceTimeCookie.getValue());
170
                    }
171
                    catch (Exception e) {
172
                        log.warn("Unable to parse session src time cookie.");
173
                    }
174
                    user.setSourceStartTime(sourceTime);
175
                }
176
 
177
                user.setSex(Sex.WONT_SAY);
178
 
179
 
180
                UserClient userContextServiceClient;
181
                userContextServiceClient = new UserClient();
182
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
183
                user = userClient.createUser(user);
184
                HelperClient hc = new HelperClient();
185
                List<String> mailingList = new ArrayList<String>();
186
                mailingList.add(email);
187
                hc.getClient().saveUserEmailForSending(mailingList, "help@saholic.com", "Login Details", "/*body*/", "/*source*/", "/*emailType*/", null, null);
188
            }
6057 anupam.sin 189
            //setUserId("" + user.getUserId());
6050 anupam.sin 190
 
191
 
192
            /*****************************
193
             * 
194
             * TODO : Handle Wallets
195
             * 
196
             */
197
 
198
 
199
            //TransactionClient tc = new TransactionClient();
200
            //setCoupons(tc.getClient().getRechargeCoupons(user.getUserId(), RechargeCouponStatus.ACTIVE));
201
        } catch (TTransportException e) {
202
            // TODO Auto-generated catch block
203
            e.printStackTrace();
204
        } catch (UserContextException e) {
205
            // TODO Auto-generated catch block
206
            e.printStackTrace();
207
        } catch (TException e) {
208
            // TODO Auto-generated catch block
209
            e.printStackTrace();
210
        } catch (HelperServiceException e) {
211
            // TODO Auto-generated catch block
212
            e.printStackTrace();
213
        }
214
        return index();
215
    }
216
 
6057 anupam.sin 217
    public String getOperatorName() {
218
        try {
219
        TransactionClient tc = new TransactionClient();
220
        Map<Long, String> providers = tc.getClient().getServiceProviders(RechargeType.findByValue(Integer.parseInt(rechargeType)));
221
        return providers.get(Long.parseLong(operator));
222
        } catch(Exception e) {
223
        log.error("Unable to get operator name");
224
        }
225
        return "N/A";
226
    }
227
 
6050 anupam.sin 228
    public String getAmount() {
229
        return amount;
230
    }
231
    public void setAmount(String amount) {
232
        this.amount = amount;
233
    }
234
    public String getOperator() {
235
        return operator;
236
    }
237
    public void setOperator(String operator) {
238
        this.operator = operator;
239
    }
240
    public String getNumber() {
241
        return number;
242
    }
243
    public void setNumber(String number) {
244
        this.number = number;
245
    }
246
    public String getEmail() {
247
        return email;
248
    }
249
    public void setEmail(String email) {
250
        this.email = email;
251
    }
252
 
253
    public void setCouponIds(List<String> couponIds) {
254
        this.couponIds = couponIds;
255
    }
256
 
257
    public List<String> getCouponIds() {
258
        return couponIds;
259
    }
260
 
261
    public void setCoupons(List<RechargeCoupon> coupons) {
262
        this.coupons = coupons;
263
    }
264
 
265
    public List<RechargeCoupon> getCoupons() {
266
        return coupons;
267
    }
268
 
269
    public String getRechargeType() {
270
        return rechargeType;
271
    }
272
 
273
    public void setRechargeType(String rechargeType) {
274
        this.rechargeType = rechargeType;
275
    }
276
 
6058 anupam.sin 277
    public String getUserId() {
6050 anupam.sin 278
        return userId;
279
    }
280
 
6058 anupam.sin 281
    public void setUserId(String userId) {
282
        this.userId = userId;
6050 anupam.sin 283
    }
284
 
6057 anupam.sin 285
    public boolean isLoggedIn() {
286
        return userinfo.isLoggedIn();
287
    }
288
 
289
    public void setMessage(String message) {
290
        this.message = message;
291
    }
292
 
293
    public String getMessage() {
294
        return message;
295
    }
296
 
297
    public long getRechargeOrderId() {
298
        return rechargeOrderId;
299
    }
300
 
301
    public void setRechargeOrderId(long l) {
302
        this.rechargeOrderId = l;
303
    }
6070 anupam.sin 304
 
305
    public String getTotalAmount() {
306
        return totalAmount;
307
    }
308
 
309
    public String getWalletAmountUsed() {
310
        return walletAmountUsed;
311
    }
312
 
313
    public void setWalletAmountUsed(String walletAmountUsed) {
314
        this.walletAmountUsed = walletAmountUsed;
315
    }
6057 anupam.sin 316
 
6050 anupam.sin 317
}