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