Subversion Repositories SmartDukaan

Rev

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

Rev 2199 Rev 2334
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import in.shop2020.config.ConfigException;
3
import in.shop2020.config.ConfigException;
-
 
4
import in.shop2020.datalogger.EventType;
4
import in.shop2020.payments.Attribute;
5
import in.shop2020.payments.Attribute;
5
import in.shop2020.payments.Payment;
6
import in.shop2020.payments.Payment;
6
import in.shop2020.payments.PaymentException;
7
import in.shop2020.payments.PaymentException;
7
import in.shop2020.payments.PaymentService.Client;
8
import in.shop2020.payments.PaymentService.Client;
8
import in.shop2020.payments.PaymentStatus;
9
import in.shop2020.payments.PaymentStatus;
9
import in.shop2020.serving.services.CommonPaymentService;
10
import in.shop2020.serving.services.CommonPaymentService;
-
 
11
import in.shop2020.serving.services.HdfcPaymentService;
-
 
12
import in.shop2020.serving.services.IPaymentService;
-
 
13
import in.shop2020.serving.utils.DataLogger;
10
import in.shop2020.thrift.clients.PaymentServiceClient;
14
import in.shop2020.thrift.clients.PaymentServiceClient;
11
import in.shop2020.thrift.clients.TransactionServiceClient;
15
import in.shop2020.thrift.clients.TransactionServiceClient;
12
import in.shop2020.thrift.clients.UserContextServiceClient;
16
import in.shop2020.thrift.clients.UserContextServiceClient;
13
import in.shop2020.thrift.clients.config.ConfigClient;
17
import in.shop2020.thrift.clients.config.ConfigClient;
14
 
18
 
15
import java.io.IOException;
19
import java.io.IOException;
-
 
20
import java.text.SimpleDateFormat;
-
 
21
import java.util.ArrayList;
-
 
22
import java.util.Date;
-
 
23
import java.util.List;
-
 
24
import java.util.Map;
16
 
25
 
17
import javax.servlet.http.HttpServletRequest;
26
import javax.servlet.http.HttpServletRequest;
18
import javax.servlet.http.HttpServletResponse;
-
 
19
 
27
 
20
import org.apache.struts2.interceptor.ServletRequestAware;
28
import org.apache.struts2.interceptor.ServletRequestAware;
21
import org.apache.struts2.interceptor.ServletResponseAware;
-
 
22
import org.apache.thrift.TException;
29
import org.apache.thrift.TException;
23
import org.apache.log4j.Logger;
30
import org.apache.log4j.Logger;
24
 
31
 
-
 
32
/**
-
 
33
 * This controller processes payment data received on the back channel from
-
 
34
 * HDFC. Since this request doesn't have any cookies associated with it, we
-
 
35
 * can't extend the BaseController which is intercepted by UserInterceptor. Thus
-
 
36
 * to get the parameters from the request, it implements the ServletRequestAware
-
 
37
 * interface.
-
 
38
 * 
-
 
39
 * @author Rajveer, Chandranshu
-
 
40
 * @
-
 
41
 */
25
public class HdfcPayResponseController implements ServletResponseAware, ServletRequestAware{
42
public class HdfcPayResponseController implements ServletRequestAware{
-
 
43
 
-
 
44
	/**
26
	private static final long serialVersionUID = 1L;
45
	 * Enum of all statuses that can be returned by the HDFC gateway
-
 
46
	 * 
-
 
47
	 * @author Chandranshu
27
	
48
	 * 
-
 
49
	 */
28
	private enum PaymentReturnStatus{
50
	private enum PaymentReturnStatus{
-
 
51
		APPROVED("APPROVED"),
-
 
52
		NOT_APPROVED("NOT APPROVED"),
29
		CAPTURED("CAPTURED"),
53
		CAPTURED("CAPTURED"),
30
		NOT_CAPTURED ("NOT CAPTURED"),
54
		NOT_CAPTURED ("NOT CAPTURED"),
31
		CANCELLED ("CANCELLED"),
55
		CANCELLED ("CANCELLED"),
32
		DENIED_BY_RISK("DENIED BY RISK"),
56
		DENIED_BY_RISK("DENIED BY RISK"),
33
		HOST_TIMEOUT("HOST TIMEOUT");
57
		HOST_TIMEOUT("HOST TIMEOUT");
Line 39... Line 63...
39
			return this.value;
63
			return this.value;
40
		}
64
		}
41
	}
65
	}
42
	
66
	
43
	HttpServletRequest request;
67
	HttpServletRequest request;
44
	HttpServletResponse response;
-
 
45
 
68
 
46
	//private static Logger log = LoggerFactory.getLogger(HdfcPayResponseController.class);
69
	//private static Logger log = LoggerFactory.getLogger(HdfcPayResponseController.class);
47
	private static Logger log = Logger.getLogger(Class.class);
70
	private static Logger log = Logger.getLogger(Class.class);
48
 
71
 
49
	public static String successUrl;
72
	public static String successUrl;
Line 73... Line 96...
73
	String redirectUrl;
96
	String redirectUrl;
74
	
97
	
75
	public HdfcPayResponseController() {
98
	public HdfcPayResponseController() {
76
		
99
		
77
	}
100
	}
78
	
101
 
-
 
102
	/**
-
 
103
	 * Sets the redirectUrl instance variable which is used in the view to
-
 
104
	 * redirect the customer to the success or failure page.
-
 
105
	 * 
-
 
106
	 * @return index in all cases.
-
 
107
	 * @throws IOException
-
 
108
	 * @throws SecurityException
-
 
109
	 */
79
	public String create() throws IOException, SecurityException{
110
	public String create() throws IOException, SecurityException{
80
		log.info("Inside hdfc pay response Create");
111
		log.info("Inside hdfc pay response Create");
81
		
112
		
82
		PaymentServiceClient paymentServiceClient = null;
113
		PaymentServiceClient paymentServiceClient = null;
83
		TransactionServiceClient transactionServiceClient = null;
114
		TransactionServiceClient transactionServiceClient = null;
Line 86... Line 117...
86
			paymentServiceClient = new PaymentServiceClient();
117
			paymentServiceClient = new PaymentServiceClient();
87
			transactionServiceClient = new TransactionServiceClient();
118
			transactionServiceClient = new TransactionServiceClient();
88
			userServiceClient = new UserContextServiceClient();
119
			userServiceClient = new UserContextServiceClient();
89
		}catch(Exception e){
120
		}catch(Exception e){
90
			//Nothing to worry. lets move forward
121
			//Nothing to worry. lets move forward
91
			log.error("Unable to initialize one of the clients");
122
			log.error("Unable to initialize one of the clients", e);
92
			e.printStackTrace();
-
 
93
		}
123
		}
94
		
124
		
95
		Long txnId = null;
125
		Long txnId = null;
96
		
126
		
97
		String paymentId = request.getParameter(PAYMENTID);
127
		String paymentId = request.getParameter(PAYMENTID);
Line 104... Line 134...
104
		//FIXME dump them somewhere
134
		//FIXME dump them somewhere
105
		String udf5=request.getParameter(UDF5);
135
		String udf5=request.getParameter(UDF5);
106
		//FIXME hdfc is sending comma separated amount, which is very disappointing. May be we get more surprises moving forward.
136
		//FIXME hdfc is sending comma separated amount, which is very disappointing. May be we get more surprises moving forward.
107
		amount= amount.replace(",", "");
137
		amount= amount.replace(",", "");
108
		
138
		
-
 
139
		//Setting redirect URL to the error URL value by default.
-
 
140
		this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
-
 
141
		
109
		Client paymentClient = paymentServiceClient.getClient();
142
		Client paymentClient = paymentServiceClient.getClient();
-
 
143
		Payment payment = null;
110
		try {
144
		try {
111
			Payment payment = paymentClient.getPayment(merchantPaymentId);
145
			payment = paymentClient.getPayment(merchantPaymentId);
112
			txnId = payment.getMerchantTxnId();
146
			txnId = payment.getMerchantTxnId();
113
			
147
			
114
			if(!validatePaymentParams(paymentId, amount, udf5, payment))
148
			if(!validatePaymentParams(paymentId, amount, udf5, payment))
115
				return "index";
149
				return "index";
116
		} catch (PaymentException e1) {
150
		} catch (PaymentException e1) {
117
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId);
151
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);
118
			e1.printStackTrace();
-
 
119
		} catch (TException e1) {
152
		} catch (TException e1) {
120
			log.error("Thrift exception. Check payment id "+ merchantPaymentId);
153
			log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);
121
			e1.printStackTrace();
-
 
122
		}
154
		}
123
		
155
		
124
		if (result != null && result.trim().equals(PaymentReturnStatus.CAPTURED.value())) {
156
		if (result != null && result.trim().equals(PaymentReturnStatus.APPROVED.value())) {
-
 
157
			log.info("Payment " + merchantPaymentId + " authorized successfully. Updating the database.");
125
			String message = "Payment successful";
158
			String description = "Payment authorized";
126
 
-
 
127
			updatePaymentDetails(merchantPaymentId, message, PaymentStatus.SUCCESS, request, paymentClient);
159
			updatePaymentDetails(merchantPaymentId, description, PaymentStatus.AUTHORIZED, request, paymentClient);
128
			
160
			
-
 
161
			log.info("Capturing payment with id: " + merchantPaymentId);
-
 
162
			Map<String, String> captureResult = HdfcPaymentService.capturePayment(payment, request.getParameter(TRANID));
-
 
163
			String captureStatus = captureResult.get(IPaymentService.STATUS);
-
 
164
			String gatewayStatus = captureResult.get(IPaymentService.GATEWAY_STATUS);
-
 
165
			
-
 
166
			List<Attribute> attributes = new ArrayList<Attribute>();
-
 
167
			if(!captureStatus.trim().equals("0") || !PaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)){
-
 
168
				//Failure
-
 
169
				log.info("Capture attempt failed for payment with id: " + merchantPaymentId);
-
 
170
				description = captureResult.get(IPaymentService.ERROR);
-
 
171
				String errorCode = captureResult.get(IPaymentService.ERR_CODE);				
-
 
172
				
-
 
173
				try {
-
 
174
					paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, paymentId,
-
 
175
							null, null, description, null, null, null, errorCode, PaymentStatus.FAILED, null, attributes);
-
 
176
				} catch (PaymentException e) {
-
 
177
					log.error("Error while updating failed capture payment attempt: ", e);
-
 
178
				} catch (TException e) {
-
 
179
					log.error("Error while updating failed capture payment attempt: ", e);
-
 
180
				}
-
 
181
				//DataLogger.logData(EventType.PAYMENT_FAILURE.name(), session.getId(), Long.toString(userinfo.getUserId()), userinfo.getEmail(), Long.toString(merchantPaymentId), paymentId, gatewayTxnStatus, description, errorCode);
-
 
182
				this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
-
 
183
			}else{
-
 
184
				//Success
-
 
185
				log.info("Capture attempt successful for payment with id: " + merchantPaymentId);
-
 
186
				description = "Payment Captured";
-
 
187
				attributes.add(new Attribute(IPaymentService.CAPTURE_TXN_ID, captureResult.get(IPaymentService.CAPTURE_TXN_ID)));
-
 
188
				attributes.add(new Attribute(IPaymentService.CAPTURE_REF_ID, captureResult.get(IPaymentService.CAPTURE_REF_ID)));
-
 
189
				attributes.add(new Attribute(IPaymentService.CAPTURE_AUTH_ID, captureResult.get(IPaymentService.CAPTURE_AUTH_ID)));
-
 
190
				
-
 
191
				SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
 
192
				attributes.add(new Attribute(IPaymentService.CAPTURE_TIME, captureTimeDateFormatter.format(new Date())));
-
 
193
				
-
 
194
				try {
-
 
195
					paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, paymentId,
-
 
196
							null, gatewayStatus, null, null, null, null, null, PaymentStatus.SUCCESS, null, attributes);
-
 
197
				} catch (PaymentException e) {
-
 
198
					log.error("Error while updating successful capture payment attempt: ", e);
-
 
199
				} catch (TException e) {
-
 
200
					log.error("Error while updating successful capture payment attempt: ", e);
-
 
201
				}
-
 
202
				
129
			CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient);
203
				CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient);
130
 
204
 
131
			this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
205
				this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
-
 
206
				//DataLogger.logData(EventType.PAYMENT_SUCCESS.name(), session.getId(), Long.toString(userinfo.getUserId()), userinfo.getEmail(), Long.toString(merchantPaymentId), paymentId, gatewayTxnStatus, description, captureStatus);
-
 
207
			}			
132
		} else {
208
		} else {
133
			updatePaymentDetails(merchantPaymentId, errorText, PaymentStatus.FAILED, request, paymentClient);
209
			updatePaymentDetails(merchantPaymentId, errorText, PaymentStatus.FAILED, request, paymentClient);
134
 
210
 
135
			CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
211
			CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
136
 
212
 
Line 172... Line 248...
172
		String sessionId = request.getSession().getId();
248
		String sessionId = request.getSession().getId();
173
		String errorNo = request.getParameter(ERROR);
249
		String errorNo = request.getParameter(ERROR);
174
		try {
250
		try {
175
			paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, status, postdate, null);
251
			paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, status, postdate, null);
176
		} catch (PaymentException e1) {
252
		} catch (PaymentException e1) {
177
			log.error("Unable to update payment details in our database." + e1.getError_code() + e1.getMessage());
253
			log.error("Unable to update payment details in our database.", e1);
178
			e1.printStackTrace();
-
 
179
		} catch (TException e1) {
254
		} catch (TException e1) {
180
			log.error("Unable to update payment details in our database. Thrift exception.");
255
			log.error("Unable to update payment details in our database. Thrift exception.", e1);
181
			e1.printStackTrace();
-
 
182
		}
256
		}
183
	}
257
	}
184
	
258
	
185
	public String getRedirectUrl(){
259
	public String getRedirectUrl(){
186
		return this.redirectUrl;
260
		return this.redirectUrl;
Line 188... Line 262...
188
 
262
 
189
	@Override
263
	@Override
190
	public void setServletRequest(HttpServletRequest request) {
264
	public void setServletRequest(HttpServletRequest request) {
191
		this.request = request;
265
		this.request = request;
192
		for(Object param: request.getParameterMap().keySet()){
266
		for(Object param: request.getParameterMap().keySet()){
193
			System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));
267
			log.info("PARAMS: " + param + " = "+ request.getParameter((String)param));
194
		}
268
		}
195
	}
269
	}
196
 
-
 
197
	@Override
-
 
198
	public void setServletResponse(HttpServletResponse response) {
-
 
199
		this.response = response;
-
 
200
	}
-
 
201
}
270
}
202
271