Subversion Repositories SmartDukaan

Rev

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