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