| 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 |
CommonPaymentService.sendTxnEmail(txnId, transactionServiceClient);
|
|
|
132 |
|
|
|
133 |
this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
|
|
|
134 |
} else {
|
|
|
135 |
updatePaymentDetails(merchantPaymentId, errorText, PaymentStatus.FAILED, request, paymentClient);
|
|
|
136 |
|
|
|
137 |
CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
|
|
|
138 |
|
|
|
139 |
this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
|
| 1554 |
varun.gupt |
140 |
}
|
| 1905 |
chandransh |
141 |
|
|
|
142 |
return "index";
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
private boolean validatePaymentParams(String paymentId, String amount, String udf5, Payment payment) {
|
|
|
146 |
long merchantPaymentId = payment.getPaymentId();
|
|
|
147 |
String dbUdf5="";
|
|
|
148 |
double dbAmount = payment.getAmount();
|
|
|
149 |
for(Attribute attribute: payment.getAttributes()){
|
|
|
150 |
if(attribute.getName().trim().equalsIgnoreCase(UDF5)){
|
|
|
151 |
dbUdf5 = attribute.getValue();
|
| 719 |
rajveer |
152 |
}
|
|
|
153 |
}
|
| 1905 |
chandransh |
154 |
// verify 3 things: udf5, amount and paymentid
|
|
|
155 |
//FIXME should we first dump the data and then verify these things. ??
|
| 2134 |
chandransh |
156 |
double returnedAmount = Double.parseDouble(amount);
|
|
|
157 |
log.info(paymentId+ ":"+ payment.getGatewayPaymentId() + "\n" + returnedAmount + ":" + dbAmount + "\n" + dbUdf5 + ":" + udf5 );
|
|
|
158 |
if(!(paymentId.equalsIgnoreCase(payment.getGatewayPaymentId()) && (Math.abs(dbAmount - returnedAmount) <= 0.50) && udf5.equalsIgnoreCase(dbUdf5))){
|
| 1905 |
chandransh |
159 |
log.error("Checks and balance failed on returned data");
|
|
|
160 |
this.redirectUrl = errorUrl + "?paymentId="+merchantPaymentId;
|
|
|
161 |
return false;
|
|
|
162 |
}
|
|
|
163 |
return true;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
private void updatePaymentDetails(long merchantPaymentId, String message, PaymentStatus status, HttpServletRequest req, Client paymentClient) {
|
|
|
167 |
String paymentId = request.getParameter(PAYMENTID);
|
|
|
168 |
String result = request.getParameter(RESULT);
|
|
|
169 |
String postdate = request.getParameter(POSTDATE);
|
|
|
170 |
String tranId = request.getParameter(TRANID);
|
|
|
171 |
String auth = request.getParameter(AUTH);
|
|
|
172 |
String ref = request.getParameter(REF);
|
| 719 |
rajveer |
173 |
|
| 1905 |
chandransh |
174 |
String sessionId = request.getSession().getId();
|
|
|
175 |
String errorNo = request.getParameter(ERROR);
|
|
|
176 |
try {
|
|
|
177 |
paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, status, postdate, null);
|
|
|
178 |
} catch (PaymentException e1) {
|
|
|
179 |
log.error("Unable to update payment details in our database." + e1.getError_code() + e1.getMessage());
|
|
|
180 |
e1.printStackTrace();
|
|
|
181 |
} catch (TException e1) {
|
|
|
182 |
log.error("Unable to update payment details in our database. Thrift exception.");
|
|
|
183 |
e1.printStackTrace();
|
|
|
184 |
}
|
| 719 |
rajveer |
185 |
}
|
|
|
186 |
|
|
|
187 |
public String getRedirectUrl(){
|
|
|
188 |
return this.redirectUrl;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
@Override
|
|
|
192 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
193 |
this.request = request;
|
|
|
194 |
for(Object param: request.getParameterMap().keySet()){
|
|
|
195 |
System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
@Override
|
|
|
200 |
public void setServletResponse(HttpServletResponse response) {
|
|
|
201 |
this.response = response;
|
|
|
202 |
}
|
| 1554 |
varun.gupt |
203 |
}
|