Subversion Repositories SmartDukaan

Rev

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

Rev 7248 Rev 7263
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
-
 
3
import java.util.ArrayList;
-
 
4
import java.util.Calendar;
-
 
5
import java.util.List;
-
 
6
import java.util.Random;
-
 
7
 
-
 
8
import org.apache.thrift.TException;
-
 
9
import org.apache.thrift.transport.TTransportException;
-
 
10
 
-
 
11
import in.shop2020.model.v1.catalog.Item;
-
 
12
import in.shop2020.model.v1.order.LineItem;
-
 
13
import in.shop2020.model.v1.order.Order;
-
 
14
import in.shop2020.model.v1.order.OrderStatus;
-
 
15
import in.shop2020.model.v1.order.SourceDetail;
-
 
16
import in.shop2020.model.v1.order.Transaction;
-
 
17
import in.shop2020.model.v1.order.TransactionStatus;
-
 
18
import in.shop2020.model.v1.user.User;
-
 
19
import in.shop2020.model.v1.user.UserContextException;
-
 
20
import in.shop2020.thrift.clients.CatalogClient;
-
 
21
import in.shop2020.thrift.clients.HelperClient;
-
 
22
import in.shop2020.thrift.clients.TransactionClient;
-
 
23
import in.shop2020.thrift.clients.UserClient;
-
 
24
import in.shop2020.utils.Mail;
-
 
25
 
-
 
26
import in.shop2020.serving.controllers.BaseController;
-
 
27
 
3
public class PaymentDetailsController  extends BaseController {
28
public class PaymentDetailsController  extends BaseController {
4
    
29
    
5
    /**
30
    /**
6
     * 
31
     * 
7
     */
32
     */
Line 11... Line 36...
11
    private String line1;
36
    private String line1;
12
    private String line2;
37
    private String line2;
13
    private String city;
38
    private String city;
14
    private String state;
39
    private String state;
15
    private String pincode;
40
    private String pincode;
-
 
41
    private Long product_id;
-
 
42
    private String email;
-
 
43
    private static int LENGTH = 10;
-
 
44
    private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-
 
45
    private static final Random random = new Random();
-
 
46
    private String errorMsg = "";
-
 
47
    private double price;
16
    
48
    
17
    public String index() {
49
    public String index() {
18
        return INDEX;
50
        return INDEX;
19
    }
51
    }
20
    
52
    
21
    public String create(){
53
    public String create(){
22
        return index();
54
        return index();
23
    }
55
    }
-
 
56
    
-
 
57
    public String persistTransaction() {
-
 
58
        
-
 
59
        /**
-
 
60
         * 0.Create user
-
 
61
         * 1.Make a transaction object comprised of Order which in turn contains lineitem.
-
 
62
         * 2.Call create_transaction
-
 
63
         * 3.retrieve that order
-
 
64
         */
-
 
65
        
-
 
66
        Transaction txnObj = new Transaction();
-
 
67
        
-
 
68
        long tempUserId = -1;
-
 
69
 
-
 
70
        if(email != null && !email.isEmpty()) {
-
 
71
            tempUserId = createUserAndSendMail(email);
-
 
72
        } else {
-
 
73
            try {
-
 
74
                TransactionClient tcl = new TransactionClient();
-
 
75
                //Source Id for store website is 2 and our normal website is 1
-
 
76
                SourceDetail detail = tcl.getClient().getSourceDetail(2);
-
 
77
                
-
 
78
                tempUserId = createUserAndSendMail(detail.getEmail());
-
 
79
            } catch(Exception e) {
-
 
80
                log.error("Unable to get default source", e);
-
 
81
            }
-
 
82
        }
-
 
83
        
-
 
84
        if(tempUserId == -1) {
-
 
85
            log.error("Unable to get a user, terminating the transaction");
-
 
86
            setErrorMsg("Unable to create a user for this order. Please try again.");
-
 
87
            return "error-result";
-
 
88
        }
-
 
89
        
-
 
90
        txnObj.setShoppingCartid(0);
-
 
91
        txnObj.setCustomer_id(tempUserId);
-
 
92
        txnObj.setCreatedOn(Calendar.getInstance().getTimeInMillis());
-
 
93
        txnObj.setTransactionStatus(TransactionStatus.INIT);
-
 
94
        txnObj.setStatusDescription("New Store Order");
-
 
95
        txnObj.setCoupon_code("");
-
 
96
        txnObj.setEmiSchemeId(0);
-
 
97
        
-
 
98
        List<Order> orders = new ArrayList<Order>();
-
 
99
        if(createOrder(tempUserId) ==  null) {
-
 
100
            log.error("Unable to create transaction");
-
 
101
            setErrorMsg("Unable to create order. Please try again.");
-
 
102
            return "error-result";
-
 
103
        }
-
 
104
        
-
 
105
        orders.add(createOrder(tempUserId));
-
 
106
        txnObj.setOrders(orders);
-
 
107
        TransactionClient tcl;
-
 
108
        try {
-
 
109
            tcl = new TransactionClient();
-
 
110
            long txnId = tcl.getClient().createTransaction(txnObj);
-
 
111
        } catch (Exception e) {
-
 
112
            log.error("Unable to create transaction", e);
-
 
113
            setErrorMsg("Unable to create order. Please try again.");
-
 
114
            return "error-result";
-
 
115
        }
-
 
116
        return "create-order";
-
 
117
    }
-
 
118
    
-
 
119
    public Order createOrder(long userId) {
-
 
120
        LineItem lineObj = createLineItem();
-
 
121
        if (lineObj == null) {
-
 
122
            return null;
-
 
123
        }
-
 
124
        Order order = new Order();
-
 
125
        User user = new User();
-
 
126
        try {
-
 
127
            UserClient ucl = new UserClient();
-
 
128
            user = ucl.getClient().getUserById(userId);
-
 
129
        } catch (Exception e) {
-
 
130
            log.error("Unable to get user for id " + userId, e);
-
 
131
            return null;
-
 
132
        }
-
 
133
        order.setCustomer_id(user.getUserId());
-
 
134
        order.setCustomer_email(user.getEmail());
-
 
135
        order.setCustomer_name(name);
-
 
136
        order.setCustomer_address1(line1);
-
 
137
        order.setCustomer_address2(line2);
-
 
138
        order.setCustomer_city(city);
-
 
139
        order.setCustomer_state(state);
-
 
140
        order.setCustomer_pincode(pincode);
-
 
141
        order.setCustomer_mobilenumber(phone);
-
 
142
        
-
 
143
        order.setTotal_amount(price);
-
 
144
        order.setStatus(OrderStatus.SUBMITTED_FOR_PROCESSING);
-
 
145
        order.setStatusDescription("In process");
-
 
146
        order.setCreated_timestamp(Calendar.getInstance().getTimeInMillis());
-
 
147
        
-
 
148
//        t_order.total_weight = t_line_item.total_weight
-
 
149
//        t_order.lineitems = [t_line_item]
-
 
150
        return order;
-
 
151
    }
-
 
152
        
-
 
153
    public LineItem createLineItem() {
-
 
154
        LineItem lineitem = new LineItem();
-
 
155
        Item item = null;
-
 
156
        try {
-
 
157
            CatalogClient ccl = new CatalogClient();
-
 
158
            item = ccl.getClient().getItem(product_id);
-
 
159
        } catch (Exception e) {
-
 
160
            log.error("Unable to get the item from catalog service, ItemId : " + product_id, e);
-
 
161
            setErrorMsg("Unable to create order. Please try again.");
-
 
162
            return null;
-
 
163
        }
-
 
164
        lineitem.setProductGroup(item.getProductGroup());
-
 
165
        lineitem.setBrand(item.getBrand());
-
 
166
        lineitem.setModel_number(item.getModelNumber());
-
 
167
        lineitem.setModel_name(item.getModelName());
-
 
168
        if(item.getColor() == null || item.getColor().isEmpty()) {
-
 
169
            lineitem.setColor("");
-
 
170
        } else {
-
 
171
            lineitem.setColor(item.getColor());
-
 
172
        }
-
 
173
        lineitem.setExtra_info(item.getFeatureDescription());
-
 
174
        lineitem.setItem_id(item.getId());
-
 
175
        lineitem.setQuantity(1.0);
-
 
176
        lineitem.setUnit_price(price);
-
 
177
        lineitem.setTotal_price(price);
-
 
178
        lineitem.setUnit_weight(item.getWeight());
-
 
179
        lineitem.setTotal_weight(item.getWeight());
-
 
180
        lineitem.setDealText(item.getBestDealText());
-
 
181
        
-
 
182
        if(item.getWarrantyPeriod() > 0) {
-
 
183
            //Computing Manufacturer Warranty expiry date
-
 
184
            Calendar cal = Calendar.getInstance();
-
 
185
            cal.add(Calendar.MONTH, item.getWarrantyPeriod());
-
 
186
            cal.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, 23, 59, 59);
-
 
187
            lineitem.setWarrantry_expiry_timestamp(cal.getTimeInMillis());
-
 
188
        }
-
 
189
        
-
 
190
        return lineitem;
-
 
191
    }
-
 
192
 
-
 
193
//        if item.warrantyPeriod:
-
 
194
//            #Computing Manufacturer Warranty expiry date
-
 
195
//            today = datetime.date.today()
-
 
196
//            expiry_year = today.year + int((today.month + item.warrantyPeriod) / 12)
-
 
197
//            expiry_month = (today.month + item.warrantyPeriod) % 12
-
 
198
//            
-
 
199
//            try:
-
 
200
//                expiry_date = datetime.datetime(expiry_year, expiry_month, today.day, 23, 59, 59, 999999)
-
 
201
//            except ValueError:
-
 
202
//                try:
-
 
203
//                    expiry_date = datetime.date(expiry_year, expiry_month, (today.day - 1), 23, 59, 59, 999999)
-
 
204
//                except ValueError:
-
 
205
//                    try:
-
 
206
//                        expiry_date = datetime.date(expiry_year, expiry_month, (today.day - 2), 23, 59, 59, 999999)
-
 
207
//                    except ValueError:
-
 
208
//                        expiry_date = datetime.date(expiry_year, expiry_month, (today.day - 3), 23, 59, 59, 999999)
-
 
209
//            
-
 
210
//            t_line_item.warrantry_expiry_timestamp = to_java_date(expiry_date)
-
 
211
//    
-
 
212
    private static String generateNewPassword() {
-
 
213
        char[] buf = new char[LENGTH];
-
 
214
        for (int i = 0; i < buf.length; i++) {
-
 
215
            buf[i] = chars.charAt(random.nextInt(chars.length()));
-
 
216
        }
-
 
217
        return new String(buf);
-
 
218
    }
-
 
219
    
-
 
220
    private long createUserAndSendMail(String email) {
-
 
221
        User user = null;
-
 
222
        String password = null;
-
 
223
        try{
-
 
224
        UserClient ucl = new UserClient();
-
 
225
        user = ucl.getClient().getUserByEmail(email);
-
 
226
        if(user.getUserId() == -1) {
-
 
227
            user.setEmail(email);
-
 
228
            password = generateNewPassword();
-
 
229
            String encryptedPassword = password;
-
 
230
            user.setPassword(encryptedPassword);
-
 
231
            user.setCommunicationEmail(email);
-
 
232
            UserClient userContextServiceClient = new UserClient();
-
 
233
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
-
 
234
            user = userClient.createUser(user);
-
 
235
            
-
 
236
            List<String> toList = new ArrayList<String>();
-
 
237
            toList.add(email);
-
 
238
            HelperClient helperServiceClient = null;
-
 
239
            helperServiceClient = new HelperClient();
-
 
240
            in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
-
 
241
            Mail mail = new Mail();
-
 
242
            mail.setSubject("Saholic Registration successful");
-
 
243
            mail.setTo(toList);
-
 
244
            mail.setData("Your have successfully registered with Saholic.com. Your user name is: " + email + " and your password is: " +  password);
-
 
245
            client.sendMail(mail);
-
 
246
            }
-
 
247
        }catch (UserContextException ux){
-
 
248
            return -1;               
-
 
249
        } catch (TTransportException e) {
-
 
250
            return -1;
-
 
251
        } catch (TException e) {
-
 
252
            return -1;
-
 
253
        } catch (Exception e) {
-
 
254
            log.error("Unexpected error while mailing the new password");
-
 
255
            return -1;
-
 
256
        }
-
 
257
        return user.getUserId();
-
 
258
    }
24
 
259
 
25
    public String getLine1() {
260
    public String getLine1() {
26
        return line1;
261
        return line1;
27
    }
262
    }
28
 
263
 
Line 76... Line 311...
76
 
311
 
77
    public void setPhone(String phone) {
312
    public void setPhone(String phone) {
78
        this.phone = phone;
313
        this.phone = phone;
79
    }
314
    }
80
 
315
 
-
 
316
    public void setProduct_id(Long product_id) {
-
 
317
        this.product_id = product_id;
-
 
318
    }
-
 
319
 
-
 
320
    public Long getProduct_id() {
-
 
321
        return product_id;
-
 
322
    }
-
 
323
 
-
 
324
    public String getEmail() {
-
 
325
        return email;
-
 
326
    }
-
 
327
 
-
 
328
    public void setEmail(String email) {
-
 
329
        this.email = email;
-
 
330
    }
-
 
331
 
-
 
332
    public void setErrorMsg(String errorMsg) {
-
 
333
        this.errorMsg = errorMsg;
-
 
334
    }
-
 
335
 
-
 
336
    public String getErrorMsg() {
-
 
337
        return errorMsg;
-
 
338
    }
-
 
339
 
-
 
340
    public double getPrice() {
-
 
341
        return price;
-
 
342
    }
-
 
343
 
-
 
344
    public void setPrice(double price) {
-
 
345
        this.price = price;
-
 
346
    }
-
 
347
    
-
 
348
    public static void main(String[] args) {
-
 
349
        Calendar cal = Calendar.getInstance();
-
 
350
        cal.add(Calendar.MONTH, 12);
-
 
351
        cal.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, 23, 59, 59);
-
 
352
        System.out.println(cal.getTimeInMillis());
-
 
353
    }
-
 
354
 
81
}
355
}