Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
7226 anupam.sin 1
package in.shop2020.serving.controllers;
2
 
7293 anupam.sin 3
import java.text.SimpleDateFormat;
7263 anupam.sin 4
import java.util.ArrayList;
7386 anupam.sin 5
import java.util.Arrays;
7263 anupam.sin 6
import java.util.Calendar;
7293 anupam.sin 7
import java.util.Date;
7263 anupam.sin 8
import java.util.List;
9
import java.util.Random;
10
 
7343 anupam.sin 11
import org.apache.struts2.convention.annotation.Result;
12
import org.apache.struts2.convention.annotation.Results;
7263 anupam.sin 13
import org.apache.thrift.TException;
14
import org.apache.thrift.transport.TTransportException;
15
 
7293 anupam.sin 16
import in.shop2020.logistics.PickUpType;
7263 anupam.sin 17
import in.shop2020.model.v1.catalog.Item;
7323 anupam.sin 18
import in.shop2020.model.v1.catalog.StorePricing;
7386 anupam.sin 19
import in.shop2020.model.v1.order.HotspotStore;
7263 anupam.sin 20
import in.shop2020.model.v1.order.LineItem;
21
import in.shop2020.model.v1.order.Order;
7293 anupam.sin 22
import in.shop2020.model.v1.order.OrderSource;
7263 anupam.sin 23
import in.shop2020.model.v1.order.OrderStatus;
7293 anupam.sin 24
import in.shop2020.model.v1.order.OrderType;
7263 anupam.sin 25
import in.shop2020.model.v1.order.SourceDetail;
7386 anupam.sin 26
import in.shop2020.model.v1.order.StoreOrderDetail;
27
import in.shop2020.model.v1.order.StorePaymentStatus;
7263 anupam.sin 28
import in.shop2020.model.v1.order.Transaction;
29
import in.shop2020.model.v1.order.TransactionStatus;
30
import in.shop2020.model.v1.user.User;
31
import in.shop2020.model.v1.user.UserContextException;
7268 anupam.sin 32
import in.shop2020.payments.PaymentStatus;
7263 anupam.sin 33
import in.shop2020.thrift.clients.CatalogClient;
34
import in.shop2020.thrift.clients.HelperClient;
7268 anupam.sin 35
import in.shop2020.thrift.clients.PaymentClient;
7263 anupam.sin 36
import in.shop2020.thrift.clients.TransactionClient;
37
import in.shop2020.thrift.clients.UserClient;
38
import in.shop2020.utils.Mail;
39
 
40
import in.shop2020.serving.controllers.BaseController;
7293 anupam.sin 41
import in.shop2020.serving.utils.FormattingUtils;
7263 anupam.sin 42
 
7343 anupam.sin 43
@Results({
44
    @Result(name="order-success-redirect", type="redirectAction", params = {"actionName", "order-success", "paymentId", "${paymentId}", "userId", "${userId}"})
45
})
46
 
7226 anupam.sin 47
public class PaymentDetailsController  extends BaseController {
48
 
7248 anupam.sin 49
    /**
50
     * 
51
     */
52
    private static final long serialVersionUID = 1L;
53
    private String name;
54
    private String phone;
7226 anupam.sin 55
    private String line1;
56
    private String line2;
57
    private String city;
58
    private String state;
59
    private String pincode;
7263 anupam.sin 60
    private Long product_id;
61
    private String email;
62
    private static int LENGTH = 10;
63
    private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
64
    private static final Random random = new Random();
65
    private String errorMsg = "";
66
    private double price;
7293 anupam.sin 67
    private List<Order> orders = new ArrayList<Order>();
68
    private Double advanceAmount;
69
    private String deliveryDate = "";
7323 anupam.sin 70
    private String productName = "";
71
    private String bestDealText = "";
72
    private String CatalogItemId = "";
7343 anupam.sin 73
    private long paymentId;
74
    private long userId;
7226 anupam.sin 75
 
7343 anupam.sin 76
 
7323 anupam.sin 77
    private StorePricing sp;
7386 anupam.sin 78
    private double cardAmount;
79
    private double cashAmount;
7293 anupam.sin 80
 
7248 anupam.sin 81
    public String index() {
7386 anupam.sin 82
        String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
83
        if(loginStatus == null || !loginStatus.equals("TRUE")){
84
            return "authfail";
85
        }
86
        storeId = Long.parseLong((String) request.getSession().getAttribute("STORE_ID"));
87
        if(!hotspotStores.containsKey(storeId)){
88
            try{
89
                HotspotStore hotSpotStore = (new TransactionClient()).getClient().getHotspotStore(storeId, "");
90
                hotspotStores.put(storeId, hotSpotStore);
91
            } catch (Exception e) {
92
                log.error("Unable to get store", e);
93
            }
94
        }
95
 
7248 anupam.sin 96
        return INDEX;
97
    }
98
 
7226 anupam.sin 99
    public String create(){
100
        return index();
101
    }
7263 anupam.sin 102
 
103
    public String persistTransaction() {
104
        Transaction txnObj = new Transaction();
7386 anupam.sin 105
        StoreOrderDetail storeOrderDetail = new StoreOrderDetail();
7263 anupam.sin 106
 
107
        if(email != null && !email.isEmpty()) {
7343 anupam.sin 108
            userId = createUserAndSendMail(email);
7263 anupam.sin 109
        } else {
110
            try {
111
                TransactionClient tcl = new TransactionClient();
112
                //Source Id for store website is 2 and our normal website is 1
113
                SourceDetail detail = tcl.getClient().getSourceDetail(2);
114
 
7343 anupam.sin 115
                userId = createUserAndSendMail(detail.getEmail());
7263 anupam.sin 116
            } catch(Exception e) {
117
                log.error("Unable to get default source", e);
118
            }
119
        }
120
 
7343 anupam.sin 121
        if(userId == -1) {
7263 anupam.sin 122
            log.error("Unable to get a user, terminating the transaction");
123
            setErrorMsg("Unable to create a user for this order. Please try again.");
124
            return "error-result";
125
        }
126
 
127
        txnObj.setShoppingCartid(0);
7343 anupam.sin 128
        txnObj.setCustomer_id(userId);
7263 anupam.sin 129
        txnObj.setCreatedOn(Calendar.getInstance().getTimeInMillis());
130
        txnObj.setTransactionStatus(TransactionStatus.INIT);
131
        txnObj.setStatusDescription("New Store Order");
132
        txnObj.setCoupon_code("");
133
        txnObj.setEmiSchemeId(0);
134
 
7343 anupam.sin 135
        Order order = createOrder(userId);
7323 anupam.sin 136
 
137
        if(order ==  null) {
7263 anupam.sin 138
            log.error("Unable to create transaction");
139
            setErrorMsg("Unable to create order. Please try again.");
140
            return "error-result";
141
        }
142
 
7323 anupam.sin 143
        orders.add(order);
7263 anupam.sin 144
        txnObj.setOrders(orders);
7386 anupam.sin 145
 
7263 anupam.sin 146
        TransactionClient tcl;
7268 anupam.sin 147
        long txnId = 0;
7263 anupam.sin 148
        try {
149
            tcl = new TransactionClient();
7268 anupam.sin 150
            txnId = tcl.getClient().createTransaction(txnObj);
7386 anupam.sin 151
 
152
            storeOrderDetail.setAdvanceAmount(advanceAmount);
153
            storeOrderDetail.setCardAmount(cardAmount);
154
            storeOrderDetail.setCashAmount(cashAmount);
155
            storeOrderDetail.setOrderId(txnId);
156
            storeOrderDetail.setStoreId(1);
157
            if(advanceAmount == price) {
158
                storeOrderDetail.setPayStatus(StorePaymentStatus.FULL_PAY_RECEIVED);
159
            } else {
160
                storeOrderDetail.setPayStatus(StorePaymentStatus.ADV_RECEIVED);
161
            }
162
 
163
            boolean saveSuccess = tcl.getClient().saveStoreOrderDetail(storeOrderDetail);
164
            if(!saveSuccess) {
165
                setErrorMsg("Unable to create order. Please try again.");
166
                return "error-result";
167
            }
168
 
7263 anupam.sin 169
        } catch (Exception e) {
170
            log.error("Unable to create transaction", e);
171
            setErrorMsg("Unable to create order. Please try again.");
172
            return "error-result";
173
        }
7268 anupam.sin 174
 
7293 anupam.sin 175
        PaymentClient pcl;
7268 anupam.sin 176
        try {
7293 anupam.sin 177
            pcl = new PaymentClient();
7343 anupam.sin 178
            paymentId = pcl.getClient().createPayment(userId, price, 10, txnId, true);
7293 anupam.sin 179
            //Update payment status as authorized
7343 anupam.sin 180
            pcl.getClient().updatePaymentDetails(paymentId, "", "", "0", "", "", "", "", "", PaymentStatus.SUCCESS, "", null);
181
            txnId = pcl.getClient().getPayment(paymentId).getMerchantTxnId();
7268 anupam.sin 182
        } catch(Exception e) {
183
            log.error("Unable to create payment for this order");
184
            setErrorMsg("Unable to create order. Please try again.");
185
            return "error-result";
186
        }
7293 anupam.sin 187
 
188
        try {
7323 anupam.sin 189
            TransactionStatus txnStatus = TransactionStatus.COD_IN_PROCESS;
190
            if(advanceAmount >= price) {
191
                txnStatus = TransactionStatus.AUTHORIZED;
192
            }
193
 
7293 anupam.sin 194
            tcl = new TransactionClient();
7323 anupam.sin 195
            boolean status = tcl.getClient().changeTransactionStatus(txnId, txnStatus, "Payment authorized", PickUpType.COURIER.getValue(), OrderType.B2C, OrderSource.STORE);
7293 anupam.sin 196
            if(!status) {
197
                log.error("Unable to update transaction");
198
                setErrorMsg("Unable to create order. Please try again.");
199
                return "error-result";
200
            }
201
        } catch (Exception e) {
202
            log.error("Unable to create transaction", e);
203
            setErrorMsg("Unable to create order. Please try again.");
204
            return "error-result";
205
        }
206
 
7343 anupam.sin 207
        return "order-success-redirect";
7263 anupam.sin 208
    }
209
 
210
    public Order createOrder(long userId) {
211
        LineItem lineObj = createLineItem();
212
        if (lineObj == null) {
213
            return null;
214
        }
215
        Order order = new Order();
216
        User user = new User();
217
        try {
218
            UserClient ucl = new UserClient();
219
            user = ucl.getClient().getUserById(userId);
220
        } catch (Exception e) {
221
            log.error("Unable to get user for id " + userId, e);
222
            return null;
223
        }
224
        order.setCustomer_id(user.getUserId());
225
        order.setCustomer_email(user.getEmail());
226
        order.setCustomer_name(name);
227
        order.setCustomer_address1(line1);
228
        order.setCustomer_address2(line2);
229
        order.setCustomer_city(city);
230
        order.setCustomer_state(state);
231
        order.setCustomer_pincode(pincode);
232
        order.setCustomer_mobilenumber(phone);
233
 
234
        order.setTotal_amount(price);
235
        order.setStatus(OrderStatus.SUBMITTED_FOR_PROCESSING);
236
        order.setStatusDescription("In process");
237
        order.setCreated_timestamp(Calendar.getInstance().getTimeInMillis());
7268 anupam.sin 238
        order.setTotal_weight(lineObj.getTotal_weight());
7263 anupam.sin 239
 
7293 anupam.sin 240
        order.setSource(2);
241
        order.setStoreId(1);
7323 anupam.sin 242
 
7293 anupam.sin 243
        order.setAdvanceAmount(advanceAmount);
7323 anupam.sin 244
        order.setFreebieItemId(sp.getFreebieItemId());
7293 anupam.sin 245
 
7268 anupam.sin 246
        List<LineItem> lines = new ArrayList<LineItem>();
247
        lines.add(lineObj);
248
        order.setLineitems(lines);
7263 anupam.sin 249
        return order;
250
    }
251
 
252
    public LineItem createLineItem() {
253
        LineItem lineitem = new LineItem();
254
        Item item = null;
255
        try {
256
            CatalogClient ccl = new CatalogClient();
257
            item = ccl.getClient().getItem(product_id);
7323 anupam.sin 258
            sp = ccl.getClient().getStorePricing(product_id);
259
            lineitem.setDealText(sp.getBestDealText());
7263 anupam.sin 260
        } catch (Exception e) {
261
            log.error("Unable to get the item from catalog service, ItemId : " + product_id, e);
262
            setErrorMsg("Unable to create order. Please try again.");
263
            return null;
264
        }
265
        lineitem.setProductGroup(item.getProductGroup());
266
        lineitem.setBrand(item.getBrand());
267
        lineitem.setModel_number(item.getModelNumber());
268
        lineitem.setModel_name(item.getModelName());
269
        if(item.getColor() == null || item.getColor().isEmpty()) {
270
            lineitem.setColor("");
271
        } else {
272
            lineitem.setColor(item.getColor());
273
        }
274
        lineitem.setExtra_info(item.getFeatureDescription());
275
        lineitem.setItem_id(item.getId());
276
        lineitem.setQuantity(1.0);
277
        lineitem.setUnit_price(price);
278
        lineitem.setTotal_price(price);
279
        lineitem.setUnit_weight(item.getWeight());
280
        lineitem.setTotal_weight(item.getWeight());
7360 anupam.sin 281
        lineitem.setDealText(bestDealText);
7263 anupam.sin 282
 
283
        if(item.getWarrantyPeriod() > 0) {
284
            //Computing Manufacturer Warranty expiry date
285
            Calendar cal = Calendar.getInstance();
286
            cal.add(Calendar.MONTH, item.getWarrantyPeriod());
7268 anupam.sin 287
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
7263 anupam.sin 288
            lineitem.setWarrantry_expiry_timestamp(cal.getTimeInMillis());
289
        }
290
 
291
        return lineitem;
292
    }
293
 
294
    private static String generateNewPassword() {
295
        char[] buf = new char[LENGTH];
296
        for (int i = 0; i < buf.length; i++) {
297
            buf[i] = chars.charAt(random.nextInt(chars.length()));
298
        }
299
        return new String(buf);
300
    }
301
 
302
    private long createUserAndSendMail(String email) {
303
        User user = null;
304
        String password = null;
305
        try{
306
        UserClient ucl = new UserClient();
307
        user = ucl.getClient().getUserByEmail(email);
308
        if(user.getUserId() == -1) {
309
            user.setEmail(email);
310
            password = generateNewPassword();
311
            String encryptedPassword = password;
312
            user.setPassword(encryptedPassword);
313
            user.setCommunicationEmail(email);
314
            UserClient userContextServiceClient = new UserClient();
315
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
316
            user = userClient.createUser(user);
317
 
318
            List<String> toList = new ArrayList<String>();
319
            toList.add(email);
320
            HelperClient helperServiceClient = null;
321
            helperServiceClient = new HelperClient();
322
            in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
323
            Mail mail = new Mail();
324
            mail.setSubject("Saholic Registration successful");
325
            mail.setTo(toList);
326
            mail.setData("Your have successfully registered with Saholic.com. Your user name is: " + email + " and your password is: " +  password);
327
            client.sendMail(mail);
328
            }
329
        }catch (UserContextException ux){
330
            return -1;               
331
        } catch (TTransportException e) {
332
            return -1;
333
        } catch (TException e) {
334
            return -1;
335
        } catch (Exception e) {
336
            log.error("Unexpected error while mailing the new password");
337
            return -1;
338
        }
339
        return user.getUserId();
340
    }
7386 anupam.sin 341
 
342
    public List<String> getAllEdcBanks() {
343
        List<String> banks = new ArrayList<String>(); 
344
        try {
345
            TransactionClient tcl = new TransactionClient();
346
            banks = tcl.getClient().getAllEdcBanks();
347
        } catch (Exception e) {
348
            log.error("Unable to get banks for EDC", e);
349
        }
350
        return banks;
351
    }
7263 anupam.sin 352
 
7226 anupam.sin 353
    public String getLine1() {
354
        return line1;
355
    }
356
 
357
    public void setLine1(String line1) {
358
        this.line1 = line1;
359
    }
360
 
361
    public String getLine2() {
362
        return line2;
363
    }
364
 
365
    public void setLine2(String line2) {
366
        this.line2 = line2;
367
    }
368
 
369
    public String getCity() {
370
        return city;
371
    }
372
 
373
    public void setCity(String city) {
374
        this.city = city;
375
    }
376
 
377
    public String getState() {
378
        return state;
379
    }
380
 
381
    public void setState(String state) {
382
        this.state = state;
383
    }
384
 
385
    public String getPincode() {
386
        return pincode;
387
    }
388
 
389
    public void setPincode(String pincode) {
390
        this.pincode = pincode;
391
    }
392
 
7248 anupam.sin 393
    public String getName() {
394
        return name;
395
    }
396
 
397
    public void setName(String name) {
398
        this.name = name;
399
    }
400
 
401
    public String getPhone() {
402
        return phone;
403
    }
404
 
405
    public void setPhone(String phone) {
406
        this.phone = phone;
407
    }
408
 
7263 anupam.sin 409
    public void setProduct_id(Long product_id) {
410
        this.product_id = product_id;
411
    }
412
 
413
    public Long getProduct_id() {
414
        return product_id;
415
    }
416
 
417
    public String getEmail() {
418
        return email;
419
    }
420
 
421
    public void setEmail(String email) {
422
        this.email = email;
423
    }
424
 
425
    public void setErrorMsg(String errorMsg) {
426
        this.errorMsg = errorMsg;
427
    }
428
 
429
    public String getErrorMsg() {
430
        return errorMsg;
431
    }
432
 
433
    public double getPrice() {
434
        return price;
435
    }
436
 
437
    public void setPrice(double price) {
438
        this.price = price;
439
    }
7293 anupam.sin 440
 
441
    public List<Order> getOrders() {
442
        return orders;
443
    }
444
 
445
    public void setOrders(List<Order> orders) {
446
        this.orders = orders;
447
    }
448
 
449
    public Double getAdvanceAmount() {
450
        return advanceAmount;
451
    }
452
 
453
    public void setAdvanceAmount(Double advanceAmount) {
454
        this.advanceAmount = advanceAmount;
455
    }
456
 
457
    public void setDeliveryDate(String deliveryDate) {
458
        this.deliveryDate = deliveryDate;
459
    }
460
 
461
    public String getDeliveryDate() {
462
        return deliveryDate;
463
    }
464
 
7323 anupam.sin 465
    public String getProductName() {
466
        return productName;
467
    }
468
 
469
    public void setProductName(String productName) {
470
        this.productName = productName;
471
    }
472
 
473
    public String getBestDealText() {
474
        return bestDealText;
475
    }
476
 
477
    public void setBestDealText(String bestDealText) {
478
        this.bestDealText = bestDealText;
479
    }
480
 
481
 
482
    public String getImageSource() {
483
        return "/storewebsite/images/website/" + getCatalogItemId() + "/default.jpg";
484
    }
485
 
486
    public String getCatalogItemId() {
487
        return CatalogItemId;
488
    }
489
 
490
    public void setCatalogItemId(String catalogItemId) {
491
        CatalogItemId = catalogItemId;
492
    }
7343 anupam.sin 493
 
494
    public long getPaymentId() {
495
        return paymentId;
496
    }
497
 
498
    public void setPaymentId(long paymentId) {
499
        this.paymentId = paymentId;
500
    }
501
 
502
    public long getUserId() {
503
        return userId;
504
    }
505
 
506
    public void setUserId(long userId) {
507
        this.userId = userId;
508
    }
7386 anupam.sin 509
 
510
    public double getCashAmount() {
511
        return cashAmount;
512
    }
513
 
514
    public void setCashAmount(double cashAmount) {
515
        this.cashAmount = cashAmount;
516
    }
517
 
518
    public double getCardAmount() {
519
        return cardAmount;
520
    }
521
 
522
    public void setCardAmount(double cardAmount) {
523
        this.cardAmount = cardAmount;
524
    }
7226 anupam.sin 525
}