Subversion Repositories SmartDukaan

Rev

Rev 7263 | Rev 7293 | 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
 
7263 anupam.sin 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;
7268 anupam.sin 20
import in.shop2020.payments.PaymentStatus;
7263 anupam.sin 21
import in.shop2020.thrift.clients.CatalogClient;
22
import in.shop2020.thrift.clients.HelperClient;
7268 anupam.sin 23
import in.shop2020.thrift.clients.PaymentClient;
7263 anupam.sin 24
import in.shop2020.thrift.clients.TransactionClient;
25
import in.shop2020.thrift.clients.UserClient;
26
import in.shop2020.utils.Mail;
27
 
28
import in.shop2020.serving.controllers.BaseController;
29
 
7226 anupam.sin 30
public class PaymentDetailsController  extends BaseController {
31
 
7248 anupam.sin 32
    /**
33
     * 
34
     */
35
    private static final long serialVersionUID = 1L;
36
    private String name;
37
    private String phone;
7226 anupam.sin 38
    private String line1;
39
    private String line2;
40
    private String city;
41
    private String state;
42
    private String pincode;
7263 anupam.sin 43
    private Long product_id;
44
    private String email;
45
    private static int LENGTH = 10;
46
    private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
47
    private static final Random random = new Random();
48
    private String errorMsg = "";
49
    private double price;
7226 anupam.sin 50
 
7248 anupam.sin 51
    public String index() {
52
        return INDEX;
53
    }
54
 
7226 anupam.sin 55
    public String create(){
56
        return index();
57
    }
7263 anupam.sin 58
 
59
    public String persistTransaction() {
60
 
61
        /**
62
         * 0.Create user
63
         * 1.Make a transaction object comprised of Order which in turn contains lineitem.
64
         * 2.Call create_transaction
65
         * 3.retrieve that order
66
         */
67
 
68
        Transaction txnObj = new Transaction();
69
 
70
        long tempUserId = -1;
7226 anupam.sin 71
 
7263 anupam.sin 72
        if(email != null && !email.isEmpty()) {
73
            tempUserId = createUserAndSendMail(email);
74
        } else {
75
            try {
76
                TransactionClient tcl = new TransactionClient();
77
                //Source Id for store website is 2 and our normal website is 1
78
                SourceDetail detail = tcl.getClient().getSourceDetail(2);
79
 
80
                tempUserId = createUserAndSendMail(detail.getEmail());
81
            } catch(Exception e) {
82
                log.error("Unable to get default source", e);
83
            }
84
        }
85
 
86
        if(tempUserId == -1) {
87
            log.error("Unable to get a user, terminating the transaction");
88
            setErrorMsg("Unable to create a user for this order. Please try again.");
89
            return "error-result";
90
        }
91
 
92
        txnObj.setShoppingCartid(0);
93
        txnObj.setCustomer_id(tempUserId);
94
        txnObj.setCreatedOn(Calendar.getInstance().getTimeInMillis());
95
        txnObj.setTransactionStatus(TransactionStatus.INIT);
96
        txnObj.setStatusDescription("New Store Order");
97
        txnObj.setCoupon_code("");
98
        txnObj.setEmiSchemeId(0);
99
 
100
        List<Order> orders = new ArrayList<Order>();
101
        if(createOrder(tempUserId) ==  null) {
102
            log.error("Unable to create transaction");
103
            setErrorMsg("Unable to create order. Please try again.");
104
            return "error-result";
105
        }
106
 
107
        orders.add(createOrder(tempUserId));
108
        txnObj.setOrders(orders);
109
        TransactionClient tcl;
7268 anupam.sin 110
        long txnId = 0;
7263 anupam.sin 111
        try {
112
            tcl = new TransactionClient();
7268 anupam.sin 113
            txnId = tcl.getClient().createTransaction(txnObj);
7263 anupam.sin 114
        } catch (Exception e) {
115
            log.error("Unable to create transaction", e);
116
            setErrorMsg("Unable to create order. Please try again.");
117
            return "error-result";
118
        }
7268 anupam.sin 119
 
120
        try {
121
        PaymentClient pcl = new PaymentClient();
122
        Long merchantPaymentId = pcl.getClient().createPayment(tempUserId, price, 10, txnId, true);
123
        //Update payment status as authorized
124
        pcl.getClient().updatePaymentDetails(merchantPaymentId, "", "", "0", "", "", "", "", "", PaymentStatus.SUCCESS, "", null);
125
        } catch(Exception e) {
126
            log.error("Unable to create payment for this order");
127
            setErrorMsg("Unable to create order. Please try again.");
128
            return "error-result";
129
        }
7263 anupam.sin 130
        return "create-order";
131
    }
132
 
133
    public Order createOrder(long userId) {
134
        LineItem lineObj = createLineItem();
135
        if (lineObj == null) {
136
            return null;
137
        }
138
        Order order = new Order();
139
        User user = new User();
140
        try {
141
            UserClient ucl = new UserClient();
142
            user = ucl.getClient().getUserById(userId);
143
        } catch (Exception e) {
144
            log.error("Unable to get user for id " + userId, e);
145
            return null;
146
        }
147
        order.setCustomer_id(user.getUserId());
148
        order.setCustomer_email(user.getEmail());
149
        order.setCustomer_name(name);
150
        order.setCustomer_address1(line1);
151
        order.setCustomer_address2(line2);
152
        order.setCustomer_city(city);
153
        order.setCustomer_state(state);
154
        order.setCustomer_pincode(pincode);
155
        order.setCustomer_mobilenumber(phone);
156
 
157
        order.setTotal_amount(price);
158
        order.setStatus(OrderStatus.SUBMITTED_FOR_PROCESSING);
159
        order.setStatusDescription("In process");
160
        order.setCreated_timestamp(Calendar.getInstance().getTimeInMillis());
7268 anupam.sin 161
        order.setTotal_weight(lineObj.getTotal_weight());
7263 anupam.sin 162
 
7268 anupam.sin 163
        List<LineItem> lines = new ArrayList<LineItem>();
164
        lines.add(lineObj);
165
        order.setLineitems(lines);
7263 anupam.sin 166
        return order;
167
    }
168
 
169
    public LineItem createLineItem() {
170
        LineItem lineitem = new LineItem();
171
        Item item = null;
172
        try {
173
            CatalogClient ccl = new CatalogClient();
174
            item = ccl.getClient().getItem(product_id);
175
        } catch (Exception e) {
176
            log.error("Unable to get the item from catalog service, ItemId : " + product_id, e);
177
            setErrorMsg("Unable to create order. Please try again.");
178
            return null;
179
        }
180
        lineitem.setProductGroup(item.getProductGroup());
181
        lineitem.setBrand(item.getBrand());
182
        lineitem.setModel_number(item.getModelNumber());
183
        lineitem.setModel_name(item.getModelName());
184
        if(item.getColor() == null || item.getColor().isEmpty()) {
185
            lineitem.setColor("");
186
        } else {
187
            lineitem.setColor(item.getColor());
188
        }
189
        lineitem.setExtra_info(item.getFeatureDescription());
190
        lineitem.setItem_id(item.getId());
191
        lineitem.setQuantity(1.0);
192
        lineitem.setUnit_price(price);
193
        lineitem.setTotal_price(price);
194
        lineitem.setUnit_weight(item.getWeight());
195
        lineitem.setTotal_weight(item.getWeight());
196
        lineitem.setDealText(item.getBestDealText());
197
 
198
        if(item.getWarrantyPeriod() > 0) {
199
            //Computing Manufacturer Warranty expiry date
200
            Calendar cal = Calendar.getInstance();
201
            cal.add(Calendar.MONTH, item.getWarrantyPeriod());
7268 anupam.sin 202
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
7263 anupam.sin 203
            lineitem.setWarrantry_expiry_timestamp(cal.getTimeInMillis());
204
        }
205
 
206
        return lineitem;
207
    }
208
 
209
    private static String generateNewPassword() {
210
        char[] buf = new char[LENGTH];
211
        for (int i = 0; i < buf.length; i++) {
212
            buf[i] = chars.charAt(random.nextInt(chars.length()));
213
        }
214
        return new String(buf);
215
    }
216
 
217
    private long createUserAndSendMail(String email) {
218
        User user = null;
219
        String password = null;
220
        try{
221
        UserClient ucl = new UserClient();
222
        user = ucl.getClient().getUserByEmail(email);
223
        if(user.getUserId() == -1) {
224
            user.setEmail(email);
225
            password = generateNewPassword();
226
            String encryptedPassword = password;
227
            user.setPassword(encryptedPassword);
228
            user.setCommunicationEmail(email);
229
            UserClient userContextServiceClient = new UserClient();
230
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
231
            user = userClient.createUser(user);
232
 
233
            List<String> toList = new ArrayList<String>();
234
            toList.add(email);
235
            HelperClient helperServiceClient = null;
236
            helperServiceClient = new HelperClient();
237
            in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
238
            Mail mail = new Mail();
239
            mail.setSubject("Saholic Registration successful");
240
            mail.setTo(toList);
241
            mail.setData("Your have successfully registered with Saholic.com. Your user name is: " + email + " and your password is: " +  password);
242
            client.sendMail(mail);
243
            }
244
        }catch (UserContextException ux){
245
            return -1;               
246
        } catch (TTransportException e) {
247
            return -1;
248
        } catch (TException e) {
249
            return -1;
250
        } catch (Exception e) {
251
            log.error("Unexpected error while mailing the new password");
252
            return -1;
253
        }
254
        return user.getUserId();
255
    }
256
 
7226 anupam.sin 257
    public String getLine1() {
258
        return line1;
259
    }
260
 
261
    public void setLine1(String line1) {
262
        this.line1 = line1;
263
    }
264
 
265
    public String getLine2() {
266
        return line2;
267
    }
268
 
269
    public void setLine2(String line2) {
270
        this.line2 = line2;
271
    }
272
 
273
    public String getCity() {
274
        return city;
275
    }
276
 
277
    public void setCity(String city) {
278
        this.city = city;
279
    }
280
 
281
    public String getState() {
282
        return state;
283
    }
284
 
285
    public void setState(String state) {
286
        this.state = state;
287
    }
288
 
289
    public String getPincode() {
290
        return pincode;
291
    }
292
 
293
    public void setPincode(String pincode) {
294
        this.pincode = pincode;
295
    }
296
 
7248 anupam.sin 297
    public String getName() {
298
        return name;
299
    }
300
 
301
    public void setName(String name) {
302
        this.name = name;
303
    }
304
 
305
    public String getPhone() {
306
        return phone;
307
    }
308
 
309
    public void setPhone(String phone) {
310
        this.phone = phone;
311
    }
312
 
7263 anupam.sin 313
    public void setProduct_id(Long product_id) {
314
        this.product_id = product_id;
315
    }
316
 
317
    public Long getProduct_id() {
318
        return product_id;
319
    }
320
 
321
    public String getEmail() {
322
        return email;
323
    }
324
 
325
    public void setEmail(String email) {
326
        this.email = email;
327
    }
328
 
329
    public void setErrorMsg(String errorMsg) {
330
        this.errorMsg = errorMsg;
331
    }
332
 
333
    public String getErrorMsg() {
334
        return errorMsg;
335
    }
336
 
337
    public double getPrice() {
338
        return price;
339
    }
340
 
341
    public void setPrice(double price) {
342
        this.price = price;
343
    }
7226 anupam.sin 344
}