Subversion Repositories SmartDukaan

Rev

Rev 1555 | Rev 2134 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1555 Rev 1905
Line 10... Line 10...
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.PaymentService.Client;
14
import in.shop2020.payments.PaymentStatus;
14
import in.shop2020.payments.PaymentStatus;
-
 
15
import in.shop2020.serving.services.CommonPaymentService;
15
import in.shop2020.thrift.clients.PaymentServiceClient;
16
import in.shop2020.thrift.clients.PaymentServiceClient;
16
import in.shop2020.thrift.clients.TransactionServiceClient;
17
import in.shop2020.thrift.clients.TransactionServiceClient;
17
import in.shop2020.thrift.clients.UserContextServiceClient;
18
import in.shop2020.thrift.clients.UserContextServiceClient;
18
import in.shop2020.thrift.clients.config.ConfigClient;
19
import in.shop2020.thrift.clients.config.ConfigClient;
19
 
20
 
Line 64... Line 65...
64
	public static String PAYMENTID = "paymentid";
65
	public static String PAYMENTID = "paymentid";
65
	public static String REF = "ref";
66
	public static String REF = "ref";
66
	public static String POSTDATE = "postdate";
67
	public static String POSTDATE = "postdate";
67
	public static String ERROR = "Error";
68
	public static String ERROR = "Error";
68
	public static String ERRORTEXT = "ErrorText";
69
	public static String ERRORTEXT = "ErrorText";
69
	public static String UDF5 = "udf5";
70
	public static String UDF5 = "udf5";	
70
	String redirectUrl;
-
 
71
	String amount;
-
 
72
	String trackId;
-
 
73
	String result;
-
 
74
	String postdate;
-
 
75
	String auth;
-
 
76
	String ref;
-
 
77
	String tranId;
-
 
78
	String paymentId;
-
 
79
	String sessionId;
-
 
80
	String errorText;
-
 
81
	String errorNo;
-
 
82
	long txnId;
-
 
83
	long merchantPaymentId;
-
 
84
	Transaction transaction = null;
-
 
85
	
-
 
86
 
71
 
87
	static{
72
	static{
88
		try {
73
		try {
89
			successUrl = ConfigClient.getClient().get("payment_success_url");
74
			successUrl = ConfigClient.getClient().get("payment_success_url");
90
			errorUrl = ConfigClient.getClient().get("payment_error_url");
75
			errorUrl = ConfigClient.getClient().get("payment_error_url");
91
		} catch (ConfigException e) {
76
		} catch (ConfigException e) {
92
			log.error("Unable to get success and error usr info from config server.");
77
			log.error("Unable to get success and error usr info from config server.");
93
		}
78
		}
94
	}
79
	}
95
	
80
	
-
 
81
	String redirectUrl;
96
	
82
	
97
	public HdfcPayResponseController() {
83
	public HdfcPayResponseController() {
98
		
84
		
99
	}
85
	}
100
	
86
	
Line 112... Line 98...
112
			//Nothing to worry. lets move forward
98
			//Nothing to worry. lets move forward
113
			log.error("Unable to initialize one of the clients");
99
			log.error("Unable to initialize one of the clients");
114
			e.printStackTrace();
100
			e.printStackTrace();
115
		}
101
		}
116
		
102
		
117
		result = this.request.getParameter(RESULT);
-
 
118
		postdate = this.request.getParameter(POSTDATE);
103
		Long txnId = null;
-
 
104
		
119
		tranId = this.request.getParameter(TRANID);
105
		String paymentId = request.getParameter(PAYMENTID);
120
		auth = this.request.getParameter(AUTH);
-
 
121
		ref = this.request.getParameter(REF);
-
 
122
		amount = this.request.getParameter(AMOUNT);
106
		String result = request.getParameter(RESULT);
123
		paymentId = this.request.getParameter(PAYMENTID);
-
 
124
		trackId = this.request.getParameter(TRACKID);
107
		String trackId = request.getParameter(TRACKID);
125
 
-
 
126
		merchantPaymentId = Long.parseLong(trackId);
108
		long merchantPaymentId = Long.parseLong(trackId);
127
		sessionId = this.request.getSession().getId();
-
 
128
		errorNo = request.getParameter(ERROR);
109
		String amount = request.getParameter(AMOUNT);
129
		errorText = request.getParameter(ERRORTEXT);
110
		String errorText = request.getParameter(ERRORTEXT);
-
 
111
		
130
		//FIXME dump them somewhere
112
		//FIXME dump them somewhere
131
		String udf1=request.getParameter("udf1");
-
 
132
		String udf2=request.getParameter("udf2");
-
 
133
		String udf3=request.getParameter("udf3");
-
 
134
		String udf4=request.getParameter("udf4");
-
 
135
		String udf5=request.getParameter(UDF5);
113
		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.
114
		//FIXME hdfc is sending comma separated amount, which is very disappointing. May be we get more surprises moving forward.
137
		amount= amount.replace(",", "");
115
		amount= amount.replace(",", "");
138
		
116
		
139
		Client paymentClient = paymentServiceClient.getClient();
117
		Client paymentClient = paymentServiceClient.getClient();
140
		in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
-
 
141
		try {
118
		try {
142
			Payment payment = paymentClient.getPayment(merchantPaymentId);
119
			Payment payment = paymentClient.getPayment(merchantPaymentId);
143
			String dbUdf5="";
-
 
144
			double dbAmount = payment.getAmount();
120
			txnId = payment.getMerchantTxnId();
145
			for(Attribute attribute: payment.getAttributes()){
-
 
146
				if(attribute.getName().trim().equalsIgnoreCase(UDF5)){
-
 
147
					dbUdf5 = attribute.getValue();
-
 
148
				}
-
 
149
			}
121
			
150
			// verify 3 things:  udf5, amount and paymentid
122
			if(!validatePaymentParams(paymentId, amount, udf5, payment))
151
			//FIXME should we first dump the data and than verify these things. ?? 
-
 
152
			log.info(paymentId+ ":"+ payment.getGatewayPaymentId() + "\n" + Double.parseDouble(amount) + ":" + dbAmount + "\n" + dbUdf5 + ":" + udf5 );
-
 
153
			if(!(paymentId.equalsIgnoreCase(payment.getGatewayPaymentId()) && dbAmount == Double.parseDouble(amount) && udf5.equalsIgnoreCase(dbUdf5))){
-
 
154
				log.error("Checks and balance failed on returned date");
-
 
155
				this.redirectUrl =  errorUrl + "?paymentId="+merchantPaymentId;
-
 
156
				return "index";
123
				return "index";
157
			}
-
 
158
		} catch (PaymentException e1) {
124
		} catch (PaymentException e1) {
159
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId);
125
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId);
160
			e1.printStackTrace();
126
			e1.printStackTrace();
161
		} catch (TException e1) {
127
		} catch (TException e1) {
162
			log.error("Thrift exception. Check payment id "+ merchantPaymentId);
128
			log.error("Thrift exception. Check payment id "+ merchantPaymentId);
163
			e1.printStackTrace();
129
			e1.printStackTrace();
164
		}
130
		}
165
		
131
		
166
		if(result != null && result.trim().equals(PaymentReturnStatus.CAPTURED.value())){
132
		if (result != null && result.trim().equals(PaymentReturnStatus.CAPTURED.value())) {
167
			String message = "Payment successful";
133
			String message = "Payment successful";
-
 
134
 
-
 
135
			updatePaymentDetails(merchantPaymentId, message, PaymentStatus.SUCCESS, request, paymentClient);
168
			
136
			
169
			try {
-
 
170
				paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, PaymentStatus.SUCCESS, postdate, null);
-
 
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();
-
 
177
			}
-
 
178
			
-
 
179
			Map<Long,Double> items = new HashMap<Long, Double>();
-
 
180
			try {
-
 
181
				txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
137
			CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient);
182
				transactionClient.changeTransactionStatus(txnId, TransactionStatus.IN_PROCESS, "Payment received for the order");
-
 
183
				transaction = transactionClient.getTransaction(txnId);
-
 
184
				for(Order order: transaction.getOrders()){
-
 
185
					for(LineItem lineitem: order.getLineitems()){
-
 
186
						Long itemId = lineitem.getItem_id();
-
 
187
						Double quantity = items.get(itemId);
-
 
188
						if(quantity==null){
-
 
189
							quantity = lineitem.getQuantity();
-
 
190
						}
-
 
191
						else{
-
 
192
							quantity= quantity + lineitem.getQuantity();
-
 
193
						}
-
 
194
						items.put(itemId, quantity);
-
 
195
					}
-
 
196
				}	
-
 
197
			} catch (PaymentException e1) {
-
 
198
				log.error("Unable to get transaction id from payment service." + e1.getError_code() + e1.getMessage());
-
 
199
				e1.printStackTrace();
-
 
200
			} catch (TException e1) {
-
 
201
				log.error("Unable to get transaction id from payment service. Thrift Exception" + e1.getMessage());
-
 
202
				e1.printStackTrace();
-
 
203
			} catch (TransactionServiceException e) {
-
 
204
				log.error("Unable to get transaction id from payment service. Thrift Exception" + e.getErrorCode() + e.getMessage());
-
 
205
				e.printStackTrace();
-
 
206
			}
-
 
207
			
-
 
208
			
-
 
209
			
138
 
210
			try {
-
 
211
				//TODO Optimize the function to send less number of data over network
-
 
212
				System.out.println("Transaction shopping cart id is: " + transaction.getShoppingCartid());
-
 
213
				System.out.println("Items to restr in cart are: " + items );
-
 
214
				log.info("Transaction shopping cart id is: " + transaction.getShoppingCartid());
139
			CommonPaymentService.sendTxnEmail(txnId, transactionServiceClient);
215
				log.info("Items to restr in cart are: " + items );
-
 
216
				userServiceClient.getClient().resetCart(transaction.getShoppingCartid(), items);	
-
 
217
			}catch (TException e) {
-
 
218
				log.error("Error while updating information in payment database.");
-
 
219
				e.printStackTrace();
-
 
220
				//FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
-
 
221
				
-
 
222
			} catch (ShoppingCartException e) {
-
 
223
				log.error("Error while reseting the cart in cart database.");
-
 
224
				e.printStackTrace();
-
 
225
			}
-
 
226
			
-
 
227
			// Enqueue the email, containing transaction info, to be sent later by batch job  
-
 
228
			try {
-
 
229
				transactionClient.enqueueTransactionInfoEmail(txnId);
-
 
230
			} catch (Exception e) {
-
 
231
				log.error("Error while adding email to depatch queue.");
-
 
232
				e.printStackTrace();
-
 
233
			}
-
 
234
			
140
 
235
			this.redirectUrl = successUrl + "?paymentId="+merchantPaymentId;
141
			this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
236
		}
-
 
237
		else	{
142
		} else {
238
			try {
-
 
239
				paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, errorText, tranId, auth, ref, errorNo, PaymentStatus.FAILED, postdate, null);
143
			updatePaymentDetails(merchantPaymentId, errorText, PaymentStatus.FAILED, request, paymentClient);
240
			} catch (PaymentException e1) {
-
 
241
				log.error("Unable to update payment details in our database." + e1.getError_code() + e1.getMessage());
-
 
242
				e1.printStackTrace();
-
 
243
			} catch (TException e1) {
-
 
244
				log.error("Unable to update payment details in our database. Thrift exception.");
-
 
245
				e1.printStackTrace();
-
 
246
			}
-
 
247
			
144
 
248
			try{
-
 
249
				txnId = paymentClient.getPayment(merchantPaymentId).getMerchantTxnId();
-
 
250
				transactionClient.changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
-
 
251
			}catch(TException e){
-
 
252
				log.error("Thrift exception while getting information from transaction service.");
145
			CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
253
				e.printStackTrace();
-
 
254
			} catch (PaymentException e) {
-
 
255
				log.error("Error while getting information from merchant.");
-
 
256
				e.printStackTrace();
-
 
257
			} catch (TransactionServiceException e) {
-
 
258
				log.error("Error while updating status information in transaction database.");
-
 
259
				e.printStackTrace();
-
 
260
			}
-
 
261
			
146
 
262
			this.redirectUrl = errorUrl + "?paymentId="+merchantPaymentId;
147
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
263
		}
148
		}
264
		
149
		
265
		return "index";
150
		return "index";
266
	}
151
	}
-
 
152
 
-
 
153
	private boolean validatePaymentParams(String paymentId,	String amount, String udf5, Payment payment) {
-
 
154
		long merchantPaymentId = payment.getPaymentId();
-
 
155
		String dbUdf5="";
-
 
156
		double dbAmount = payment.getAmount();
-
 
157
		for(Attribute attribute: payment.getAttributes()){
-
 
158
			if(attribute.getName().trim().equalsIgnoreCase(UDF5)){
-
 
159
				dbUdf5 = attribute.getValue();
-
 
160
			}
-
 
161
		}
-
 
162
		// verify 3 things:  udf5, amount and paymentid
-
 
163
		//FIXME should we first dump the data and then verify these things. ?? 
-
 
164
		log.info(paymentId+ ":"+ payment.getGatewayPaymentId() + "\n" + Double.parseDouble(amount) + ":" + dbAmount + "\n" + dbUdf5 + ":" + udf5 );
-
 
165
		if(!(paymentId.equalsIgnoreCase(payment.getGatewayPaymentId()) && dbAmount == Double.parseDouble(amount) && udf5.equalsIgnoreCase(dbUdf5))){
-
 
166
			log.error("Checks and balance failed on returned data");
-
 
167
			this.redirectUrl =  errorUrl + "?paymentId="+merchantPaymentId;
-
 
168
			return false;
-
 
169
		}
-
 
170
		return true;
-
 
171
	}
-
 
172
 
-
 
173
	private void updatePaymentDetails(long merchantPaymentId, String message, PaymentStatus status, HttpServletRequest req, Client paymentClient) {
-
 
174
		String paymentId = request.getParameter(PAYMENTID);
-
 
175
		String result = request.getParameter(RESULT);
-
 
176
		String postdate = request.getParameter(POSTDATE);
-
 
177
		String tranId = request.getParameter(TRANID);
-
 
178
		String auth = request.getParameter(AUTH);
-
 
179
		String ref = request.getParameter(REF);
-
 
180
		
-
 
181
		String sessionId = request.getSession().getId();
-
 
182
		String errorNo = request.getParameter(ERROR);
-
 
183
		try {
-
 
184
			paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, status, postdate, null);
-
 
185
		} catch (PaymentException e1) {
-
 
186
			log.error("Unable to update payment details in our database." + e1.getError_code() + e1.getMessage());
-
 
187
			e1.printStackTrace();
-
 
188
		} catch (TException e1) {
-
 
189
			log.error("Unable to update payment details in our database. Thrift exception.");
-
 
190
			e1.printStackTrace();
-
 
191
		}
267
	
192
	}
268
	
193
	
269
	public String getRedirectUrl(){
194
	public String getRedirectUrl(){
270
		return this.redirectUrl;
195
		return this.redirectUrl;
271
	}
196
	}
272
 
197