Subversion Repositories SmartDukaan

Rev

Rev 7268 | Rev 7323 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7268 Rev 7293
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
-
 
3
import java.text.SimpleDateFormat;
3
import java.util.ArrayList;
4
import java.util.ArrayList;
4
import java.util.Calendar;
5
import java.util.Calendar;
-
 
6
import java.util.Date;
5
import java.util.List;
7
import java.util.List;
6
import java.util.Random;
8
import java.util.Random;
7
 
9
 
8
import org.apache.thrift.TException;
10
import org.apache.thrift.TException;
9
import org.apache.thrift.transport.TTransportException;
11
import org.apache.thrift.transport.TTransportException;
10
 
12
 
-
 
13
import in.shop2020.logistics.PickUpType;
11
import in.shop2020.model.v1.catalog.Item;
14
import in.shop2020.model.v1.catalog.Item;
12
import in.shop2020.model.v1.order.LineItem;
15
import in.shop2020.model.v1.order.LineItem;
13
import in.shop2020.model.v1.order.Order;
16
import in.shop2020.model.v1.order.Order;
-
 
17
import in.shop2020.model.v1.order.OrderSource;
14
import in.shop2020.model.v1.order.OrderStatus;
18
import in.shop2020.model.v1.order.OrderStatus;
-
 
19
import in.shop2020.model.v1.order.OrderType;
15
import in.shop2020.model.v1.order.SourceDetail;
20
import in.shop2020.model.v1.order.SourceDetail;
16
import in.shop2020.model.v1.order.Transaction;
21
import in.shop2020.model.v1.order.Transaction;
17
import in.shop2020.model.v1.order.TransactionStatus;
22
import in.shop2020.model.v1.order.TransactionStatus;
18
import in.shop2020.model.v1.user.User;
23
import in.shop2020.model.v1.user.User;
19
import in.shop2020.model.v1.user.UserContextException;
24
import in.shop2020.model.v1.user.UserContextException;
Line 24... Line 29...
24
import in.shop2020.thrift.clients.TransactionClient;
29
import in.shop2020.thrift.clients.TransactionClient;
25
import in.shop2020.thrift.clients.UserClient;
30
import in.shop2020.thrift.clients.UserClient;
26
import in.shop2020.utils.Mail;
31
import in.shop2020.utils.Mail;
27
 
32
 
28
import in.shop2020.serving.controllers.BaseController;
33
import in.shop2020.serving.controllers.BaseController;
-
 
34
import in.shop2020.serving.utils.FormattingUtils;
29
 
35
 
30
public class PaymentDetailsController  extends BaseController {
36
public class PaymentDetailsController  extends BaseController {
31
    
37
    
32
    /**
38
    /**
33
     * 
39
     * 
Line 45... Line 51...
45
    private static int LENGTH = 10;
51
    private static int LENGTH = 10;
46
    private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
52
    private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
47
    private static final Random random = new Random();
53
    private static final Random random = new Random();
48
    private String errorMsg = "";
54
    private String errorMsg = "";
49
    private double price;
55
    private double price;
-
 
56
    private List<Order> orders = new ArrayList<Order>();
-
 
57
    private Double advanceAmount;
-
 
58
    private String deliveryDate = "";
-
 
59
    
-
 
60
    private FormattingUtils formattingUtils = new FormattingUtils();
50
    
61
    
51
    public String index() {
62
    public String index() {
52
        return INDEX;
63
        return INDEX;
53
    }
64
    }
54
    
65
    
Line 66... Line 77...
66
         */
77
         */
67
        
78
        
68
        Transaction txnObj = new Transaction();
79
        Transaction txnObj = new Transaction();
69
        
80
        
70
        long tempUserId = -1;
81
        long tempUserId = -1;
-
 
82
        Long merchantPaymentId = 0l;
71
 
83
 
72
        if(email != null && !email.isEmpty()) {
84
        if(email != null && !email.isEmpty()) {
73
            tempUserId = createUserAndSendMail(email);
85
            tempUserId = createUserAndSendMail(email);
74
        } else {
86
        } else {
75
            try {
87
            try {
Line 95... Line 107...
95
        txnObj.setTransactionStatus(TransactionStatus.INIT);
107
        txnObj.setTransactionStatus(TransactionStatus.INIT);
96
        txnObj.setStatusDescription("New Store Order");
108
        txnObj.setStatusDescription("New Store Order");
97
        txnObj.setCoupon_code("");
109
        txnObj.setCoupon_code("");
98
        txnObj.setEmiSchemeId(0);
110
        txnObj.setEmiSchemeId(0);
99
        
111
        
100
        List<Order> orders = new ArrayList<Order>();
-
 
101
        if(createOrder(tempUserId) ==  null) {
112
        if(createOrder(tempUserId) ==  null) {
102
            log.error("Unable to create transaction");
113
            log.error("Unable to create transaction");
103
            setErrorMsg("Unable to create order. Please try again.");
114
            setErrorMsg("Unable to create order. Please try again.");
104
            return "error-result";
115
            return "error-result";
105
        }
116
        }
Line 115... Line 126...
115
            log.error("Unable to create transaction", e);
126
            log.error("Unable to create transaction", e);
116
            setErrorMsg("Unable to create order. Please try again.");
127
            setErrorMsg("Unable to create order. Please try again.");
117
            return "error-result";
128
            return "error-result";
118
        }
129
        }
119
        
130
        
-
 
131
        PaymentClient pcl;
120
        try {
132
        try {
121
        PaymentClient pcl = new PaymentClient();
133
            pcl = new PaymentClient();
122
        Long merchantPaymentId = pcl.getClient().createPayment(tempUserId, price, 10, txnId, true);
134
            merchantPaymentId = pcl.getClient().createPayment(tempUserId, price, 10, txnId, true);
123
        //Update payment status as authorized
135
            //Update payment status as authorized
124
        pcl.getClient().updatePaymentDetails(merchantPaymentId, "", "", "0", "", "", "", "", "", PaymentStatus.SUCCESS, "", null);
136
            pcl.getClient().updatePaymentDetails(merchantPaymentId, "", "", "0", "", "", "", "", "", PaymentStatus.SUCCESS, "", null);
-
 
137
            txnId = pcl.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
125
        } catch(Exception e) {
138
        } catch(Exception e) {
126
            log.error("Unable to create payment for this order");
139
            log.error("Unable to create payment for this order");
127
            setErrorMsg("Unable to create order. Please try again.");
140
            setErrorMsg("Unable to create order. Please try again.");
128
            return "error-result";
141
            return "error-result";
129
        }
142
        }
-
 
143
        
-
 
144
        try {
-
 
145
            tcl = new TransactionClient();
-
 
146
            boolean status = tcl.getClient().changeTransactionStatus(txnId, TransactionStatus.COD_IN_PROCESS, "Payment authorized", PickUpType.COURIER.getValue(), OrderType.B2C, OrderSource.STORE);
-
 
147
            if(!status) {
-
 
148
                log.error("Unable to update transaction");
-
 
149
                setErrorMsg("Unable to create order. Please try again.");
-
 
150
                return "error-result";
-
 
151
            }
-
 
152
        } catch (Exception e) {
-
 
153
            log.error("Unable to create transaction", e);
-
 
154
            setErrorMsg("Unable to create order. Please try again.");
-
 
155
            return "error-result";
-
 
156
        }
-
 
157
        
-
 
158
        
-
 
159
        try {
-
 
160
            pcl = new PaymentClient();
-
 
161
            
-
 
162
            orders = tcl.getClient().getOrdersForTransaction(txnId, tempUserId);
-
 
163
        } catch (Exception e) {
-
 
164
            log.error("Unable to retrieve orders");
-
 
165
            setErrorMsg("Order was created but due to some error we could not display it. Please try again.");
-
 
166
        }
-
 
167
        
130
        return "create-order";
168
        return "order-success";
131
    }
169
    }
132
    
170
    
133
    public Order createOrder(long userId) {
171
    public Order createOrder(long userId) {
134
        LineItem lineObj = createLineItem();
172
        LineItem lineObj = createLineItem();
135
        if (lineObj == null) {
173
        if (lineObj == null) {
Line 158... Line 196...
158
        order.setStatus(OrderStatus.SUBMITTED_FOR_PROCESSING);
196
        order.setStatus(OrderStatus.SUBMITTED_FOR_PROCESSING);
159
        order.setStatusDescription("In process");
197
        order.setStatusDescription("In process");
160
        order.setCreated_timestamp(Calendar.getInstance().getTimeInMillis());
198
        order.setCreated_timestamp(Calendar.getInstance().getTimeInMillis());
161
        order.setTotal_weight(lineObj.getTotal_weight());
199
        order.setTotal_weight(lineObj.getTotal_weight());
162
        
200
        
-
 
201
        order.setSource(2);
-
 
202
        order.setStoreId(1);
-
 
203
        order.setAdvanceAmount(advanceAmount);
-
 
204
        
163
        List<LineItem> lines = new ArrayList<LineItem>();
205
        List<LineItem> lines = new ArrayList<LineItem>();
164
        lines.add(lineObj);
206
        lines.add(lineObj);
165
        order.setLineitems(lines);
207
        order.setLineitems(lines);
166
        return order;
208
        return order;
167
    }
209
    }
Line 339... Line 381...
339
    }
381
    }
340
 
382
 
341
    public void setPrice(double price) {
383
    public void setPrice(double price) {
342
        this.price = price;
384
        this.price = price;
343
    }
385
    }
-
 
386
 
-
 
387
    public List<Order> getOrders() {
-
 
388
        return orders;
-
 
389
    }
-
 
390
 
-
 
391
    public void setOrders(List<Order> orders) {
-
 
392
        this.orders = orders;
-
 
393
    }
-
 
394
 
-
 
395
    public Double getAdvanceAmount() {
-
 
396
        return advanceAmount;
-
 
397
    }
-
 
398
 
-
 
399
    public void setAdvanceAmount(Double advanceAmount) {
-
 
400
        this.advanceAmount = advanceAmount;
-
 
401
    }
-
 
402
 
-
 
403
    public void setDeliveryDate(String deliveryDate) {
-
 
404
        this.deliveryDate = deliveryDate;
-
 
405
    }
-
 
406
 
-
 
407
    public String getDeliveryDate() {
-
 
408
        return deliveryDate;
-
 
409
    }
-
 
410
    
-
 
411
    public String formatPrice(double price)    {
-
 
412
        return formattingUtils.formatPrice(price);
-
 
413
    }
-
 
414
    
-
 
415
    public static String formatDate(long timestamp){
-
 
416
        SimpleDateFormat dateformat = new SimpleDateFormat("h:m a, dd MMMM yyyy");
-
 
417
        return dateformat.format(new Date(timestamp));  
-
 
418
    }
344
}
419
}