| 6060 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.config.ConfigException;
|
| 13372 |
amit.gupta |
4 |
import in.shop2020.model.v1.order.RechargeOrder;
|
|
|
5 |
import in.shop2020.model.v1.order.RechargeOrderStatus;
|
| 6060 |
rajveer |
6 |
import in.shop2020.payments.Attribute;
|
|
|
7 |
import in.shop2020.payments.Payment;
|
|
|
8 |
import in.shop2020.payments.PaymentException;
|
|
|
9 |
import in.shop2020.payments.PaymentStatus;
|
|
|
10 |
import in.shop2020.serving.services.CommonPaymentService;
|
|
|
11 |
import in.shop2020.thrift.clients.PaymentClient;
|
|
|
12 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 13393 |
amit.gupta |
13 |
import in.shop2020.thrift.clients.UserClient;
|
| 6060 |
rajveer |
14 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
|
|
15 |
|
|
|
16 |
import java.security.MessageDigest;
|
|
|
17 |
import java.security.NoSuchAlgorithmException;
|
| 13372 |
amit.gupta |
18 |
import java.text.SimpleDateFormat;
|
| 6060 |
rajveer |
19 |
import java.util.ArrayList;
|
| 13372 |
amit.gupta |
20 |
import java.util.Date;
|
| 6060 |
rajveer |
21 |
import java.util.List;
|
|
|
22 |
import java.util.Map;
|
|
|
23 |
import java.util.TreeMap;
|
|
|
24 |
|
|
|
25 |
import javax.servlet.http.HttpServletRequest;
|
|
|
26 |
|
|
|
27 |
import org.apache.log4j.Logger;
|
|
|
28 |
import org.apache.thrift.TException;
|
|
|
29 |
|
|
|
30 |
@SuppressWarnings("serial")
|
|
|
31 |
public class PayuPayResponseController extends BaseController{
|
|
|
32 |
|
|
|
33 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
34 |
|
|
|
35 |
private static final String UNMAPPED_STATUS = "UnMappedStatus";
|
|
|
36 |
|
|
|
37 |
private static String successUrl;
|
|
|
38 |
private static String errorUrl;
|
| 13372 |
amit.gupta |
39 |
private static String rechargeResultUri;
|
| 6060 |
rajveer |
40 |
|
|
|
41 |
/**
|
|
|
42 |
* The secret key used to decode RC4 encoded data.
|
|
|
43 |
*/
|
| 6062 |
rajveer |
44 |
private static String accountKey;
|
| 6060 |
rajveer |
45 |
|
| 6062 |
rajveer |
46 |
private static String salt;
|
| 6060 |
rajveer |
47 |
|
|
|
48 |
private String redirectUrl;
|
| 13372 |
amit.gupta |
49 |
|
|
|
50 |
private String eurl;
|
|
|
51 |
private String surl;
|
| 6060 |
rajveer |
52 |
|
|
|
53 |
static{
|
|
|
54 |
try {
|
| 13372 |
amit.gupta |
55 |
ConfigClient cc = ConfigClient.getClient();
|
|
|
56 |
successUrl = cc.get("payu_success_url");
|
|
|
57 |
errorUrl = cc.get("payu_error_url");
|
|
|
58 |
accountKey = cc.get("payu_account_id");
|
|
|
59 |
salt = cc.get("payu_secret_key");
|
|
|
60 |
rechargeResultUri = ConfigClient.getClient().get("recharge_result_uri");
|
| 6060 |
rajveer |
61 |
} catch (ConfigException e) {
|
|
|
62 |
log.error("Unable to get success and error usr info from config server.");
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
private Map<String, String> paymentParams = new TreeMap<String, String>();
|
|
|
67 |
|
| 13380 |
amit.gupta |
68 |
public String create() {
|
| 13372 |
amit.gupta |
69 |
eurl = errorUrl;
|
|
|
70 |
surl = successUrl;
|
| 6060 |
rajveer |
71 |
updatePaymentParams(request.getParameterMap());
|
|
|
72 |
|
|
|
73 |
PaymentClient paymentServiceClient = null;
|
|
|
74 |
TransactionClient transactionServiceClient = null;
|
| 13393 |
amit.gupta |
75 |
UserClient userServiceClient = null;
|
| 6060 |
rajveer |
76 |
try {
|
| 13393 |
amit.gupta |
77 |
userServiceClient = new UserClient();
|
| 6060 |
rajveer |
78 |
paymentServiceClient = new PaymentClient();
|
|
|
79 |
transactionServiceClient = new TransactionClient();
|
|
|
80 |
} catch (Exception e) {
|
|
|
81 |
log.error("Unable to initialize one of the clients", e);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
long merchantPaymentId = Long.parseLong(paymentParams.get("txnid"));
|
|
|
86 |
String gatewayPaymentId = paymentParams.get("mihpayid");
|
|
|
87 |
double amount = Double.parseDouble(paymentParams.get("amount"));
|
|
|
88 |
String gatewayTxnStatus = paymentParams.get("status");
|
|
|
89 |
String hash = paymentParams.get("hash");
|
|
|
90 |
String bankRefId = paymentParams.get("bank_ref_num");
|
|
|
91 |
String unmappedStatus = paymentParams.get("unmappedstatus");
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
List<Attribute> attributes = new ArrayList<Attribute>();
|
|
|
95 |
attributes.add(new Attribute(UNMAPPED_STATUS, unmappedStatus));
|
|
|
96 |
|
|
|
97 |
Payment payment = null;
|
|
|
98 |
Long txnId = null;
|
|
|
99 |
try {
|
|
|
100 |
payment = paymentServiceClient.getClient().getPayment(merchantPaymentId);
|
|
|
101 |
txnId = payment.getMerchantTxnId();
|
|
|
102 |
} catch (PaymentException e1) {
|
|
|
103 |
log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);
|
|
|
104 |
} catch (TException e1) {
|
|
|
105 |
log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);
|
|
|
106 |
}
|
| 13372 |
amit.gupta |
107 |
|
|
|
108 |
if(payment.getStatus() != PaymentStatus.INIT){
|
|
|
109 |
// We have already processed a response for this payment. Processing
|
|
|
110 |
// it again may fail his orders. So, let's ask him to check his
|
|
|
111 |
// account.
|
|
|
112 |
return "ebs-pay-response-maybe";
|
|
|
113 |
}
|
| 6060 |
rajveer |
114 |
|
| 13372 |
amit.gupta |
115 |
if(payment.isIsDigital()){
|
|
|
116 |
eurl = rechargeResultUri;
|
|
|
117 |
surl = rechargeResultUri;
|
|
|
118 |
}
|
|
|
119 |
|
| 6060 |
rajveer |
120 |
if(!validatePaymentParams(amount, payment, hash)){
|
| 13372 |
amit.gupta |
121 |
this.redirectUrl = eurl + "?paymentId=" + merchantPaymentId;
|
| 6060 |
rajveer |
122 |
return "index";
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
if(gatewayTxnStatus.equalsIgnoreCase("SUCCESS")){
|
|
|
126 |
try {
|
|
|
127 |
paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
|
| 13372 |
amit.gupta |
128 |
"", gatewayTxnStatus, unmappedStatus.equals("captured") ? "Transaction captured at PG":"Transaction Authorized at PG",
|
|
|
129 |
"", "", bankRefId, "", unmappedStatus.equals("captured") ? PaymentStatus.SUCCESS : PaymentStatus.AUTHORIZED, new SimpleDateFormat().format(new Date()), attributes);
|
| 6060 |
rajveer |
130 |
} catch (PaymentException e) {
|
| 13372 |
amit.gupta |
131 |
this.redirectUrl = eurl + "?paymentId=" + merchantPaymentId;
|
|
|
132 |
return "index";
|
| 6060 |
rajveer |
133 |
} catch (TException e) {
|
| 13372 |
amit.gupta |
134 |
this.redirectUrl = eurl + "?paymentId=" + merchantPaymentId;
|
|
|
135 |
return "index";
|
| 6060 |
rajveer |
136 |
}
|
| 13372 |
amit.gupta |
137 |
if(payment.isIsDigital()){
|
|
|
138 |
RechargeOrder rechargeOrder = null;
|
|
|
139 |
try {
|
|
|
140 |
rechargeOrder = transactionServiceClient.getClient().getRechargeOrdersForTransaction(txnId);
|
|
|
141 |
} catch (Exception e1) {
|
|
|
142 |
log.error("Problem with txn client while getting recharge object", e1);
|
|
|
143 |
this.redirectUrl = eurl + "?paymentId=" + merchantPaymentId;
|
|
|
144 |
return "index";
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
try {
|
|
|
148 |
PaymentClient pcl = new PaymentClient();
|
|
|
149 |
boolean isCaptured = pcl.getClient().capturePayment(txnId, true);
|
|
|
150 |
|
|
|
151 |
//Retry in case we are not able to capture first time
|
|
|
152 |
if(!isCaptured){
|
|
|
153 |
Thread.sleep(2000);
|
|
|
154 |
isCaptured = pcl.getClient().capturePayment(txnId, true);
|
|
|
155 |
}
|
|
|
156 |
if(!isCaptured){
|
|
|
157 |
Thread.sleep(2000);
|
|
|
158 |
isCaptured = pcl.getClient().capturePayment(txnId, true);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
if(isCaptured) {
|
|
|
162 |
transactionServiceClient.getClient().updateRechargeOrderStatus(rechargeOrder.getId(), RechargeOrderStatus.PAYMENT_SUCCESSFUL);
|
|
|
163 |
} else {
|
|
|
164 |
transactionServiceClient.getClient().updateRechargeOrderStatus(rechargeOrder.getId(), RechargeOrderStatus.PAYMENT_FAILED);
|
|
|
165 |
}
|
|
|
166 |
} catch (Exception e) {
|
|
|
167 |
log.error("Problem with txn client while trying to recharge", e);
|
|
|
168 |
}
|
| 13393 |
amit.gupta |
169 |
} else {
|
|
|
170 |
CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient, false);
|
| 13372 |
amit.gupta |
171 |
}
|
| 13393 |
amit.gupta |
172 |
this.redirectUrl = surl + "?paymentId=" + merchantPaymentId;
|
| 6060 |
rajveer |
173 |
|
| 13393 |
amit.gupta |
174 |
} else{
|
| 6060 |
rajveer |
175 |
try {
|
|
|
176 |
paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
|
| 13372 |
amit.gupta |
177 |
"", gatewayTxnStatus, "Payment Failed at PG", "", "", "", "", PaymentStatus.FAILED, new SimpleDateFormat().format(new Date()), attributes);
|
| 6060 |
rajveer |
178 |
} catch (PaymentException e) {
|
|
|
179 |
log.error("Unable to mark the payment as failed", e);
|
|
|
180 |
} catch (TException e) {
|
|
|
181 |
log.error("Unable to mark the payment as failed", e);
|
|
|
182 |
}
|
|
|
183 |
|
| 13411 |
amit.gupta |
184 |
if(!payment.isIsDigital()) {
|
|
|
185 |
CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
|
|
|
186 |
}
|
| 6060 |
rajveer |
187 |
|
| 13411 |
amit.gupta |
188 |
this.redirectUrl = eurl + "?paymentId=" + merchantPaymentId;
|
| 13393 |
amit.gupta |
189 |
|
| 6060 |
rajveer |
190 |
}
|
|
|
191 |
|
| 13393 |
amit.gupta |
192 |
log.info("User will be redirected to: " + surl + "?paymentId=" + merchantPaymentId);
|
| 6060 |
rajveer |
193 |
return "index";
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
private boolean validatePaymentParams(double returnedAmount, Payment payment, String hash){
|
|
|
197 |
if(!(payment != null && Math.abs(payment.getAmount() - returnedAmount) <= 0.50 && hash.equals(getSecureHash()))){
|
|
|
198 |
// We did not request this payment or the authorised amount is different.
|
|
|
199 |
log.error("Checks and balance failed on returned data");
|
|
|
200 |
return false;
|
|
|
201 |
}
|
|
|
202 |
return true;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
|
|
|
206 |
public String getSecureHash(){
|
|
|
207 |
try{
|
| 6065 |
rajveer |
208 |
String pass = salt + "|" + paymentParams.get("status") + "|||||||||||" + paymentParams.get("email") + "|" + paymentParams.get("firstname") + "|" + paymentParams.get("productinfo") + "|" + paymentParams.get("amount") + "|" + paymentParams.get("txnid") + "|" + accountKey;
|
| 6060 |
rajveer |
209 |
System.out.println(pass);
|
|
|
210 |
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
|
|
211 |
md.update(pass.getBytes(), 0, pass.getBytes().length);
|
|
|
212 |
byte[] mdbytes = md.digest();
|
|
|
213 |
// convert the byte to hex format method
|
|
|
214 |
StringBuffer sb = new StringBuffer();
|
|
|
215 |
for (int i = 0; i < mdbytes.length; i++) {
|
|
|
216 |
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
|
|
|
217 |
}
|
|
|
218 |
return sb.toString();
|
|
|
219 |
}catch(NoSuchAlgorithmException nsae){
|
|
|
220 |
log.error("No such algorithm exception");
|
|
|
221 |
return null;
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
private void updatePaymentParams(Map map){
|
|
|
226 |
for(Object key : map.keySet()){
|
|
|
227 |
String keyStr = (String)key;
|
|
|
228 |
String[] vals = (String[])map.get(keyStr);
|
|
|
229 |
String value = vals[0];
|
|
|
230 |
System.out.println("Key " + (String)key + " : " + value);
|
|
|
231 |
paymentParams.put(keyStr, value);
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
public String getRedirectUrl(){
|
|
|
236 |
return this.redirectUrl;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
@Override
|
|
|
240 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
241 |
this.request = request;
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
public Map<String, String> getPaymentParams() {
|
|
|
245 |
return paymentParams;
|
|
|
246 |
}
|
|
|
247 |
}
|