Subversion Repositories SmartDukaan

Rev

Rev 2145 | Rev 2334 | 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;
719 rajveer 10
import in.shop2020.thrift.clients.PaymentServiceClient;
11
import in.shop2020.thrift.clients.TransactionServiceClient;
12
import in.shop2020.thrift.clients.UserContextServiceClient;
13
import in.shop2020.thrift.clients.config.ConfigClient;
14
 
15
import java.io.IOException;
16
 
17
import javax.servlet.http.HttpServletRequest;
18
import javax.servlet.http.HttpServletResponse;
19
 
20
import org.apache.struts2.interceptor.ServletRequestAware;
21
import org.apache.struts2.interceptor.ServletResponseAware;
741 rajveer 22
import org.apache.thrift.TException;
1044 chandransh 23
import org.apache.log4j.Logger;
719 rajveer 24
 
25
public class HdfcPayResponseController implements ServletResponseAware, ServletRequestAware{
26
	private static final long serialVersionUID = 1L;
27
 
28
	private enum PaymentReturnStatus{
29
		CAPTURED("CAPTURED"),
30
		NOT_CAPTURED ("NOT CAPTURED"),
31
		CANCELLED ("CANCELLED"),
32
		DENIED_BY_RISK("DENIED BY RISK"),
33
		HOST_TIMEOUT("HOST TIMEOUT");
34
		private String value;
35
		PaymentReturnStatus(String value) {
36
			this.value = value;
37
		}
38
		public String value(){
39
			return this.value;
40
		}
41
	}
42
 
43
	HttpServletRequest request;
44
	HttpServletResponse response;
45
 
1044 chandransh 46
	//private static Logger log = LoggerFactory.getLogger(HdfcPayResponseController.class);
47
	private static Logger log = Logger.getLogger(Class.class);
719 rajveer 48
 
894 rajveer 49
	public static String successUrl;
50
	public static String errorUrl;
51
 
719 rajveer 52
	public static String AMOUNT = "amt";
53
	public static String TRACKID = "trackid";
54
	public static String RESULT = "result";
55
	public static String AUTH = "auth";
56
	public static String TRANID = "tranid";
57
	public static String PAYMENTID = "paymentid";
58
	public static String REF = "ref";
59
	public static String POSTDATE = "postdate";
60
	public static String ERROR = "Error";
61
	public static String ERRORTEXT = "ErrorText";
1905 chandransh 62
	public static String UDF5 = "udf5";	
894 rajveer 63
 
64
	static{
719 rajveer 65
		try {
894 rajveer 66
			successUrl = ConfigClient.getClient().get("payment_success_url");
67
			errorUrl = ConfigClient.getClient().get("payment_error_url");
68
		} catch (ConfigException e) {
69
			log.error("Unable to get success and error usr info from config server.");
719 rajveer 70
		}
71
	}
72
 
1905 chandransh 73
	String redirectUrl;
894 rajveer 74
 
75
	public HdfcPayResponseController() {
76
 
77
	}
78
 
719 rajveer 79
	public String create() throws IOException, SecurityException{
894 rajveer 80
		log.info("Inside hdfc pay response Create");
719 rajveer 81
 
894 rajveer 82
		PaymentServiceClient paymentServiceClient = null;
83
		TransactionServiceClient transactionServiceClient = null;
84
		UserContextServiceClient userServiceClient = null;
85
		try{
86
			paymentServiceClient = new PaymentServiceClient();
87
			transactionServiceClient = new TransactionServiceClient();
88
			userServiceClient = new UserContextServiceClient();
89
		}catch(Exception e){
90
			//Nothing to worry. lets move forward
91
			log.error("Unable to initialize one of the clients");
92
			e.printStackTrace();
93
		}
94
 
1905 chandransh 95
		Long txnId = null;
96
 
97
		String paymentId = request.getParameter(PAYMENTID);
98
		String result = request.getParameter(RESULT);
99
		String trackId = request.getParameter(TRACKID);
100
		long merchantPaymentId = Long.parseLong(trackId);
101
		String amount = request.getParameter(AMOUNT);
102
		String errorText = request.getParameter(ERRORTEXT);
103
 
719 rajveer 104
		//FIXME dump them somewhere
853 rajveer 105
		String udf5=request.getParameter(UDF5);
894 rajveer 106
		//FIXME hdfc is sending comma separated amount, which is very disappointing. May be we get more surprises moving forward.
856 rajveer 107
		amount= amount.replace(",", "");
894 rajveer 108
 
109
		Client paymentClient = paymentServiceClient.getClient();
741 rajveer 110
		try {
894 rajveer 111
			Payment payment = paymentClient.getPayment(merchantPaymentId);
1905 chandransh 112
			txnId = payment.getMerchantTxnId();
113
 
114
			if(!validatePaymentParams(paymentId, amount, udf5, payment))
741 rajveer 115
				return "index";
116
		} catch (PaymentException e1) {
853 rajveer 117
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId);
741 rajveer 118
			e1.printStackTrace();
119
		} catch (TException e1) {
853 rajveer 120
			log.error("Thrift exception. Check payment id "+ merchantPaymentId);
741 rajveer 121
			e1.printStackTrace();
122
		}
719 rajveer 123
 
1905 chandransh 124
		if (result != null && result.trim().equals(PaymentReturnStatus.CAPTURED.value())) {
894 rajveer 125
			String message = "Payment successful";
1905 chandransh 126
 
127
			updatePaymentDetails(merchantPaymentId, message, PaymentStatus.SUCCESS, request, paymentClient);
894 rajveer 128
 
1905 chandransh 129
			CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient);
130
 
131
			this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
132
		} else {
133
			updatePaymentDetails(merchantPaymentId, errorText, PaymentStatus.FAILED, request, paymentClient);
134
 
135
			CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
136
 
137
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
1554 varun.gupt 138
		}
1905 chandransh 139
 
140
		return "index";
141
	}
142
 
143
	private boolean validatePaymentParams(String paymentId,	String amount, String udf5, Payment payment) {
144
		long merchantPaymentId = payment.getPaymentId();
145
		String dbUdf5="";
146
		double dbAmount = payment.getAmount();
147
		for(Attribute attribute: payment.getAttributes()){
148
			if(attribute.getName().trim().equalsIgnoreCase(UDF5)){
149
				dbUdf5 = attribute.getValue();
719 rajveer 150
			}
151
		}
1905 chandransh 152
		// verify 3 things:  udf5, amount and paymentid
153
		//FIXME should we first dump the data and then verify these things. ?? 
2134 chandransh 154
		double returnedAmount = Double.parseDouble(amount);
155
		log.info(paymentId+ ":"+ payment.getGatewayPaymentId() + "\n" + returnedAmount + ":" + dbAmount + "\n" + dbUdf5 + ":" + udf5 );
156
		if(!(paymentId.equalsIgnoreCase(payment.getGatewayPaymentId()) && (Math.abs(dbAmount - returnedAmount) <= 0.50) && udf5.equalsIgnoreCase(dbUdf5))){
1905 chandransh 157
			log.error("Checks and balance failed on returned data");
158
			this.redirectUrl =  errorUrl + "?paymentId="+merchantPaymentId;
159
			return false;
160
		}
161
		return true;
162
	}
163
 
164
	private void updatePaymentDetails(long merchantPaymentId, String message, PaymentStatus status, HttpServletRequest req, Client paymentClient) {
165
		String paymentId = request.getParameter(PAYMENTID);
166
		String result = request.getParameter(RESULT);
167
		String postdate = request.getParameter(POSTDATE);
168
		String tranId = request.getParameter(TRANID);
169
		String auth = request.getParameter(AUTH);
170
		String ref = request.getParameter(REF);
719 rajveer 171
 
1905 chandransh 172
		String sessionId = request.getSession().getId();
173
		String errorNo = request.getParameter(ERROR);
174
		try {
175
			paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, status, postdate, null);
176
		} catch (PaymentException e1) {
177
			log.error("Unable to update payment details in our database." + e1.getError_code() + e1.getMessage());
178
			e1.printStackTrace();
179
		} catch (TException e1) {
180
			log.error("Unable to update payment details in our database. Thrift exception.");
181
			e1.printStackTrace();
182
		}
719 rajveer 183
	}
184
 
185
	public String getRedirectUrl(){
186
		return this.redirectUrl;
187
	}
188
 
189
	@Override
190
	public void setServletRequest(HttpServletRequest request) {
191
		this.request = request;
192
		for(Object param: request.getParameterMap().keySet()){
193
			System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));
194
		}
195
	}
196
 
197
	@Override
198
	public void setServletResponse(HttpServletResponse response) {
199
		this.response = response;
200
	}
1554 varun.gupt 201
}