Subversion Repositories SmartDukaan

Rev

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