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
		PaymentInfo paymentInfo = new PaymentInfo();
154
		t.setPaymentInfo(paymentInfo);
155
		t.setShoppingCartid(pmnt.getCart_id());
156
		t.setCustomer_id(pmnt.getUser_id());
157
		t.setCreatedOn(pmnt.getBank_ack_timestamp());
158
		t.setTransactionStatus(TransactionStatus.INIT);
159
		t.setStatusDescription("New order");
428 ashish 160
 
485 rajveer 161
		//create payment
162
		Payment payment = new Payment();
163
		payment.setAmount(Double.parseDouble(amount));
164
		payment.setBank_tx_id(tranId);
165
		payment.setMerchant_tx_id(trackId);
166
		payment.setStatus(PaymentStatus.SUCCESS);
167
		payment.setMode("credit card");
168
		payment.setCompletionTimestamp(pmnt.getBank_ack_timestamp());
169
		payment.setSubmissionTimestamp(pmnt.getInit_timestamp());
170
 
171
		Map<Long, Payment> paymentMap = new HashMap<Long, Payment>();
172
		paymentMap.put(payment.getCompletionTimestamp(), payment);
173
		paymentInfo.setPayments(paymentMap);
174
 
175
		t.setOrders(getOrders());
176
 
177
		return t;
178
	}
179
 
180
private List<Order> getOrders(){
181
	List<Order> orders = new ArrayList<Order>();
182
 
183
	Cart c = null;
184
	try {
555 chandransh 185
		c = usc.getClient().getCart(pmnt.getCart_id());
485 rajveer 186
	} catch (ShoppingCartException e) {
187
		// TODO Auto-generated catch block
188
		e.printStackTrace();
189
	} catch (TException e) {
190
		// TODO Auto-generated catch block
191
		e.printStackTrace();
192
	} catch (Exception e) {
193
		// TODO Auto-generated catch block
194
		e.printStackTrace();
195
	}
555 chandransh 196
	List<in.shop2020.model.v1.user.Line> lines = c.getLines();
485 rajveer 197
 
555 chandransh 198
	for(in.shop2020.model.v1.user.Line line : lines){
485 rajveer 199
		if(line.getLineStatus() == LineStatus.LINE_ACTIVE){
200
			//line is active
201
			LineItem lineItem = getLineItem(line.getItemId());
202
			// create orders equivalent to quantity. Create one order per item.
203
			for(int i= 0; i<line.getQuantity(); i++){
204
				//  set order
205
				Order order = getOrder(c.getAddressId(), lineItem);
206
				order.addToLineitems(lineItem);
207
				orders.add(order);
208
			}
209
		}
210
	}
211
	return orders;
212
}
213
 
214
 
215
private Order getOrder(long addressId, LineItem lineItem){
216
	String sku_id = lineItem.getSku_id();
217
	double total_amount = lineItem.getTotal_price();
218
	double total_weight = lineItem.getTotal_weight();
219
 
220
	long customer_id = pmnt.getUser_id();
221
	Address address = getAddress(addressId);
222
	String customer_email = getEmail(customer_id);
223
	String customer_name = address.getName();
224
	String customer_address = getAddressString(address);
225
	String customer_mobilenumber = address.getPhone();
226
	String customer_pincode = address.getPin();
227
 
228
 
229
 
230
	// get logistics information
231
	LogisticsInfo logistics_info = null;
232
	try {
233
		logistics_info = lsc.getClient().getLogisticsInfo(customer_pincode, sku_id);
234
	} catch (TException e) {
235
		// TODO Auto-generated catch block
236
		e.printStackTrace();
237
	} catch (LogisticsServiceException e) {
238
		// TODO Auto-generated catch block
239
		e.printStackTrace();
240
	}
241
 
649 chandransh 242
	long warehouse_id = logistics_info.getWarehouseId();
243
	long logistics_provider_id= logistics_info.getProviderId();
485 rajveer 244
	String airwaybill_no = String.valueOf(logistics_info.getAirway_billno()); // should it be really long ?
245
	String tracking_id = airwaybill_no; //right now both are same
649 chandransh 246
	long expected_delivery_time = (new Date()).getTime() + 60*60*1000*24*logistics_info.getDeliveryTime(); 
485 rajveer 247
 
248
	long created_timestamp = (new Date()).getTime();
249
	OrderStatus status = OrderStatus.INIT; // to get from orderstatus file
250
	String statusDescription = "New Order";
251
 
252
	Order order = new Order();
253
	order.setLogistics_provider_id(logistics_provider_id);
254
	order.setAirwaybill_no(airwaybill_no);
255
	order.setExpected_delivery_time(expected_delivery_time);
256
	order.setTracking_id(tracking_id);
257
 
258
	order.setWarehouse_id(warehouse_id);
259
	order.setCreated_timestamp(created_timestamp);
260
	order.setStatus(status);
261
	order.setStatusDescription(statusDescription);
262
 
263
	order.setCustomer_id(customer_id);
264
	order.setCustomer_name(customer_name);
265
	order.setCustomer_address(customer_address);
266
	order.setCustomer_email(customer_email);
267
	order.setCustomer_mobilenumber(customer_mobilenumber);
268
	order.setCustomer_pincode(customer_pincode);
269
 
270
	order.setTotal_amount(total_amount);
271
	order.setTotal_weight(total_weight);
272
 
273
	return order;
274
}
275
 
276
private String getEmail(long userId){
277
	String email = null;
278
	try {
555 chandransh 279
		email = usc.getClient().getUserById(userId).getEmail();
485 rajveer 280
	} catch (UserContextException e) {
281
		// TODO Auto-generated catch block
282
		e.printStackTrace();
283
	} catch (TException e) {
284
		// TODO Auto-generated catch block
285
		e.printStackTrace();
286
	}
287
	return email;
288
}
289
 
290
private Address getAddress(long addressId){
517 rajveer 291
	List<Address> addresses = null;
485 rajveer 292
	try {
555 chandransh 293
		addresses = usc.getClient().getUserById(pmnt.getUser_id()).getAddresses();
485 rajveer 294
	} catch (UserContextException e) {
295
		// TODO Auto-generated catch block
296
		e.printStackTrace();
297
	} catch (TException e) {
298
		// TODO Auto-generated catch block
299
		e.printStackTrace();
300
	}
301
	Address address = null;
302
	for(Address a : addresses){
303
		if(a.getId() == addressId){
304
			address = a;
305
		}
306
	}
307
	return address;
308
}
309
 
637 rajveer 310
	private LineItem getLineItem(long itemId){ 
485 rajveer 311
	LineItem lineItem = new LineItem();
312
	Item item = null;
313
	try {
637 rajveer 314
		item = csc.getClient().getItem(itemId);
485 rajveer 315
	} catch (InventoryServiceException e) {
316
		// TODO Auto-generated catch block
317
		e.printStackTrace();
318
	} catch (TException e) {
319
		// TODO Auto-generated catch block
320
		e.printStackTrace();
321
	}
322
 
323
	String model_name = item.getModelName();
324
	String model_number = item.getModelNumber();
517 rajveer 325
	String brand = item.getManufacturerName();
485 rajveer 326
	String sku_id = item.getVendorItemId();
327
	double unit_price = item.getSellingPrice();
328
	double unit_weight = item.getWeight();
329
	double total_price = item.getSellingPrice();
330
	double total_weight = item.getWeight();
331
	String  extra_info = item.getFeatureDescription();
332
	double quantity = 1; // because now we will create one order per item
333
 
334
	lineItem.setSku_id(sku_id);
335
	lineItem.setBrand(brand);
336
	lineItem.setExtra_info(extra_info);
337
	lineItem.setModel_name(model_name);
338
	lineItem.setModel_number(model_number);
339
	lineItem.setQuantity(quantity);
340
	lineItem.setSku_id(sku_id);
341
	lineItem.setUnit_price(unit_price);
342
	lineItem.setUnit_weight(unit_weight);
343
	lineItem.setTotal_price(total_price);
344
	lineItem.setTotal_weight(total_weight);
345
	return lineItem;
346
}
347
 
348
	private String getAddressString(Address a){
349
		String add = a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry();
350
		return add;
351
	}
352
 
353
	private String getAddress(Set<Address> address, long id){
354
		for(Address a : address){
355
			if(a.getId() == id){
356
				//Prepare String
357
				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();
358
				return add;
359
			}
360
		}
361
		return "";
362
	}
363
 
364
	public String getMessage(){
365
		return this.message;
366
	}
367
 
368
	public long getTransactionId(){
369
		return this.transaction_id;
370
	}
371
 
372
 
373
}
374
 
375
/*
376
	private Transaction getTransaction(){
377
 
428 ashish 378
		try {
379
			pmnt = pclient.getClient().getPayment(Long.parseLong(trackId));
380
		} catch (NumberFormatException e1) {
381
			// TODO Auto-generated catch block
382
			e1.printStackTrace();
383
		} catch (PaymentException e1) {
384
			// TODO Auto-generated catch block
385
			e1.printStackTrace();
386
		} catch (TException e1) {
387
			// TODO Auto-generated catch block
388
			e1.printStackTrace();
389
		}
390
		Transaction t = new Transaction();
391
		PaymentInfo paymentInfo = new PaymentInfo();
392
		t.setPaymentInfo(paymentInfo);
393
		t.setShoppingCartid(pmnt.getCart_id());
394
		t.setCustomer_id(pmnt.getUser_id());
395
		t.setCreatedOn(pmnt.getBank_ack_timestamp());
396
		t.setTransactionStatus(TransactionStatus.INIT);
397
		t.setStatusDescription("New order");
398
		//create payment
399
		Payment payment = new Payment();
400
		payment.setAmount(Double.parseDouble(amount));
401
		payment.setBank_tx_id(tranId);
402
		payment.setMerchant_tx_id(trackId);
403
		payment.setStatus(PaymentStatus.SUCCESS);
404
		payment.setMode("credit card");
405
		payment.setCompletionTimestamp(pmnt.getBank_ack_timestamp());
406
		payment.setSubmissionTimestamp(pmnt.getInit_timestamp());
407
 
408
		Map<Long, Payment> paymentMap = new HashMap<Long, Payment>();
409
		paymentMap.put(payment.getCompletionTimestamp(), payment);
410
 
411
		paymentInfo.setPayments(paymentMap);
412
 
413
		Cart c = null;
414
		try {
415
			c = cl.getClient().getCart(pmnt.getCart_id());
416
		} catch (ShoppingCartException e) {
417
			// TODO Auto-generated catch block
418
			e.printStackTrace();
419
		} catch (TException e) {
420
			// TODO Auto-generated catch block
421
			e.printStackTrace();
422
		} catch (Exception e) {
423
			// TODO Auto-generated catch block
424
			e.printStackTrace();
425
		}
426
 
427
		//Shipping info
428
 
429
		ShipmentInfo shipmentInfo = new ShipmentInfo();
430
		t.setShipmentInfo(shipmentInfo);
431
 
432
		Shipment shipment = new Shipment();
433
		try {
434
			shipment.setAirwayBillNo(lsc.getClient().getEmptyAWB(1));
435
		} catch (TException e) {
436
			// TODO Auto-generated catch block
437
			e.printStackTrace();
438
		}
439
		try {
440
			shipment.setAddress(getAddress(usc.getClient().getPrimaryInfo(pmnt.getUser_id(), false).getAddresses(),c.getAddressId()));
441
		} catch (UserContextException e) {
442
			// TODO Auto-generated catch block
443
			e.printStackTrace();
444
		} catch (TException e) {
445
			// TODO Auto-generated catch block
446
			e.printStackTrace();
447
		}
448
 
449
		shipment.setInsurance(0.0);
450
		shipment.setProvider("Aramex");
451
		shipment.setTrackingId(shipment.getAirwayBillNo());
452
		shipment.setWeight(0.0);
453
		shipment.setValue(Double.parseDouble(amount));
454
		shipment.setLineItems(null);
455
		shipmentInfo.addToShipments(shipment);
456
 
457
 
458
		//billinginfo will be added later by warehouse
459
 
460
 
461
		OrderInfo orderInfo = new OrderInfo();
462
		t.setOrderInfo(orderInfo);
463
 
555 chandransh 464
		List<LineItem> lines = c.getLines();
428 ashish 465
 
555 chandransh 466
		for(in.shop2020.model.v1.user.LineItem line : lines){
428 ashish 467
			if(line.getLineStatus() == LineStatus.LINE_ACTIVE){
468
				//line is active
469
				LineItem lineItem = new LineItem();
470
				Item item = null;
471
				try {
472
					item = csc.getClient().getItemByCatalogId(line.getItemId());
473
				} catch (InventoryServiceException e) {
474
					// TODO Auto-generated catch block
475
					e.printStackTrace();
476
				} catch (TException e) {
477
					// TODO Auto-generated catch block
478
					e.printStackTrace();
479
				}
480
				in.shop2020.model.v1.order.Item oitem = new in.shop2020.model.v1.order.Item();
481
				oitem.setId(item.getCatalogItemId());
482
				//get price for it 
483
				long warehouse = getWarehouse();
484
				oitem.setPrice(item.getPrice().get(warehouse));
485
				oitem.setWeight(item.getWeight());
486
				lineItem.setItem(oitem);
487
				lineItem.setAddedOn(pmnt.getBank_ack_timestamp());
488
				orderInfo.addToLineitems(lineItem);
489
			}
490
		}
491
		return t;
492
 
493
	}
485 rajveer 494
	*/