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;
4
import java.util.HashMap;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Set;
8
 
9
import org.apache.struts2.rest.DefaultHttpHeaders;
10
import org.apache.struts2.rest.HttpHeaders;
11
import org.apache.thrift.TException;
12
 
13
import in.shop2020.model.v1.catalog.InventoryServiceException;
14
import in.shop2020.model.v1.catalog.Item;
15
import in.shop2020.model.v1.order.BillingInfo;
16
import in.shop2020.model.v1.order.LineItem;
17
import in.shop2020.model.v1.order.OrderInfo;
18
import in.shop2020.model.v1.order.Payment;
19
import in.shop2020.model.v1.order.PaymentInfo;
20
import in.shop2020.model.v1.order.PaymentStatus;
21
import in.shop2020.model.v1.order.Shipment;
22
import in.shop2020.model.v1.order.ShipmentInfo;
23
import in.shop2020.model.v1.order.Transaction;
24
import in.shop2020.model.v1.order.TransactionServiceException;
25
import in.shop2020.model.v1.order.TransactionStatus;
26
import in.shop2020.model.v1.shoppingcart.Cart;
27
import in.shop2020.model.v1.shoppingcart.Line;
28
import in.shop2020.model.v1.shoppingcart.LineStatus;
29
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
30
import in.shop2020.model.v1.user.Address;
31
import in.shop2020.model.v1.user.UserContext;
32
import in.shop2020.model.v1.user.UserContextException;
33
import in.shop2020.payments.PaymentException;
34
import in.shop2020.thrift.clients.CatalogServiceClient;
35
import in.shop2020.thrift.clients.LogisticsServiceClient;
36
import in.shop2020.thrift.clients.PaymentServiceClient;
37
import in.shop2020.thrift.clients.ShoppingCartClient;
38
import in.shop2020.thrift.clients.TransactionServiceClient;
39
import in.shop2020.thrift.clients.UserContextServiceClient;
40
import in.shop2020.utils.Logger;
41
 
42
public class PayresponseController extends BaseController{
43
	public static String AMOUNT = "amount";
44
	public static String TRACKID = "trackid";
45
	public static String TRACKID_CANCELLED = "trackId";
46
	public static String RESULT = "result";
47
	public static String AUTH = "auth";
48
	public static String TRANID = "tranid";
49
	public static String PAYMENTID = "paymentId";
50
	public static String REF = "ref";
51
	public static String POSTDATE = "postdate";
52
	public static String CANCELED = "CANCELED";
53
	public static String APPROVED = "APPROVED";
54
	public static String CAPTURED = "CAPTURED";
55
 
56
	PaymentServiceClient pclient = null;
57
	LogisticsServiceClient lsc = null;
58
	ShoppingCartClient cl = null;
59
	UserContextServiceClient usc = null;
60
	CatalogServiceClient csc = null;
61
	TransactionServiceClient tsc = null;
62
 
63
	String amount;
64
	String trackId;
65
	String result;
66
	String postdate;
67
	String auth;
68
	String ref;
69
	String tranId;
70
	String paymentId;
71
	long transaction_id = 0;
72
	String message = "Unable to process the payment. PLease try Again.";
73
 
74
	in.shop2020.payments.Payment pmnt = null;
75
 
76
	public PayresponseController(){
77
		super();
78
		try {
79
			pclient = new PaymentServiceClient();
80
			lsc = new LogisticsServiceClient();
81
			cl = new ShoppingCartClient();
82
			usc = new UserContextServiceClient();
83
			csc = new CatalogServiceClient();
84
			tsc = new TransactionServiceClient();
85
 
86
		} catch (Exception e) {
87
			Logger.log("Could not initialize the paymentservice client", this);
88
		}
89
	}
90
 
91
	public HttpHeaders show() throws IOException, SecurityException{
92
		//need to ignore id here.
93
 
94
		amount = this.request.getParameter(AMOUNT);
95
		paymentId = this.request.getParameter(PAYMENTID);
96
		tranId = this.request.getParameter(TRANID);
97
		result = this.request.getParameter(RESULT);
98
		if (result.equalsIgnoreCase(CANCELED)){
99
			trackId = this.request.getParameter(TRACKID_CANCELLED);
100
		}else{
101
			trackId = this.request.getParameter(TRACKID);
102
		}
103
		auth = this.request.getParameter(AUTH);
104
		ref = this.request.getParameter(REF);
105
		postdate = this.request.getParameter(POSTDATE);
106
 
107
		//update the payment info object
108
		try {
109
			pclient.getClient().addBankDetails(Long.parseLong(trackId), paymentId, tranId, "", "", postdate, auth, ref);
110
			message = "Payment processed. Creating transaction. In case you see this message, please call us with ref no "+ trackId;
111
			Transaction t = getTransaction();
112
			transaction_id = tsc.getClient().createTransaction(t);
113
			message = "Your order id is "+ transaction_id;
114
			cl.getClient().commitCart(pmnt.getCart_id());
115
			message = "Error commiting the cart, but your order has been placed. Please refer to "+ transaction_id;
116
 
117
		} catch (NumberFormatException e) {
118
 
119
			Logger.log(e.toString(), this);
120
		} catch (PaymentException e) {
121
 
122
			Logger.log(e.toString(), this);
123
		} catch (TException e) {
124
 
125
			Logger.log(e.toString(), this);
126
		} catch (TransactionServiceException e) {
127
			// TODO Auto-generated catch block
128
			e.printStackTrace();
129
		}
130
		return new DefaultHttpHeaders(result);
131
	}
132
 
133
	private Transaction getTransaction(){
134
 
135
		try {
136
			pmnt = pclient.getClient().getPayment(Long.parseLong(trackId));
137
		} catch (NumberFormatException e1) {
138
			// TODO Auto-generated catch block
139
			e1.printStackTrace();
140
		} catch (PaymentException e1) {
141
			// TODO Auto-generated catch block
142
			e1.printStackTrace();
143
		} catch (TException e1) {
144
			// TODO Auto-generated catch block
145
			e1.printStackTrace();
146
		}
147
		Transaction t = new Transaction();
148
		PaymentInfo paymentInfo = new PaymentInfo();
149
		t.setPaymentInfo(paymentInfo);
150
		t.setShoppingCartid(pmnt.getCart_id());
151
		t.setCustomer_id(pmnt.getUser_id());
152
		t.setCreatedOn(pmnt.getBank_ack_timestamp());
153
		t.setTransactionStatus(TransactionStatus.INIT);
154
		t.setStatusDescription("New order");
155
		//create payment
156
		Payment payment = new Payment();
157
		payment.setAmount(Double.parseDouble(amount));
158
		payment.setBank_tx_id(tranId);
159
		payment.setMerchant_tx_id(trackId);
160
		payment.setStatus(PaymentStatus.SUCCESS);
161
		payment.setMode("credit card");
162
		payment.setCompletionTimestamp(pmnt.getBank_ack_timestamp());
163
		payment.setSubmissionTimestamp(pmnt.getInit_timestamp());
164
 
165
		Map<Long, Payment> paymentMap = new HashMap<Long, Payment>();
166
		paymentMap.put(payment.getCompletionTimestamp(), payment);
167
 
168
		paymentInfo.setPayments(paymentMap);
169
 
170
		Cart c = null;
171
		try {
172
			c = cl.getClient().getCart(pmnt.getCart_id());
173
		} catch (ShoppingCartException e) {
174
			// TODO Auto-generated catch block
175
			e.printStackTrace();
176
		} catch (TException e) {
177
			// TODO Auto-generated catch block
178
			e.printStackTrace();
179
		} catch (Exception e) {
180
			// TODO Auto-generated catch block
181
			e.printStackTrace();
182
		}
183
 
184
		//Shipping info
185
 
186
		ShipmentInfo shipmentInfo = new ShipmentInfo();
187
		t.setShipmentInfo(shipmentInfo);
188
 
189
		Shipment shipment = new Shipment();
190
		try {
191
			shipment.setAirwayBillNo(lsc.getClient().getEmptyAWB(1));
192
		} catch (TException e) {
193
			// TODO Auto-generated catch block
194
			e.printStackTrace();
195
		}
196
		try {
197
			shipment.setAddress(getAddress(usc.getClient().getPrimaryInfo(pmnt.getUser_id(), false).getAddresses(),c.getAddressId()));
198
		} catch (UserContextException e) {
199
			// TODO Auto-generated catch block
200
			e.printStackTrace();
201
		} catch (TException e) {
202
			// TODO Auto-generated catch block
203
			e.printStackTrace();
204
		}
205
 
206
		shipment.setInsurance(0.0);
207
		shipment.setProvider("Aramex");
208
		shipment.setTrackingId(shipment.getAirwayBillNo());
209
		shipment.setWeight(0.0);
210
		shipment.setValue(Double.parseDouble(amount));
211
		shipment.setLineItems(null);
212
		shipmentInfo.addToShipments(shipment);
213
 
214
 
215
		//billinginfo will be added later by warehouse
216
 
217
 
218
		OrderInfo orderInfo = new OrderInfo();
219
		t.setOrderInfo(orderInfo);
220
 
221
		List<Line> lines = c.getLines();
222
 
223
		for(Line line : lines){
224
			if(line.getLineStatus() == LineStatus.LINE_ACTIVE){
225
				//line is active
226
				LineItem lineItem = new LineItem();
227
				Item item = null;
228
				try {
229
					item = csc.getClient().getItemByCatalogId(line.getItemId());
230
				} catch (InventoryServiceException e) {
231
					// TODO Auto-generated catch block
232
					e.printStackTrace();
233
				} catch (TException e) {
234
					// TODO Auto-generated catch block
235
					e.printStackTrace();
236
				}
237
				in.shop2020.model.v1.order.Item oitem = new in.shop2020.model.v1.order.Item();
238
				oitem.setId(item.getCatalogItemId());
239
				//get price for it 
240
				long warehouse = getWarehouse();
241
				oitem.setPrice(item.getPrice().get(warehouse));
242
				oitem.setWeight(item.getWeight());
243
				lineItem.setItem(oitem);
244
				lineItem.setAddedOn(pmnt.getBank_ack_timestamp());
245
				orderInfo.addToLineitems(lineItem);
246
			}
247
		}
248
		return t;
249
 
250
	}
251
 
252
	private String getAddress(Set<Address> address, long id){
253
		for(Address a : address){
254
			if(a.getId() == id){
255
				//Prepare String
256
				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();
257
				return add;
258
			}
259
		}
260
		return "";
261
	}
262
 
263
	private long getWarehouse(){
264
		return 1l;
265
	}
266
 
267
	public String getMessage(){
268
		return this.message;
269
	}
270
 
271
	public long getTransactionId(){
272
		return this.transaction_id;
273
	}
274
 
275
 
276
}