Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
428 ashish 1
package in.shop2020.serving.controllers;
2
 
3
import java.io.IOException;
485 rajveer 4
import java.util.ArrayList;
5
import java.util.Date;
428 ashish 6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10
 
11
import org.apache.struts2.rest.DefaultHttpHeaders;
12
import org.apache.struts2.rest.HttpHeaders;
13
import org.apache.thrift.TException;
14
 
485 rajveer 15
import in.shop2020.logistics.LogisticsInfo;
16
import in.shop2020.logistics.LogisticsServiceException;
428 ashish 17
import in.shop2020.model.v1.catalog.InventoryServiceException;
18
import in.shop2020.model.v1.catalog.Item;
19
import in.shop2020.model.v1.order.BillingInfo;
555 chandransh 20
import in.shop2020.model.v1.order.Order;
428 ashish 21
import in.shop2020.model.v1.order.LineItem;
22
import in.shop2020.model.v1.order.OrderInfo;
485 rajveer 23
import in.shop2020.model.v1.order.OrderStatus;
428 ashish 24
import in.shop2020.model.v1.order.Payment;
25
import in.shop2020.model.v1.order.PaymentInfo;
26
import in.shop2020.model.v1.order.PaymentStatus;
27
import in.shop2020.model.v1.order.Shipment;
28
import in.shop2020.model.v1.order.ShipmentInfo;
29
import in.shop2020.model.v1.order.Transaction;
30
import in.shop2020.model.v1.order.TransactionServiceException;
31
import in.shop2020.model.v1.order.TransactionStatus;
555 chandransh 32
import in.shop2020.model.v1.user.Cart;
33
import in.shop2020.model.v1.user.LineStatus;
34
import in.shop2020.model.v1.user.ShoppingCartException;
428 ashish 35
import in.shop2020.model.v1.user.Address;
555 chandransh 36
import in.shop2020.model.v1.user.User;
428 ashish 37
import in.shop2020.model.v1.user.UserContextException;
38
import in.shop2020.payments.PaymentException;
39
import in.shop2020.thrift.clients.CatalogServiceClient;
40
import in.shop2020.thrift.clients.LogisticsServiceClient;
41
import in.shop2020.thrift.clients.PaymentServiceClient;
42
import in.shop2020.thrift.clients.TransactionServiceClient;
43
import in.shop2020.thrift.clients.UserContextServiceClient;
44
import in.shop2020.utils.Logger;
45
 
517 rajveer 46
public class PayResponseController extends BaseController{
428 ashish 47
	public static String AMOUNT = "amount";
48
	public static String TRACKID = "trackid";
49
	public static String TRACKID_CANCELLED = "trackId";
50
	public static String RESULT = "result";
51
	public static String AUTH = "auth";
52
	public static String TRANID = "tranid";
53
	public static String PAYMENTID = "paymentId";
54
	public static String REF = "ref";
55
	public static String POSTDATE = "postdate";
56
	public static String CANCELED = "CANCELED";
57
	public static String APPROVED = "APPROVED";
58
	public static String CAPTURED = "CAPTURED";
59
 
60
	PaymentServiceClient pclient = null;
61
	LogisticsServiceClient lsc = null;
62
	UserContextServiceClient usc = null;
63
	CatalogServiceClient csc = null;
64
	TransactionServiceClient tsc = null;
65
 
66
	String amount;
67
	String trackId;
68
	String result;
69
	String postdate;
70
	String auth;
71
	String ref;
72
	String tranId;
73
	String paymentId;
74
	long transaction_id = 0;
75
	String message = "Unable to process the payment. PLease try Again.";
76
 
77
	in.shop2020.payments.Payment pmnt = null;
78
 
517 rajveer 79
	public PayResponseController(){
428 ashish 80
		super();
81
		try {
82
			pclient = new PaymentServiceClient();
83
			lsc = new LogisticsServiceClient();
84
			usc = new UserContextServiceClient();
85
			csc = new CatalogServiceClient();
86
			tsc = new TransactionServiceClient();
87
 
88
		} catch (Exception e) {
89
			Logger.log("Could not initialize the paymentservice client", this);
90
		}
91
	}
92
 
93
	public HttpHeaders show() throws IOException, SecurityException{
94
		//need to ignore id here.
95
 
96
		amount = this.request.getParameter(AMOUNT);
97
		paymentId = this.request.getParameter(PAYMENTID);
98
		tranId = this.request.getParameter(TRANID);
99
		result = this.request.getParameter(RESULT);
100
		if (result.equalsIgnoreCase(CANCELED)){
101
			trackId = this.request.getParameter(TRACKID_CANCELLED);
102
		}else{
103
			trackId = this.request.getParameter(TRACKID);
104
		}
105
		auth = this.request.getParameter(AUTH);
106
		ref = this.request.getParameter(REF);
107
		postdate = this.request.getParameter(POSTDATE);
108
 
109
		//update the payment info object
110
		try {
111
			pclient.getClient().addBankDetails(Long.parseLong(trackId), paymentId, tranId, "", "", postdate, auth, ref);
112
			message = "Payment processed. Creating transaction. In case you see this message, please call us with ref no "+ trackId;
113
			Transaction t = getTransaction();
114
			transaction_id = tsc.getClient().createTransaction(t);
115
			message = "Your order id is "+ transaction_id;
555 chandransh 116
			usc.getClient().commitCart(pmnt.getCart_id());
428 ashish 117
			message = "Error commiting the cart, but your order has been placed. Please refer to "+ transaction_id;
118
 
119
		} catch (NumberFormatException e) {
120
 
121
			Logger.log(e.toString(), this);
122
		} catch (PaymentException e) {
123
 
124
			Logger.log(e.toString(), this);
125
		} catch (TException e) {
126
 
127
			Logger.log(e.toString(), this);
128
		} catch (TransactionServiceException e) {
129
			// TODO Auto-generated catch block
130
			e.printStackTrace();
507 rajveer 131
		} catch (ShoppingCartException e) {
132
			// TODO Auto-generated catch block
133
			e.printStackTrace();
428 ashish 134
		}
517 rajveer 135
		return new DefaultHttpHeaders("show");
428 ashish 136
	}
485 rajveer 137
 
428 ashish 138
 
139
	private Transaction getTransaction(){
485 rajveer 140
		try {
141
			pmnt = pclient.getClient().getPayment(Long.parseLong(trackId));
142
		} catch (NumberFormatException e1) {
143
			// TODO Auto-generated catch block
144
			e1.printStackTrace();
145
		} catch (PaymentException e1) {
146
			// TODO Auto-generated catch block
147
			e1.printStackTrace();
148
		} catch (TException e1) {
149
			// TODO Auto-generated catch block
150
			e1.printStackTrace();
151
		}
152
		Transaction t = new Transaction();
153
		t.setShoppingCartid(pmnt.getCart_id());
154
		t.setCustomer_id(pmnt.getUser_id());
155
		t.setCreatedOn(pmnt.getBank_ack_timestamp());
156
		t.setTransactionStatus(TransactionStatus.INIT);
157
		t.setStatusDescription("New order");
428 ashish 158
 
485 rajveer 159
		//create payment
160
		Payment payment = new Payment();
161
		payment.setAmount(Double.parseDouble(amount));
162
		payment.setBank_tx_id(tranId);
163
		payment.setMerchant_tx_id(trackId);
164
		payment.setStatus(PaymentStatus.SUCCESS);
165
		payment.setMode("credit card");
166
		payment.setCompletionTimestamp(pmnt.getBank_ack_timestamp());
167
		payment.setSubmissionTimestamp(pmnt.getInit_timestamp());
168
 
169
		Map<Long, Payment> paymentMap = new HashMap<Long, Payment>();
170
		paymentMap.put(payment.getCompletionTimestamp(), payment);
171
 
172
		t.setOrders(getOrders());
173
 
174
		return t;
175
	}
176
 
177
private List<Order> getOrders(){
178
	List<Order> orders = new ArrayList<Order>();
179
 
180
	Cart c = null;
181
	try {
555 chandransh 182
		c = usc.getClient().getCart(pmnt.getCart_id());
485 rajveer 183
	} catch (ShoppingCartException e) {
184
		// TODO Auto-generated catch block
185
		e.printStackTrace();
186
	} catch (TException e) {
187
		// TODO Auto-generated catch block
188
		e.printStackTrace();
189
	} catch (Exception e) {
190
		// TODO Auto-generated catch block
191
		e.printStackTrace();
192
	}
555 chandransh 193
	List<in.shop2020.model.v1.user.Line> lines = c.getLines();
485 rajveer 194
 
555 chandransh 195
	for(in.shop2020.model.v1.user.Line line : lines){
485 rajveer 196
		if(line.getLineStatus() == LineStatus.LINE_ACTIVE){
197
			//line is active
198
			LineItem lineItem = getLineItem(line.getItemId());
199
			// create orders equivalent to quantity. Create one order per item.
200
			for(int i= 0; i<line.getQuantity(); i++){
201
				//  set order
202
				Order order = getOrder(c.getAddressId(), lineItem);
203
				order.addToLineitems(lineItem);
204
				orders.add(order);
205
			}
206
		}
207
	}
208
	return orders;
209
}
210
 
211
 
212
private Order getOrder(long addressId, LineItem lineItem){
213
	String sku_id = lineItem.getSku_id();
214
	double total_amount = lineItem.getTotal_price();
215
	double total_weight = lineItem.getTotal_weight();
216
 
217
	long customer_id = pmnt.getUser_id();
218
	Address address = getAddress(addressId);
219
	String customer_email = getEmail(customer_id);
220
	String customer_name = address.getName();
221
	String customer_address = getAddressString(address);
222
	String customer_mobilenumber = address.getPhone();
223
	String customer_pincode = address.getPin();
224
 
225
 
226
 
227
	// get logistics information
228
	LogisticsInfo logistics_info = null;
229
	try {
230
		logistics_info = lsc.getClient().getLogisticsInfo(customer_pincode, sku_id);
231
	} catch (TException e) {
232
		// TODO Auto-generated catch block
233
		e.printStackTrace();
234
	} catch (LogisticsServiceException e) {
235
		// TODO Auto-generated catch block
236
		e.printStackTrace();
237
	}
238
 
649 chandransh 239
	long warehouse_id = logistics_info.getWarehouseId();
240
	long logistics_provider_id= logistics_info.getProviderId();
485 rajveer 241
	String airwaybill_no = String.valueOf(logistics_info.getAirway_billno()); // should it be really long ?
242
	String tracking_id = airwaybill_no; //right now both are same
649 chandransh 243
	long expected_delivery_time = (new Date()).getTime() + 60*60*1000*24*logistics_info.getDeliveryTime(); 
485 rajveer 244
 
245
	long created_timestamp = (new Date()).getTime();
246
	OrderStatus status = OrderStatus.INIT; // to get from orderstatus file
247
	String statusDescription = "New Order";
248
 
249
	Order order = new Order();
250
	order.setLogistics_provider_id(logistics_provider_id);
251
	order.setAirwaybill_no(airwaybill_no);
252
	order.setExpected_delivery_time(expected_delivery_time);
253
	order.setTracking_id(tracking_id);
254
 
255
	order.setWarehouse_id(warehouse_id);
256
	order.setCreated_timestamp(created_timestamp);
257
	order.setStatus(status);
258
	order.setStatusDescription(statusDescription);
259
 
260
	order.setCustomer_id(customer_id);
261
	order.setCustomer_name(customer_name);
262
	order.setCustomer_address(customer_address);
263
	order.setCustomer_email(customer_email);
264
	order.setCustomer_mobilenumber(customer_mobilenumber);
265
	order.setCustomer_pincode(customer_pincode);
266
 
267
	order.setTotal_amount(total_amount);
268
	order.setTotal_weight(total_weight);
269
 
270
	return order;
271
}
272
 
273
private String getEmail(long userId){
274
	String email = null;
275
	try {
555 chandransh 276
		email = usc.getClient().getUserById(userId).getEmail();
485 rajveer 277
	} catch (UserContextException e) {
278
		// TODO Auto-generated catch block
279
		e.printStackTrace();
280
	} catch (TException e) {
281
		// TODO Auto-generated catch block
282
		e.printStackTrace();
283
	}
284
	return email;
285
}
286
 
287
private Address getAddress(long addressId){
517 rajveer 288
	List<Address> addresses = null;
485 rajveer 289
	try {
555 chandransh 290
		addresses = usc.getClient().getUserById(pmnt.getUser_id()).getAddresses();
485 rajveer 291
	} catch (UserContextException e) {
292
		// TODO Auto-generated catch block
293
		e.printStackTrace();
294
	} catch (TException e) {
295
		// TODO Auto-generated catch block
296
		e.printStackTrace();
297
	}
298
	Address address = null;
299
	for(Address a : addresses){
300
		if(a.getId() == addressId){
301
			address = a;
302
		}
303
	}
304
	return address;
305
}
306
 
637 rajveer 307
	private LineItem getLineItem(long itemId){ 
485 rajveer 308
	LineItem lineItem = new LineItem();
309
	Item item = null;
310
	try {
637 rajveer 311
		item = csc.getClient().getItem(itemId);
485 rajveer 312
	} catch (InventoryServiceException e) {
313
		// TODO Auto-generated catch block
314
		e.printStackTrace();
315
	} catch (TException e) {
316
		// TODO Auto-generated catch block
317
		e.printStackTrace();
318
	}
319
 
320
	String model_name = item.getModelName();
321
	String model_number = item.getModelNumber();
517 rajveer 322
	String brand = item.getManufacturerName();
485 rajveer 323
	String sku_id = item.getVendorItemId();
324
	double unit_price = item.getSellingPrice();
325
	double unit_weight = item.getWeight();
326
	double total_price = item.getSellingPrice();
327
	double total_weight = item.getWeight();
328
	String  extra_info = item.getFeatureDescription();
329
	double quantity = 1; // because now we will create one order per item
330
 
331
	lineItem.setSku_id(sku_id);
332
	lineItem.setBrand(brand);
333
	lineItem.setExtra_info(extra_info);
334
	lineItem.setModel_name(model_name);
335
	lineItem.setModel_number(model_number);
336
	lineItem.setQuantity(quantity);
337
	lineItem.setSku_id(sku_id);
338
	lineItem.setUnit_price(unit_price);
339
	lineItem.setUnit_weight(unit_weight);
340
	lineItem.setTotal_price(total_price);
341
	lineItem.setTotal_weight(total_weight);
342
	return lineItem;
343
}
344
 
345
	private String getAddressString(Address a){
346
		String add = a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry();
347
		return add;
348
	}
349
 
350
	private String getAddress(Set<Address> address, long id){
351
		for(Address a : address){
352
			if(a.getId() == id){
353
				//Prepare String
354
				String add = a.getName()+",\n"+a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry()+",\n"+a.getPin()+",\n Phone :- "+a.getPhone();
355
				return add;
356
			}
357
		}
358
		return "";
359
	}
360
 
361
	public String getMessage(){
362
		return this.message;
363
	}
364
 
365
	public long getTransactionId(){
366
		return this.transaction_id;
367
	}
368
 
369
 
370
}
371
 
372
/*
373
	private Transaction getTransaction(){
374
 
428 ashish 375
		try {
376
			pmnt = pclient.getClient().getPayment(Long.parseLong(trackId));
377
		} catch (NumberFormatException e1) {
378
			// TODO Auto-generated catch block
379
			e1.printStackTrace();
380
		} catch (PaymentException e1) {
381
			// TODO Auto-generated catch block
382
			e1.printStackTrace();
383
		} catch (TException e1) {
384
			// TODO Auto-generated catch block
385
			e1.printStackTrace();
386
		}
387
		Transaction t = new Transaction();
388
		PaymentInfo paymentInfo = new PaymentInfo();
389
		t.setPaymentInfo(paymentInfo);
390
		t.setShoppingCartid(pmnt.getCart_id());
391
		t.setCustomer_id(pmnt.getUser_id());
392
		t.setCreatedOn(pmnt.getBank_ack_timestamp());
393
		t.setTransactionStatus(TransactionStatus.INIT);
394
		t.setStatusDescription("New order");
395
		//create payment
396
		Payment payment = new Payment();
397
		payment.setAmount(Double.parseDouble(amount));
398
		payment.setBank_tx_id(tranId);
399
		payment.setMerchant_tx_id(trackId);
400
		payment.setStatus(PaymentStatus.SUCCESS);
401
		payment.setMode("credit card");
402
		payment.setCompletionTimestamp(pmnt.getBank_ack_timestamp());
403
		payment.setSubmissionTimestamp(pmnt.getInit_timestamp());
404
 
405
		Map<Long, Payment> paymentMap = new HashMap<Long, Payment>();
406
		paymentMap.put(payment.getCompletionTimestamp(), payment);
407
 
408
		paymentInfo.setPayments(paymentMap);
409
 
410
		Cart c = null;
411
		try {
412
			c = cl.getClient().getCart(pmnt.getCart_id());
413
		} catch (ShoppingCartException e) {
414
			// TODO Auto-generated catch block
415
			e.printStackTrace();
416
		} catch (TException e) {
417
			// TODO Auto-generated catch block
418
			e.printStackTrace();
419
		} catch (Exception e) {
420
			// TODO Auto-generated catch block
421
			e.printStackTrace();
422
		}
423
 
424
		//Shipping info
425
 
426
		ShipmentInfo shipmentInfo = new ShipmentInfo();
427
		t.setShipmentInfo(shipmentInfo);
428
 
429
		Shipment shipment = new Shipment();
430
		try {
431
			shipment.setAirwayBillNo(lsc.getClient().getEmptyAWB(1));
432
		} catch (TException e) {
433
			// TODO Auto-generated catch block
434
			e.printStackTrace();
435
		}
436
		try {
437
			shipment.setAddress(getAddress(usc.getClient().getPrimaryInfo(pmnt.getUser_id(), false).getAddresses(),c.getAddressId()));
438
		} catch (UserContextException e) {
439
			// TODO Auto-generated catch block
440
			e.printStackTrace();
441
		} catch (TException e) {
442
			// TODO Auto-generated catch block
443
			e.printStackTrace();
444
		}
445
 
446
		shipment.setInsurance(0.0);
447
		shipment.setProvider("Aramex");
448
		shipment.setTrackingId(shipment.getAirwayBillNo());
449
		shipment.setWeight(0.0);
450
		shipment.setValue(Double.parseDouble(amount));
451
		shipment.setLineItems(null);
452
		shipmentInfo.addToShipments(shipment);
453
 
454
 
455
		//billinginfo will be added later by warehouse
456
 
457
 
458
		OrderInfo orderInfo = new OrderInfo();
459
		t.setOrderInfo(orderInfo);
460
 
555 chandransh 461
		List<LineItem> lines = c.getLines();
428 ashish 462
 
555 chandransh 463
		for(in.shop2020.model.v1.user.LineItem line : lines){
428 ashish 464
			if(line.getLineStatus() == LineStatus.LINE_ACTIVE){
465
				//line is active
466
				LineItem lineItem = new LineItem();
467
				Item item = null;
468
				try {
469
					item = csc.getClient().getItemByCatalogId(line.getItemId());
470
				} catch (InventoryServiceException e) {
471
					// TODO Auto-generated catch block
472
					e.printStackTrace();
473
				} catch (TException e) {
474
					// TODO Auto-generated catch block
475
					e.printStackTrace();
476
				}
477
				in.shop2020.model.v1.order.Item oitem = new in.shop2020.model.v1.order.Item();
478
				oitem.setId(item.getCatalogItemId());
479
				//get price for it 
480
				long warehouse = getWarehouse();
481
				oitem.setPrice(item.getPrice().get(warehouse));
482
				oitem.setWeight(item.getWeight());
483
				lineItem.setItem(oitem);
484
				lineItem.setAddedOn(pmnt.getBank_ack_timestamp());
485
				orderInfo.addToLineitems(lineItem);
486
			}
487
		}
488
		return t;
489
 
490
	}
485 rajveer 491
	*/