Subversion Repositories SmartDukaan

Rev

Rev 889 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 889 Rev 894
Line 8... Line 8...
8
import in.shop2020.model.v1.order.TransactionStatus;
8
import in.shop2020.model.v1.order.TransactionStatus;
9
import in.shop2020.model.v1.user.ShoppingCartException;
9
import in.shop2020.model.v1.user.ShoppingCartException;
10
import in.shop2020.payments.Attribute;
10
import in.shop2020.payments.Attribute;
11
import in.shop2020.payments.Payment;
11
import in.shop2020.payments.Payment;
12
import in.shop2020.payments.PaymentException;
12
import in.shop2020.payments.PaymentException;
-
 
13
import in.shop2020.payments.PaymentService.Client;
13
import in.shop2020.payments.PaymentStatus;
14
import in.shop2020.payments.PaymentStatus;
14
import in.shop2020.thrift.clients.PaymentServiceClient;
15
import in.shop2020.thrift.clients.PaymentServiceClient;
15
import in.shop2020.thrift.clients.TransactionServiceClient;
16
import in.shop2020.thrift.clients.TransactionServiceClient;
16
import in.shop2020.thrift.clients.UserContextServiceClient;
17
import in.shop2020.thrift.clients.UserContextServiceClient;
17
import in.shop2020.thrift.clients.config.ConfigClient;
18
import in.shop2020.thrift.clients.config.ConfigClient;
18
import in.shop2020.utils.Logger;
-
 
19
 
19
 
20
import java.io.IOException;
20
import java.io.IOException;
21
import java.util.HashMap;
21
import java.util.HashMap;
22
import java.util.Map;
22
import java.util.Map;
23
 
23
 
Line 49... Line 49...
49
	}
49
	}
50
	
50
	
51
	HttpServletRequest request;
51
	HttpServletRequest request;
52
	HttpServletResponse response;
52
	HttpServletResponse response;
53
 
53
 
54
	PaymentServiceClient pclient = null;
-
 
55
	TransactionServiceClient tsc = null;
-
 
56
	UserContextServiceClient usc = null;
-
 
57
	private static Log log = LogFactory.getLog(HdfcPayResponseController.class);
54
	private static Log log = LogFactory.getLog(HdfcPayResponseController.class);
58
 
55
 
-
 
56
	public static String successUrl;
-
 
57
	public static String errorUrl;
-
 
58
	
59
	public static String AMOUNT = "amt";
59
	public static String AMOUNT = "amt";
60
	public static String TRACKID = "trackid";
60
	public static String TRACKID = "trackid";
61
	public static String RESULT = "result";
61
	public static String RESULT = "result";
62
	public static String AUTH = "auth";
62
	public static String AUTH = "auth";
63
	public static String TRANID = "tranid";
63
	public static String TRANID = "tranid";
Line 81... Line 81...
81
	String errorNo;
81
	String errorNo;
82
	long txnId;
82
	long txnId;
83
	long merchantPaymentId;
83
	long merchantPaymentId;
84
	Transaction transaction = null;
84
	Transaction transaction = null;
85
	
85
	
-
 
86
 
86
	public HdfcPayResponseController() {
87
	static{
87
		try {
88
		try {
88
			pclient = new PaymentServiceClient();
89
			successUrl = ConfigClient.getClient().get("payment_success_url");
89
			tsc = new TransactionServiceClient();
-
 
90
			usc = new UserContextServiceClient();
90
			errorUrl = ConfigClient.getClient().get("payment_error_url");
91
		} catch (Exception e) {
91
		} catch (ConfigException e) {
92
			Logger.log("Could not initialize the paymentservice client", this);
92
			log.error("Unable to get success and error usr info from config server.");
93
		}
93
		}
94
	}
94
	}
95
	
95
	
-
 
96
	
-
 
97
	public HdfcPayResponseController() {
-
 
98
		
-
 
99
	}
-
 
100
	
96
	public String create() throws IOException, SecurityException{
101
	public String create() throws IOException, SecurityException{
97
		System.out.println("Inside hdfc pay response Create");
102
		log.info("Inside hdfc pay response Create");
-
 
103
		
-
 
104
		PaymentServiceClient paymentServiceClient = null;
-
 
105
		TransactionServiceClient transactionServiceClient = null;
-
 
106
		UserContextServiceClient userServiceClient = null;
-
 
107
		try{
-
 
108
			paymentServiceClient = new PaymentServiceClient();
-
 
109
			transactionServiceClient = new TransactionServiceClient();
-
 
110
			userServiceClient = new UserContextServiceClient();
-
 
111
		}catch(Exception e){
-
 
112
			//Nothing to worry. lets move forward
-
 
113
			log.error("Unable to initialize one of the clients");
-
 
114
			e.printStackTrace();
-
 
115
		}
98
		
116
		
99
		result = this.request.getParameter(RESULT);
117
		result = this.request.getParameter(RESULT);
100
		postdate = this.request.getParameter(POSTDATE);
118
		postdate = this.request.getParameter(POSTDATE);
101
		tranId = this.request.getParameter(TRANID);
119
		tranId = this.request.getParameter(TRANID);
102
		auth = this.request.getParameter(AUTH);
120
		auth = this.request.getParameter(AUTH);
Line 108... Line 126...
108
		merchantPaymentId = Long.parseLong(trackId);
126
		merchantPaymentId = Long.parseLong(trackId);
109
		sessionId = this.request.getSession().getId();
127
		sessionId = this.request.getSession().getId();
110
		errorNo = request.getParameter(ERROR);
128
		errorNo = request.getParameter(ERROR);
111
		errorText = request.getParameter(ERRORTEXT);
129
		errorText = request.getParameter(ERRORTEXT);
112
		//FIXME dump them somewhere
130
		//FIXME dump them somewhere
113
		/*
-
 
114
		String udf1=request.getParameter("udf1");
131
		String udf1=request.getParameter("udf1");
115
		String udf2=request.getParameter("udf2");
132
		String udf2=request.getParameter("udf2");
116
		String udf3=request.getParameter("udf3");
133
		String udf3=request.getParameter("udf3");
117
		String udf4=request.getParameter("udf4");
134
		String udf4=request.getParameter("udf4");
118
		*/
-
 
119
		String udf5=request.getParameter(UDF5);
135
		String udf5=request.getParameter(UDF5);
-
 
136
		//FIXME hdfc is sending comma separated amount, which is very disappointing. May be we get more surprises moving forward.
120
		amount= amount.replace(",", "");
137
		amount= amount.replace(",", "");
-
 
138
		
121
		Payment payment = null;
139
		Client paymentClient = paymentServiceClient.getClient();
-
 
140
		in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
122
		try {
141
		try {
123
			payment = pclient.getClient().getPayment(merchantPaymentId);
142
			Payment payment = paymentClient.getPayment(merchantPaymentId);
124
			String dbUdf5="";
143
			String dbUdf5="";
125
			double dbAmount = payment.getAmount();
144
			double dbAmount = payment.getAmount();
126
			for(Attribute attribute: payment.getAttributes()){
145
			for(Attribute attribute: payment.getAttributes()){
127
				if(attribute.getName().trim().equalsIgnoreCase(UDF5)){
146
				if(attribute.getName().trim().equalsIgnoreCase(UDF5)){
128
					dbUdf5 = attribute.getValue();
147
					dbUdf5 = attribute.getValue();
129
				}
148
				}
130
			}
149
			}
131
			// verify 3 things:  udf5, amount and paymentid
150
			// verify 3 things:  udf5, amount and paymentid
-
 
151
			//FIXME should we first dump the data and than verify these things. ?? 
132
			log.info(paymentId+ ":"+ payment.getGatewayPaymentId() + "\n" + Double.parseDouble(amount) + ":" + dbAmount + "\n" + dbUdf5 + ":" + udf5 );
152
			log.info(paymentId+ ":"+ payment.getGatewayPaymentId() + "\n" + Double.parseDouble(amount) + ":" + dbAmount + "\n" + dbUdf5 + ":" + udf5 );
133
			if(!(paymentId.equalsIgnoreCase(payment.getGatewayPaymentId()) && dbAmount == Double.parseDouble(amount) && udf5.equalsIgnoreCase(dbUdf5))){
153
			if(!(paymentId.equalsIgnoreCase(payment.getGatewayPaymentId()) && dbAmount == Double.parseDouble(amount) && udf5.equalsIgnoreCase(dbUdf5))){
134
				log.error("Checks and balance failed on returned date");
154
				log.error("Checks and balance failed on returned date");
135
				this.redirectUrl = ConfigClient.getClient().get("payment_error_url") + "?paymentId="+merchantPaymentId;
155
				this.redirectUrl =  errorUrl + "?paymentId="+merchantPaymentId;
136
				return "index";
156
				return "index";
137
			}
157
			}
138
		} catch (PaymentException e1) {
158
		} catch (PaymentException e1) {
139
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId);
159
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId);
140
			e1.printStackTrace();
160
			e1.printStackTrace();
141
		} catch (TException e1) {
161
		} catch (TException e1) {
142
			log.error("Thrift exception. Check payment id "+ merchantPaymentId);
162
			log.error("Thrift exception. Check payment id "+ merchantPaymentId);
143
			e1.printStackTrace();
163
			e1.printStackTrace();
144
		} catch (ConfigException e) {
-
 
145
			this.redirectUrl = "http://173.230.147.250:8080/pay-error?paymentId="+merchantPaymentId;
-
 
146
			log.error("Unable to get error url info from Config server. Falling back to default url.");
-
 
147
			e.printStackTrace();
-
 
148
			return "index";
-
 
149
		}
164
		}
150
		
165
		
151
		if(result.trim().equals(PaymentReturnStatus.CAPTURED.value())){
166
		if(result.trim().equals(PaymentReturnStatus.CAPTURED.value())){
-
 
167
			String message = "Payment successful";
-
 
168
			
152
			try {
169
			try {
-
 
170
				paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, PaymentStatus.SUCCESS, null);
153
				String message = "Payment successful";
171
			} catch (PaymentException e1) {
-
 
172
				log.error("Unable to update payment details in our database." + e1.getError_code() + e1.getMessage());
-
 
173
				e1.printStackTrace();
-
 
174
			} catch (TException e1) {
-
 
175
				log.error("Unable to update payment details in our database. Thrift exception.");
-
 
176
				e1.printStackTrace();
154
				
177
			}
155
				
178
			
156
				pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, PaymentStatus.SUCCESS, null);
179
			Map<Long,Double> items = new HashMap<Long, Double>();
-
 
180
			try {
157
				txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
181
				txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
158
				
-
 
159
				tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.IN_PROCESS, "Payment received for the order");
182
				transactionClient.changeTransactionStatus(txnId, TransactionStatus.IN_PROCESS, "Payment received for the order");
160
				
-
 
161
		    	transaction = tsc.getClient().getTransaction(txnId);
183
				transaction = transactionClient.getTransaction(txnId);
162
		    	
-
 
163
		    	Map<Long,Double> items = new HashMap<Long, Double>();
-
 
164
		    	for(Order order: transaction.getOrders()){
184
				for(Order order: transaction.getOrders()){
165
					for(LineItem lineitem: order.getLineitems()){
185
					for(LineItem lineitem: order.getLineitems()){
166
						Long itemId = lineitem.getItem_id();
186
						Long itemId = lineitem.getItem_id();
167
						Double quantity = items.get(itemId);
187
						Double quantity = items.get(itemId);
168
						if(quantity!=null)
188
						if(quantity!=null)
169
							quantity = quantity + lineitem.getQuantity();
189
							quantity = quantity + lineitem.getQuantity();
170
						items.put(itemId, quantity);
190
						items.put(itemId, quantity);
171
					}
191
					}
172
				}
192
				}	
-
 
193
			} catch (PaymentException e1) {
-
 
194
				log.error("Unable to get transaction id from payment service." + e1.getError_code() + e1.getMessage());
-
 
195
				e1.printStackTrace();
-
 
196
			} catch (TException e1) {
-
 
197
				log.error("Unable to get transaction id from payment service. Thrift Exception" + e1.getMessage());
-
 
198
				e1.printStackTrace();
-
 
199
			} catch (TransactionServiceException e) {
-
 
200
				log.error("Unable to get transaction id from payment service. Thrift Exception" + e.getErrorCode() + e.getMessage());
-
 
201
				e.printStackTrace();
-
 
202
			}
-
 
203
			
-
 
204
			
-
 
205
			
-
 
206
			try {
173
				//TODO Optimize the function to send less number of data over network
207
				//TODO Optimize the function to send less number of data over network
174
				usc.getClient().resetCart(transaction.getShoppingCartid(), items);
208
				userServiceClient.getClient().resetCart(transaction.getShoppingCartid(), items);	
175
				
-
 
176
			}catch (TException e) {
209
			}catch (TException e) {
177
				log.error("Error while updating information in payment database.");
210
				log.error("Error while updating information in payment database.");
178
				e.printStackTrace();
211
				e.printStackTrace();
179
				//FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
212
				//FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
180
				
213
				
181
			} catch (ShoppingCartException e) {
214
			} catch (ShoppingCartException e) {
182
				log.error("Error while updating information in cart database.");
215
				log.error("Error while reseting the cart in cart database.");
183
				e.printStackTrace();
-
 
184
			} catch (PaymentException e) {
-
 
185
				log.error("Error while updating information in payment database.");
-
 
186
				e.printStackTrace();			
-
 
187
			} catch (TransactionServiceException e) {
-
 
188
				log.error("Error while updating information in transaction database.");
-
 
189
				e.printStackTrace();
216
				e.printStackTrace();
190
			}
217
			}
191
			try{
218
			
192
				this.redirectUrl = ConfigClient.getClient().get("payment_success_url") + "?paymentId="+merchantPaymentId;
219
			this.redirectUrl = successUrl + "?paymentId="+merchantPaymentId;
193
			}catch(Exception ex){
-
 
194
				this.redirectUrl = "http://173.230.147.250:8080/pay-success?paymentId="+merchantPaymentId;	
-
 
195
			}
-
 
196
			
220
			
197
		}else{
221
		}else{
-
 
222
			
-
 
223
			try {
-
 
224
				paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, errorText, tranId, auth, ref, errorNo, PaymentStatus.FAILED, null);
-
 
225
			} catch (PaymentException e1) {
-
 
226
				log.error("Unable to update payment details in our database." + e1.getError_code() + e1.getMessage());
-
 
227
				e1.printStackTrace();
-
 
228
			} catch (TException e1) {
-
 
229
				log.error("Unable to update payment details in our database. Thrift exception.");
-
 
230
				e1.printStackTrace();
-
 
231
			}
-
 
232
			
198
			try{
233
			try{
199
				pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, errorText, tranId, auth, ref, errorNo, PaymentStatus.FAILED, null);
-
 
200
				txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
234
				txnId = paymentClient.getPayment(merchantPaymentId).getMerchantTxnId();
201
				tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
235
				transactionClient.changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
202
			}catch(TException e){
236
			}catch(TException e){
203
				log.error("Error while updating information.");
237
				log.error("Thrift exception while getting information from transaction service.");
204
				e.printStackTrace();
238
				e.printStackTrace();
205
				//FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
-
 
206
			} catch (PaymentException e) {
239
			} catch (PaymentException e) {
207
				log.error("Error while updating information in payment database.");
240
				log.error("Error while getting information from merchant.");
208
				e.printStackTrace();
241
				e.printStackTrace();
209
			} catch (TransactionServiceException e) {
242
			} catch (TransactionServiceException e) {
210
				log.error("Error while updating information in transaction database.");
243
				log.error("Error while updating status information in transaction database.");
211
				e.printStackTrace();
244
				e.printStackTrace();
212
			}
245
			}
213
			
246
			
214
			try{
-
 
215
				this.redirectUrl = ConfigClient.getClient().get("payment_error_url") + "?paymentId="+merchantPaymentId;
247
			this.redirectUrl = errorUrl + "?paymentId="+merchantPaymentId;
216
			}catch(Exception ex){
-
 
217
				this.redirectUrl = "http://173.230.147.250:8080/pay-error?paymentId="+merchantPaymentId;	
-
 
218
			}
-
 
219
			
-
 
220
		}
248
		}
221
		
249
		
222
		return "index";
250
		return "index";
223
	}
251
	}
224
	
252
	
Line 231... Line 259...
231
	public void setServletRequest(HttpServletRequest request) {
259
	public void setServletRequest(HttpServletRequest request) {
232
		this.request = request;
260
		this.request = request;
233
		for(Object param: request.getParameterMap().keySet()){
261
		for(Object param: request.getParameterMap().keySet()){
234
			System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));
262
			System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));
235
		}
263
		}
236
		
-
 
237
	}
264
	}
238
 
265
 
239
	@Override
266
	@Override
240
	public void setServletResponse(HttpServletResponse response) {
267
	public void setServletResponse(HttpServletResponse response) {
241
		this.response = response;
268
		this.response = response;