Subversion Repositories SmartDukaan

Rev

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