Subversion Repositories SmartDukaan

Rev

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