Subversion Repositories SmartDukaan

Rev

Rev 2457 | Rev 3126 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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