| 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;
|
| 719 |
rajveer |
13 |
import in.shop2020.payments.PaymentStatus;
|
|
|
14 |
import in.shop2020.thrift.clients.PaymentServiceClient;
|
|
|
15 |
import in.shop2020.thrift.clients.TransactionServiceClient;
|
|
|
16 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
17 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
|
|
18 |
import in.shop2020.utils.Logger;
|
|
|
19 |
|
|
|
20 |
import java.io.IOException;
|
|
|
21 |
import java.util.HashMap;
|
|
|
22 |
import java.util.Map;
|
|
|
23 |
|
|
|
24 |
import javax.servlet.http.HttpServletRequest;
|
|
|
25 |
import javax.servlet.http.HttpServletResponse;
|
|
|
26 |
|
|
|
27 |
import org.apache.juli.logging.Log;
|
|
|
28 |
import org.apache.juli.logging.LogFactory;
|
|
|
29 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
30 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
| 741 |
rajveer |
31 |
import org.apache.thrift.TException;
|
| 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 |
|
|
|
54 |
PaymentServiceClient pclient = null;
|
|
|
55 |
TransactionServiceClient tsc = null;
|
|
|
56 |
UserContextServiceClient usc = null;
|
|
|
57 |
private static Log log = LogFactory.getLog(HdfcPayResponseController.class);
|
|
|
58 |
|
|
|
59 |
public static String AMOUNT = "amt";
|
|
|
60 |
public static String TRACKID = "trackid";
|
|
|
61 |
public static String RESULT = "result";
|
|
|
62 |
public static String AUTH = "auth";
|
|
|
63 |
public static String TRANID = "tranid";
|
|
|
64 |
public static String PAYMENTID = "paymentid";
|
|
|
65 |
public static String REF = "ref";
|
|
|
66 |
public static String POSTDATE = "postdate";
|
|
|
67 |
public static String ERROR = "Error";
|
|
|
68 |
public static String ERRORTEXT = "ErrorText";
|
| 853 |
rajveer |
69 |
public static String UDF5 = "udf5";
|
| 719 |
rajveer |
70 |
String redirectUrl;
|
|
|
71 |
String amount;
|
|
|
72 |
String trackId;
|
|
|
73 |
String result;
|
|
|
74 |
String postdate;
|
|
|
75 |
String auth;
|
|
|
76 |
String ref;
|
|
|
77 |
String tranId;
|
|
|
78 |
String paymentId;
|
|
|
79 |
String sessionId;
|
|
|
80 |
String errorText;
|
|
|
81 |
String errorNo;
|
|
|
82 |
long txnId;
|
|
|
83 |
long merchantPaymentId;
|
|
|
84 |
Transaction transaction = null;
|
|
|
85 |
|
|
|
86 |
public HdfcPayResponseController() {
|
|
|
87 |
try {
|
|
|
88 |
pclient = new PaymentServiceClient();
|
|
|
89 |
tsc = new TransactionServiceClient();
|
|
|
90 |
usc = new UserContextServiceClient();
|
|
|
91 |
} catch (Exception e) {
|
|
|
92 |
Logger.log("Could not initialize the paymentservice client", this);
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public String create() throws IOException, SecurityException{
|
|
|
97 |
System.out.println("Inside hdfc pay response Create");
|
|
|
98 |
|
|
|
99 |
result = this.request.getParameter(RESULT);
|
|
|
100 |
postdate = this.request.getParameter(POSTDATE);
|
|
|
101 |
tranId = this.request.getParameter(TRANID);
|
|
|
102 |
auth = this.request.getParameter(AUTH);
|
|
|
103 |
ref = this.request.getParameter(REF);
|
|
|
104 |
amount = this.request.getParameter(AMOUNT);
|
|
|
105 |
paymentId = this.request.getParameter(PAYMENTID);
|
|
|
106 |
trackId = this.request.getParameter(TRACKID);
|
|
|
107 |
|
|
|
108 |
merchantPaymentId = Long.parseLong(trackId);
|
|
|
109 |
sessionId = this.request.getSession().getId();
|
|
|
110 |
errorNo = request.getParameter(ERROR);
|
|
|
111 |
errorText = request.getParameter(ERRORTEXT);
|
|
|
112 |
//FIXME dump them somewhere
|
| 741 |
rajveer |
113 |
/*
|
| 719 |
rajveer |
114 |
String udf1=request.getParameter("udf1");
|
|
|
115 |
String udf2=request.getParameter("udf2");
|
|
|
116 |
String udf3=request.getParameter("udf3");
|
|
|
117 |
String udf4=request.getParameter("udf4");
|
| 741 |
rajveer |
118 |
*/
|
| 853 |
rajveer |
119 |
String udf5=request.getParameter(UDF5);
|
| 856 |
rajveer |
120 |
amount= amount.replace(",", "");
|
| 741 |
rajveer |
121 |
Payment payment = null;
|
|
|
122 |
try {
|
|
|
123 |
payment = pclient.getClient().getPayment(merchantPaymentId);
|
| 853 |
rajveer |
124 |
String dbUdf5="";
|
|
|
125 |
double dbAmount = payment.getAmount();
|
|
|
126 |
for(Attribute attribute: payment.getAttributes()){
|
|
|
127 |
if(attribute.getName().trim().equalsIgnoreCase(UDF5)){
|
|
|
128 |
dbUdf5 = attribute.getValue();
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
// verify 3 things: udf5, amount and paymentid
|
| 856 |
rajveer |
132 |
log.info(paymentId+ ":"+ payment.getGatewayPaymentId() + "\n" + Double.parseDouble(amount) + ":" + dbAmount + "\n" + dbUdf5 + ":" + udf5 );
|
|
|
133 |
if(!(paymentId.equalsIgnoreCase(payment.getGatewayPaymentId()) && dbAmount == Double.parseDouble(amount) && udf5.equalsIgnoreCase(dbUdf5))){
|
|
|
134 |
log.error("Checks and balance failed on returned date");
|
| 741 |
rajveer |
135 |
this.redirectUrl = ConfigClient.getClient().get("payment_error_url") + "?paymentId="+merchantPaymentId;
|
|
|
136 |
return "index";
|
|
|
137 |
}
|
|
|
138 |
} catch (PaymentException e1) {
|
| 853 |
rajveer |
139 |
log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId);
|
| 741 |
rajveer |
140 |
e1.printStackTrace();
|
|
|
141 |
} catch (TException e1) {
|
| 853 |
rajveer |
142 |
log.error("Thrift exception. Check payment id "+ merchantPaymentId);
|
| 741 |
rajveer |
143 |
e1.printStackTrace();
|
|
|
144 |
} catch (ConfigException e) {
|
|
|
145 |
this.redirectUrl = "http://173.230.147.250:8080/pay-error?paymentId="+merchantPaymentId;
|
| 853 |
rajveer |
146 |
log.error("Unable to get error url info from Config server. Falling back to default url.");
|
| 741 |
rajveer |
147 |
e.printStackTrace();
|
|
|
148 |
return "index";
|
|
|
149 |
}
|
| 719 |
rajveer |
150 |
|
|
|
151 |
if(result.trim().equals(PaymentReturnStatus.CAPTURED.value())){
|
|
|
152 |
try {
|
|
|
153 |
String message = "Payment successful";
|
| 741 |
rajveer |
154 |
|
|
|
155 |
|
| 719 |
rajveer |
156 |
pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, PaymentStatus.SUCCESS, null);
|
|
|
157 |
txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
|
|
|
158 |
|
|
|
159 |
tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.IN_PROCESS, "Payment received for the order");
|
|
|
160 |
|
|
|
161 |
transaction = tsc.getClient().getTransaction(txnId);
|
|
|
162 |
|
|
|
163 |
Map<Long,Double> items = new HashMap<Long, Double>();
|
|
|
164 |
for(Order order: transaction.getOrders()){
|
|
|
165 |
for(LineItem lineitem: order.getLineitems()){
|
| 889 |
chandransh |
166 |
Long itemId = lineitem.getItem_id();
|
|
|
167 |
Double quantity = items.get(itemId);
|
|
|
168 |
if(quantity!=null)
|
|
|
169 |
quantity = quantity + lineitem.getQuantity();
|
|
|
170 |
items.put(itemId, quantity);
|
| 719 |
rajveer |
171 |
}
|
|
|
172 |
}
|
|
|
173 |
//TODO Optimize the function to send less number of data over network
|
|
|
174 |
usc.getClient().resetCart(transaction.getShoppingCartid(), items);
|
|
|
175 |
|
| 853 |
rajveer |
176 |
}catch (TException e) {
|
|
|
177 |
log.error("Error while updating information in payment database.");
|
|
|
178 |
e.printStackTrace();
|
| 719 |
rajveer |
179 |
//FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
|
|
|
180 |
|
| 853 |
rajveer |
181 |
} catch (ShoppingCartException e) {
|
|
|
182 |
log.error("Error while updating information in cart database.");
|
|
|
183 |
e.printStackTrace();
|
|
|
184 |
} catch (PaymentException e) {
|
|
|
185 |
log.error("Error while updating information in payment database.");
|
|
|
186 |
e.printStackTrace();
|
|
|
187 |
} catch (TransactionServiceException e) {
|
|
|
188 |
log.error("Error while updating information in transaction database.");
|
|
|
189 |
e.printStackTrace();
|
| 719 |
rajveer |
190 |
}
|
|
|
191 |
try{
|
|
|
192 |
this.redirectUrl = ConfigClient.getClient().get("payment_success_url") + "?paymentId="+merchantPaymentId;
|
|
|
193 |
}catch(Exception ex){
|
|
|
194 |
this.redirectUrl = "http://173.230.147.250:8080/pay-success?paymentId="+merchantPaymentId;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
}else{
|
|
|
198 |
try{
|
|
|
199 |
pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, errorText, tranId, auth, ref, errorNo, PaymentStatus.FAILED, null);
|
|
|
200 |
txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
|
|
|
201 |
tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
|
| 853 |
rajveer |
202 |
}catch(TException e){
|
| 719 |
rajveer |
203 |
log.error("Error while updating information.");
|
| 853 |
rajveer |
204 |
e.printStackTrace();
|
| 719 |
rajveer |
205 |
//FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
|
| 853 |
rajveer |
206 |
} catch (PaymentException e) {
|
|
|
207 |
log.error("Error while updating information in payment database.");
|
|
|
208 |
e.printStackTrace();
|
|
|
209 |
} catch (TransactionServiceException e) {
|
|
|
210 |
log.error("Error while updating information in transaction database.");
|
|
|
211 |
e.printStackTrace();
|
| 719 |
rajveer |
212 |
}
|
|
|
213 |
|
|
|
214 |
try{
|
|
|
215 |
this.redirectUrl = ConfigClient.getClient().get("payment_error_url") + "?paymentId="+merchantPaymentId;
|
|
|
216 |
}catch(Exception ex){
|
|
|
217 |
this.redirectUrl = "http://173.230.147.250:8080/pay-error?paymentId="+merchantPaymentId;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
return "index";
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
|
|
|
226 |
public String getRedirectUrl(){
|
|
|
227 |
return this.redirectUrl;
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
@Override
|
|
|
231 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
232 |
this.request = request;
|
|
|
233 |
for(Object param: request.getParameterMap().keySet()){
|
|
|
234 |
System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
@Override
|
|
|
240 |
public void setServletResponse(HttpServletResponse response) {
|
|
|
241 |
this.response = response;
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
}
|