| 1905 |
chandransh |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.config.ConfigException;
|
| 2263 |
vikas |
4 |
import in.shop2020.datalogger.EventType;
|
| 6058 |
anupam.sin |
5 |
import in.shop2020.model.v1.order.RechargeOrder;
|
| 6050 |
anupam.sin |
6 |
import in.shop2020.model.v1.order.RechargeOrderStatus;
|
| 6091 |
anupam.sin |
7 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
| 1905 |
chandransh |
8 |
import in.shop2020.payments.Attribute;
|
|
|
9 |
import in.shop2020.payments.Payment;
|
|
|
10 |
import in.shop2020.payments.PaymentException;
|
|
|
11 |
import in.shop2020.payments.PaymentStatus;
|
|
|
12 |
import in.shop2020.serving.services.CommonPaymentService;
|
|
|
13 |
import in.shop2020.serving.utils.ebs.Base64;
|
|
|
14 |
import in.shop2020.serving.utils.ebs.RC4;
|
| 3126 |
rajveer |
15 |
import in.shop2020.thrift.clients.PaymentClient;
|
|
|
16 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
17 |
import in.shop2020.thrift.clients.UserClient;
|
| 1905 |
chandransh |
18 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
| 2511 |
vikas |
19 |
import in.shop2020.utils.DataLogger;
|
| 1905 |
chandransh |
20 |
|
| 2419 |
vikas |
21 |
import java.io.BufferedReader;
|
|
|
22 |
import java.io.ByteArrayInputStream;
|
|
|
23 |
import java.io.IOException;
|
|
|
24 |
import java.io.InputStreamReader;
|
|
|
25 |
import java.util.ArrayList;
|
|
|
26 |
import java.util.List;
|
|
|
27 |
import java.util.Map;
|
|
|
28 |
import java.util.StringTokenizer;
|
|
|
29 |
import java.util.TreeMap;
|
|
|
30 |
|
| 1905 |
chandransh |
31 |
import javax.servlet.http.HttpServletRequest;
|
|
|
32 |
|
|
|
33 |
import org.apache.log4j.Logger;
|
|
|
34 |
import org.apache.thrift.TException;
|
|
|
35 |
|
| 2118 |
chandransh |
36 |
@SuppressWarnings("serial")
|
|
|
37 |
public class EbsPayResponseController extends BaseController{
|
| 2673 |
vikas |
38 |
|
| 1905 |
chandransh |
39 |
private static Logger log = Logger.getLogger(Class.class);
|
| 2673 |
vikas |
40 |
|
| 1905 |
chandransh |
41 |
private static final String FLAG_KEY = "IsFlagged";
|
|
|
42 |
private static final String TXN_KEY = "TransactionID";
|
|
|
43 |
private static final String AUTH_TXN_ID = "AuthTxnId";
|
| 2673 |
vikas |
44 |
|
| 1905 |
chandransh |
45 |
private static String successUrl;
|
|
|
46 |
private static String errorUrl;
|
| 6050 |
anupam.sin |
47 |
private static String rechargeResultUri;
|
| 2673 |
vikas |
48 |
|
| 1905 |
chandransh |
49 |
/**
|
|
|
50 |
* The secret key used to decode RC4 encoded data.
|
|
|
51 |
*/
|
|
|
52 |
private static String accountKey;
|
| 2673 |
vikas |
53 |
|
| 1905 |
chandransh |
54 |
private String redirectUrl;
|
| 2673 |
vikas |
55 |
|
| 1905 |
chandransh |
56 |
static{
|
|
|
57 |
try {
|
|
|
58 |
successUrl = ConfigClient.getClient().get("ebs_success_url");
|
|
|
59 |
errorUrl = ConfigClient.getClient().get("ebs_error_url");
|
| 6050 |
anupam.sin |
60 |
setRechargeResultUri(ConfigClient.getClient().get("recharge_success_url"));
|
| 1905 |
chandransh |
61 |
accountKey = ConfigClient.getClient().get("ebs_secret_key");
|
|
|
62 |
} catch (ConfigException e) {
|
|
|
63 |
log.error("Unable to get success and error usr info from config server.");
|
|
|
64 |
}
|
|
|
65 |
}
|
| 2673 |
vikas |
66 |
|
| 2681 |
vikas |
67 |
private Map<String, String> paymentParams = new TreeMap<String, String>();
|
| 2673 |
vikas |
68 |
|
| 1905 |
chandransh |
69 |
public String index() {
|
|
|
70 |
StringBuffer data1 = new StringBuffer(request.getParameter("DR"));
|
|
|
71 |
log.info("Received data string: " + data1.toString());
|
|
|
72 |
byte[] result = decodeRecvdData(data1);
|
|
|
73 |
|
|
|
74 |
String recvString = parseRecvdData(result);
|
|
|
75 |
updatePaymentParams(recvString);
|
| 2673 |
vikas |
76 |
|
| 3126 |
rajveer |
77 |
PaymentClient paymentServiceClient = null;
|
|
|
78 |
TransactionClient transactionServiceClient = null;
|
|
|
79 |
UserClient userServiceClient = null;
|
| 1905 |
chandransh |
80 |
try {
|
| 3126 |
rajveer |
81 |
paymentServiceClient = new PaymentClient();
|
|
|
82 |
transactionServiceClient = new TransactionClient();
|
|
|
83 |
userServiceClient = new UserClient();
|
| 1905 |
chandransh |
84 |
} catch (Exception e) {
|
| 2942 |
chandransh |
85 |
log.error("Unable to initialize one of the clients", e);
|
| 1905 |
chandransh |
86 |
}
|
| 2673 |
vikas |
87 |
|
|
|
88 |
|
| 1905 |
chandransh |
89 |
long merchantPaymentId = Long.parseLong(paymentParams.get("MerchantRefNo"));
|
|
|
90 |
String gatewayPaymentId = paymentParams.get("PaymentID");
|
|
|
91 |
double amount = Double.parseDouble(paymentParams.get("Amount"));
|
|
|
92 |
String isFlagged = paymentParams.get(FLAG_KEY);
|
|
|
93 |
String gatewayTxnStatus = paymentParams.get("ResponseCode");
|
|
|
94 |
String description = paymentParams.get("ResponseMessage");
|
|
|
95 |
String authTxnId = paymentParams.get(TXN_KEY);
|
| 2673 |
vikas |
96 |
|
|
|
97 |
|
| 1905 |
chandransh |
98 |
List<Attribute> attributes = new ArrayList<Attribute>();
|
|
|
99 |
attributes.add(new Attribute(FLAG_KEY, isFlagged));
|
|
|
100 |
attributes.add(new Attribute(AUTH_TXN_ID, authTxnId));
|
| 2673 |
vikas |
101 |
|
| 1905 |
chandransh |
102 |
Payment payment = null;
|
|
|
103 |
Long txnId = null;
|
|
|
104 |
try {
|
|
|
105 |
payment = paymentServiceClient.getClient().getPayment(merchantPaymentId);
|
|
|
106 |
txnId = payment.getMerchantTxnId();
|
|
|
107 |
} catch (PaymentException e1) {
|
| 2334 |
chandransh |
108 |
log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);
|
| 1905 |
chandransh |
109 |
} catch (TException e1) {
|
| 2334 |
chandransh |
110 |
log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);
|
| 1905 |
chandransh |
111 |
}
|
| 2673 |
vikas |
112 |
|
| 2118 |
chandransh |
113 |
if(payment.getStatus() != PaymentStatus.INIT){
|
|
|
114 |
// We have already processed a response for this payment. Processing
|
|
|
115 |
// it again may fail his orders. So, let's ask him to check his
|
|
|
116 |
// account.
|
|
|
117 |
return "maybe";
|
|
|
118 |
}
|
| 2673 |
vikas |
119 |
|
| 1905 |
chandransh |
120 |
if(!validatePaymentParams(amount, payment)){
|
| 2118 |
chandransh |
121 |
this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
|
| 1905 |
chandransh |
122 |
return "index";
|
|
|
123 |
}
|
| 2673 |
vikas |
124 |
|
| 1905 |
chandransh |
125 |
if(gatewayTxnStatus.equals("0")){
|
| 4246 |
rajveer |
126 |
//Update payment status as authorized if payment is authorized.
|
| 1905 |
chandransh |
127 |
try {
|
|
|
128 |
paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
|
|
|
129 |
"", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.AUTHORIZED, "", attributes);
|
|
|
130 |
} catch (PaymentException e) {
|
| 2942 |
chandransh |
131 |
log.error("Unable to mark the payment as authorized", e);
|
| 1905 |
chandransh |
132 |
} catch (TException e) {
|
| 2942 |
chandransh |
133 |
log.error("Unable to mark the payment as authorized", e);
|
| 1905 |
chandransh |
134 |
}
|
| 6050 |
anupam.sin |
135 |
|
|
|
136 |
if(payment.isIsDigital()) {
|
| 6091 |
anupam.sin |
137 |
RechargeOrder rechargeOrder = null;
|
|
|
138 |
try {
|
|
|
139 |
rechargeOrder = transactionServiceClient.getClient().getRechargeOrdersForTransaction(txnId);
|
|
|
140 |
} catch (Exception e1) {
|
|
|
141 |
log.error("Problem with txn client while getting recharge object", e1);
|
| 6050 |
anupam.sin |
142 |
}
|
| 6091 |
anupam.sin |
143 |
if(!isFlagged.equals("YES")) {
|
|
|
144 |
//Recharge only when payment is digital and is captured and is not flagged
|
|
|
145 |
try {
|
|
|
146 |
PaymentClient pcl = new PaymentClient();
|
|
|
147 |
boolean isCaptured = pcl.getClient().capturePayment(txnId);
|
|
|
148 |
|
|
|
149 |
if(isCaptured) {
|
|
|
150 |
transactionServiceClient.getClient().updateRechargeOrderStatus(rechargeOrder.getId(), RechargeOrderStatus.PAYMENT_SUCCESSFUL);
|
|
|
151 |
} else {
|
|
|
152 |
transactionServiceClient.getClient().updateRechargeOrderStatus(rechargeOrder.getId(), RechargeOrderStatus.PAYMENT_FAILED);
|
|
|
153 |
}
|
|
|
154 |
} catch (Exception e) {
|
|
|
155 |
log.error("Problem with txn client while trying to recharge", e);
|
|
|
156 |
}
|
|
|
157 |
} else {
|
|
|
158 |
try {
|
|
|
159 |
transactionServiceClient.getClient().updateRechargeOrderStatus(rechargeOrder.getId(), RechargeOrderStatus.PAYMENT_FAILED);
|
|
|
160 |
} catch (Exception e) {
|
|
|
161 |
log.error("Problem with txn client while trying to mark payment failed in recharge order", e);
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
this.redirectUrl = rechargeResultUri + "?paymentId=" + merchantPaymentId;
|
| 6050 |
anupam.sin |
165 |
} else {
|
| 6091 |
anupam.sin |
166 |
//For physical orders
|
| 6050 |
anupam.sin |
167 |
if(isFlagged.equals("YES")){
|
|
|
168 |
CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient, true);
|
|
|
169 |
this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
|
|
|
170 |
}else{
|
|
|
171 |
CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient, false);
|
|
|
172 |
this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
|
|
|
173 |
}
|
| 4246 |
rajveer |
174 |
}
|
| 3010 |
chandransh |
175 |
|
| 4246 |
rajveer |
176 |
|
| 3010 |
chandransh |
177 |
// Map<String, String> captureResult = EbsPaymentService.capturePayment(payment, gatewayPaymentId);
|
|
|
178 |
// String captureStatus = captureResult.get(IPaymentService.STATUS);
|
|
|
179 |
//
|
|
|
180 |
// if("".equals(captureStatus)){
|
|
|
181 |
// //Failure
|
|
|
182 |
// description = captureResult.get(EbsPaymentService.ERROR);
|
|
|
183 |
// String errorCode = captureResult.get(EbsPaymentService.ERR_CODE);
|
|
|
184 |
// try {
|
|
|
185 |
// paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
|
|
|
186 |
// "", gatewayTxnStatus, description, "", "", "", errorCode, PaymentStatus.FAILED, "", attributes);
|
|
|
187 |
// } catch (PaymentException e) {
|
|
|
188 |
// log.error("Error while updating failed capture payment attempt: ", e);
|
|
|
189 |
// } catch (TException e) {
|
|
|
190 |
// log.error("Error while updating failed capture payment attempt: ", e);
|
|
|
191 |
// }
|
| 3224 |
vikas |
192 |
// DataLogger.logData(EventType.PAYMENT_FAILURE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,
|
| 3010 |
chandransh |
193 |
// gatewayTxnStatus, description, errorCode);
|
|
|
194 |
// this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
|
|
|
195 |
// }else{
|
|
|
196 |
// //Success
|
|
|
197 |
// try {
|
|
|
198 |
// attributes.add(new Attribute(IPaymentService.CAPTURE_TXN_ID, captureResult.get(IPaymentService.CAPTURE_TXN_ID)));
|
|
|
199 |
// attributes.add(new Attribute(IPaymentService.CAPTURE_TIME, captureResult.get(IPaymentService.CAPTURE_TIME)));
|
|
|
200 |
//
|
|
|
201 |
// paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
|
|
|
202 |
// "", captureStatus, description, "", "", "", "", PaymentStatus.SUCCESS, "", attributes);
|
|
|
203 |
// } catch (PaymentException e) {
|
|
|
204 |
// log.error("Error while updating successful capture payment attempt: ", e);
|
|
|
205 |
// } catch (TException e) {
|
|
|
206 |
// log.error("Error while updating successful capture payment attempt: ", e);
|
|
|
207 |
// }
|
|
|
208 |
//
|
|
|
209 |
// CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient);
|
|
|
210 |
//
|
|
|
211 |
// this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
|
|
|
212 |
// }
|
| 1905 |
chandransh |
213 |
}else{
|
|
|
214 |
try {
|
|
|
215 |
paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
|
|
|
216 |
"", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.FAILED, "", attributes);
|
|
|
217 |
} catch (PaymentException e) {
|
| 2942 |
chandransh |
218 |
log.error("Unable to mark the payment as failed", e);
|
| 1905 |
chandransh |
219 |
} catch (TException e) {
|
| 2942 |
chandransh |
220 |
log.error("Unable to mark the payment as failed", e);
|
| 1905 |
chandransh |
221 |
}
|
| 6050 |
anupam.sin |
222 |
if(!payment.isIsDigital()) {
|
| 1905 |
chandransh |
223 |
CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
|
| 3185 |
vikas |
224 |
DataLogger.logData(EventType.PAYMENT_FAILURE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,
|
| 2157 |
vikas |
225 |
gatewayTxnStatus, description);
|
| 1905 |
chandransh |
226 |
|
|
|
227 |
this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
|
| 6050 |
anupam.sin |
228 |
} else {
|
|
|
229 |
this.redirectUrl = rechargeResultUri + "?paymentId=" + merchantPaymentId;
|
|
|
230 |
}
|
| 1905 |
chandransh |
231 |
}
|
| 2673 |
vikas |
232 |
|
| 1905 |
chandransh |
233 |
log.info("User will be redirected to: " + this.redirectUrl);
|
|
|
234 |
return "index";
|
|
|
235 |
}
|
| 2673 |
vikas |
236 |
|
| 2134 |
chandransh |
237 |
private boolean validatePaymentParams(double returnedAmount, Payment payment){
|
|
|
238 |
if(!(payment != null && Math.abs(payment.getAmount() - returnedAmount) <= 0.50)){
|
| 1905 |
chandransh |
239 |
// We did not request this payment or the authorised amount is different.
|
|
|
240 |
log.error("Checks and balance failed on returned data");
|
|
|
241 |
return false;
|
|
|
242 |
}
|
|
|
243 |
return true;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
private byte[] decodeRecvdData(StringBuffer data1) {
|
|
|
247 |
for (int i = 0; i < data1.length(); i++) {
|
|
|
248 |
if (data1.charAt(i) == ' ')
|
|
|
249 |
data1.setCharAt(i, '+');
|
|
|
250 |
}
|
| 2673 |
vikas |
251 |
|
| 1905 |
chandransh |
252 |
Base64 base64 = new Base64();
|
|
|
253 |
byte[] data = base64.decode(data1.toString());
|
|
|
254 |
RC4 rc4 = new RC4(accountKey);
|
|
|
255 |
byte[] result = rc4.rc4(data);
|
|
|
256 |
return result;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
private String parseRecvdData(byte[] result) {
|
|
|
260 |
ByteArrayInputStream byteIn = new ByteArrayInputStream(result, 0, result.length);
|
| 2118 |
chandransh |
261 |
BufferedReader reader = new BufferedReader(new InputStreamReader(byteIn));
|
| 1905 |
chandransh |
262 |
String recvString1 = "";
|
|
|
263 |
String recvString = "";
|
|
|
264 |
try {
|
| 2118 |
chandransh |
265 |
recvString1 = reader.readLine();
|
| 1905 |
chandransh |
266 |
int lineCount = 0;
|
|
|
267 |
while (recvString1 != null) {
|
|
|
268 |
lineCount++;
|
|
|
269 |
if (lineCount > 705)
|
|
|
270 |
break;
|
|
|
271 |
recvString += recvString1 + "\n";
|
| 2118 |
chandransh |
272 |
recvString1 = reader.readLine();
|
| 1905 |
chandransh |
273 |
}
|
|
|
274 |
} catch (IOException e) {
|
| 2942 |
chandransh |
275 |
log.error("Unable to read from Ebs response", e);
|
| 1905 |
chandransh |
276 |
}
|
|
|
277 |
recvString = recvString.replace("=&", "=--&");
|
|
|
278 |
return recvString;
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
private void updatePaymentParams(String str){
|
|
|
282 |
StringTokenizer st = new StringTokenizer(str, "=&");
|
| 2673 |
vikas |
283 |
String key, value;
|
| 1905 |
chandransh |
284 |
while(st.hasMoreTokens()) {
|
|
|
285 |
key = st.nextToken();
|
|
|
286 |
value = st.nextToken();
|
|
|
287 |
log.info("Key: " + key + ", Value: " + value);
|
|
|
288 |
paymentParams.put(key, value);
|
|
|
289 |
}
|
|
|
290 |
}
|
| 2673 |
vikas |
291 |
|
| 1905 |
chandransh |
292 |
public String getRedirectUrl(){
|
|
|
293 |
return this.redirectUrl;
|
|
|
294 |
}
|
| 2673 |
vikas |
295 |
|
| 1905 |
chandransh |
296 |
@Override
|
|
|
297 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
298 |
this.request = request;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
public Map<String, String> getPaymentParams() {
|
|
|
302 |
return paymentParams;
|
|
|
303 |
}
|
| 6050 |
anupam.sin |
304 |
|
|
|
305 |
public static void setRechargeResultUri(String rechargeResultUri) {
|
|
|
306 |
EbsPayResponseController.rechargeResultUri = rechargeResultUri;
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
public static String getRechargeResultUri() {
|
|
|
310 |
return rechargeResultUri;
|
|
|
311 |
}
|
| 1905 |
chandransh |
312 |
}
|