Subversion Repositories SmartDukaan

Rev

Rev 1555 | Rev 2134 | 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;
719 rajveer 4
import in.shop2020.model.v1.order.LineItem;
5
import in.shop2020.model.v1.order.Order;
6
import in.shop2020.model.v1.order.Transaction;
853 rajveer 7
import in.shop2020.model.v1.order.TransactionServiceException;
719 rajveer 8
import in.shop2020.model.v1.order.TransactionStatus;
853 rajveer 9
import in.shop2020.model.v1.user.ShoppingCartException;
10
import in.shop2020.payments.Attribute;
741 rajveer 11
import in.shop2020.payments.Payment;
12
import in.shop2020.payments.PaymentException;
894 rajveer 13
import in.shop2020.payments.PaymentService.Client;
719 rajveer 14
import in.shop2020.payments.PaymentStatus;
1905 chandransh 15
import in.shop2020.serving.services.CommonPaymentService;
719 rajveer 16
import in.shop2020.thrift.clients.PaymentServiceClient;
17
import in.shop2020.thrift.clients.TransactionServiceClient;
18
import in.shop2020.thrift.clients.UserContextServiceClient;
19
import in.shop2020.thrift.clients.config.ConfigClient;
20
 
21
import java.io.IOException;
22
import java.util.HashMap;
23
import java.util.Map;
24
 
25
import javax.servlet.http.HttpServletRequest;
26
import javax.servlet.http.HttpServletResponse;
27
 
28
import org.apache.struts2.interceptor.ServletRequestAware;
29
import org.apache.struts2.interceptor.ServletResponseAware;
741 rajveer 30
import org.apache.thrift.TException;
1044 chandransh 31
import org.apache.log4j.Logger;
719 rajveer 32
 
33
public class HdfcPayResponseController implements ServletResponseAware, ServletRequestAware{
34
	private static final long serialVersionUID = 1L;
35
 
36
	private enum PaymentReturnStatus{
37
		CAPTURED("CAPTURED"),
38
		NOT_CAPTURED ("NOT CAPTURED"),
39
		CANCELLED ("CANCELLED"),
40
		DENIED_BY_RISK("DENIED BY RISK"),
41
		HOST_TIMEOUT("HOST TIMEOUT");
42
		private String value;
43
		PaymentReturnStatus(String value) {
44
			this.value = value;
45
		}
46
		public String value(){
47
			return this.value;
48
		}
49
	}
50
 
51
	HttpServletRequest request;
52
	HttpServletResponse response;
53
 
1044 chandransh 54
	//private static Logger log = LoggerFactory.getLogger(HdfcPayResponseController.class);
55
	private static Logger log = Logger.getLogger(Class.class);
719 rajveer 56
 
894 rajveer 57
	public static String successUrl;
58
	public static String errorUrl;
59
 
719 rajveer 60
	public static String AMOUNT = "amt";
61
	public static String TRACKID = "trackid";
62
	public static String RESULT = "result";
63
	public static String AUTH = "auth";
64
	public static String TRANID = "tranid";
65
	public static String PAYMENTID = "paymentid";
66
	public static String REF = "ref";
67
	public static String POSTDATE = "postdate";
68
	public static String ERROR = "Error";
69
	public static String ERRORTEXT = "ErrorText";
1905 chandransh 70
	public static String UDF5 = "udf5";	
894 rajveer 71
 
72
	static{
719 rajveer 73
		try {
894 rajveer 74
			successUrl = ConfigClient.getClient().get("payment_success_url");
75
			errorUrl = ConfigClient.getClient().get("payment_error_url");
76
		} catch (ConfigException e) {
77
			log.error("Unable to get success and error usr info from config server.");
719 rajveer 78
		}
79
	}
80
 
1905 chandransh 81
	String redirectUrl;
894 rajveer 82
 
83
	public HdfcPayResponseController() {
84
 
85
	}
86
 
719 rajveer 87
	public String create() throws IOException, SecurityException{
894 rajveer 88
		log.info("Inside hdfc pay response Create");
719 rajveer 89
 
894 rajveer 90
		PaymentServiceClient paymentServiceClient = null;
91
		TransactionServiceClient transactionServiceClient = null;
92
		UserContextServiceClient userServiceClient = null;
93
		try{
94
			paymentServiceClient = new PaymentServiceClient();
95
			transactionServiceClient = new TransactionServiceClient();
96
			userServiceClient = new UserContextServiceClient();
97
		}catch(Exception e){
98
			//Nothing to worry. lets move forward
99
			log.error("Unable to initialize one of the clients");
100
			e.printStackTrace();
101
		}
102
 
1905 chandransh 103
		Long txnId = null;
104
 
105
		String paymentId = request.getParameter(PAYMENTID);
106
		String result = request.getParameter(RESULT);
107
		String trackId = request.getParameter(TRACKID);
108
		long merchantPaymentId = Long.parseLong(trackId);
109
		String amount = request.getParameter(AMOUNT);
110
		String errorText = request.getParameter(ERRORTEXT);
111
 
719 rajveer 112
		//FIXME dump them somewhere
853 rajveer 113
		String udf5=request.getParameter(UDF5);
894 rajveer 114
		//FIXME hdfc is sending comma separated amount, which is very disappointing. May be we get more surprises moving forward.
856 rajveer 115
		amount= amount.replace(",", "");
894 rajveer 116
 
117
		Client paymentClient = paymentServiceClient.getClient();
741 rajveer 118
		try {
894 rajveer 119
			Payment payment = paymentClient.getPayment(merchantPaymentId);
1905 chandransh 120
			txnId = payment.getMerchantTxnId();
121
 
122
			if(!validatePaymentParams(paymentId, amount, udf5, payment))
741 rajveer 123
				return "index";
124
		} catch (PaymentException e1) {
853 rajveer 125
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId);
741 rajveer 126
			e1.printStackTrace();
127
		} catch (TException e1) {
853 rajveer 128
			log.error("Thrift exception. Check payment id "+ merchantPaymentId);
741 rajveer 129
			e1.printStackTrace();
130
		}
719 rajveer 131
 
1905 chandransh 132
		if (result != null && result.trim().equals(PaymentReturnStatus.CAPTURED.value())) {
894 rajveer 133
			String message = "Payment successful";
1905 chandransh 134
 
135
			updatePaymentDetails(merchantPaymentId, message, PaymentStatus.SUCCESS, request, paymentClient);
894 rajveer 136
 
1905 chandransh 137
			CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient);
138
 
139
			CommonPaymentService.sendTxnEmail(txnId, transactionServiceClient);
140
 
141
			this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
142
		} else {
143
			updatePaymentDetails(merchantPaymentId, errorText, PaymentStatus.FAILED, request, paymentClient);
144
 
145
			CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
146
 
147
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
1554 varun.gupt 148
		}
1905 chandransh 149
 
150
		return "index";
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();
719 rajveer 160
			}
161
		}
1905 chandransh 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);
719 rajveer 180
 
1905 chandransh 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
		}
719 rajveer 192
	}
193
 
194
	public String getRedirectUrl(){
195
		return this.redirectUrl;
196
	}
197
 
198
	@Override
199
	public void setServletRequest(HttpServletRequest request) {
200
		this.request = request;
201
		for(Object param: request.getParameterMap().keySet()){
202
			System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));
203
		}
204
	}
205
 
206
	@Override
207
	public void setServletResponse(HttpServletResponse response) {
208
		this.response = response;
209
	}
1554 varun.gupt 210
}