| 13286 |
amit.gupta |
1 |
package in.shop2020.payment.service.handler;
|
|
|
2 |
|
| 13518 |
amit.gupta |
3 |
import in.shop2020.payment.domain.Payment;
|
| 13286 |
amit.gupta |
4 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
|
|
5 |
|
|
|
6 |
import java.io.BufferedReader;
|
|
|
7 |
import java.io.InputStreamReader;
|
|
|
8 |
import java.lang.reflect.Type;
|
|
|
9 |
import java.math.BigDecimal;
|
|
|
10 |
import java.security.MessageDigest;
|
|
|
11 |
import java.security.NoSuchAlgorithmException;
|
| 13352 |
amit.gupta |
12 |
import java.text.SimpleDateFormat;
|
|
|
13 |
import java.util.ArrayList;
|
| 13286 |
amit.gupta |
14 |
import java.util.Date;
|
|
|
15 |
import java.util.HashMap;
|
|
|
16 |
import java.util.List;
|
|
|
17 |
import java.util.Map;
|
| 13352 |
amit.gupta |
18 |
import java.util.Map.Entry;
|
| 13286 |
amit.gupta |
19 |
|
|
|
20 |
import org.apache.http.HttpResponse;
|
|
|
21 |
import org.apache.http.NameValuePair;
|
|
|
22 |
import org.apache.http.client.HttpClient;
|
|
|
23 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
24 |
import org.apache.http.client.methods.HttpPost;
|
|
|
25 |
import org.apache.http.impl.client.DefaultHttpClient;
|
| 13352 |
amit.gupta |
26 |
import org.apache.http.message.BasicNameValuePair;
|
| 13286 |
amit.gupta |
27 |
import org.apache.log4j.Logger;
|
| 13540 |
amit.gupta |
28 |
import org.json.JSONException;
|
|
|
29 |
import org.json.JSONObject;
|
| 13286 |
amit.gupta |
30 |
|
|
|
31 |
import com.google.gson.Gson;
|
|
|
32 |
import com.google.gson.GsonBuilder;
|
|
|
33 |
import com.google.gson.JsonArray;
|
|
|
34 |
import com.google.gson.JsonDeserializationContext;
|
|
|
35 |
import com.google.gson.JsonDeserializer;
|
|
|
36 |
import com.google.gson.JsonElement;
|
|
|
37 |
import com.google.gson.JsonObject;
|
|
|
38 |
import com.google.gson.JsonPrimitive;
|
| 13352 |
amit.gupta |
39 |
import com.google.gson.reflect.TypeToken;
|
| 13286 |
amit.gupta |
40 |
|
|
|
41 |
public class PayuPaymentHandler implements IPaymentHandler {
|
|
|
42 |
|
|
|
43 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
44 |
|
|
|
45 |
public static final String TXN_ID = "transactionId";
|
|
|
46 |
public static final String PAYMENT_ID = "paymentId";
|
|
|
47 |
public static final String AMOUNT = "amount";
|
|
|
48 |
public static final String DATE_TIME = "dateTime";
|
|
|
49 |
public static final String MODE = "mode";
|
|
|
50 |
public static final String REF_NO = "referenceNo";
|
|
|
51 |
public static final String TXN_TYPE = "transactionType";
|
|
|
52 |
|
|
|
53 |
private static String apiUrl;
|
|
|
54 |
private static String accountId;
|
|
|
55 |
private static String secretKey;
|
|
|
56 |
|
|
|
57 |
static {
|
|
|
58 |
try {
|
|
|
59 |
ConfigClient cc = ConfigClient.getClient();
|
|
|
60 |
accountId = cc.get("payu_account_id");
|
|
|
61 |
secretKey = cc.get("payu_secret_key");
|
|
|
62 |
apiUrl = cc.get("payu_api_url");
|
|
|
63 |
} catch (Exception e) {
|
|
|
64 |
log.error("Could not initialize Payu Handler!");
|
|
|
65 |
}
|
|
|
66 |
}
|
| 13518 |
amit.gupta |
67 |
|
|
|
68 |
public static Map<String, String> refundPayment(Payment payment, double amount){
|
|
|
69 |
Map<String, String> params = new HashMap<String, String>();
|
|
|
70 |
Map<String, String> resultMap = new HashMap<String, String>();
|
|
|
71 |
String payuId = payment.getGatewayPaymentId();
|
|
|
72 |
params.put("key", accountId);
|
|
|
73 |
params.put("command", "cancel_refund_transaction");
|
|
|
74 |
params.put("var1", payuId);
|
|
|
75 |
params.put("var2", new Date().getTime() + "");
|
|
|
76 |
params.put("var3", amount + "");
|
|
|
77 |
try {
|
|
|
78 |
params.put("hash", getSecureHash(params));
|
|
|
79 |
} catch (Exception e) {
|
|
|
80 |
resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
|
|
81 |
resultMap.put(ERROR, Errors.CONN_FAILURE.message);
|
|
|
82 |
return resultMap;
|
|
|
83 |
}
|
|
|
84 |
String postResponse = "";
|
|
|
85 |
try {
|
|
|
86 |
postResponse = executePost(apiUrl, getUrlParams(params));
|
|
|
87 |
} catch (Exception e) {
|
|
|
88 |
resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
|
|
89 |
resultMap.put(ERROR, Errors.CONN_FAILURE.message);
|
|
|
90 |
return resultMap;
|
|
|
91 |
}
|
| 13538 |
amit.gupta |
92 |
log.info("postResponse====" + postResponse);
|
| 13540 |
amit.gupta |
93 |
JSONObject postMap = null;
|
|
|
94 |
try {
|
|
|
95 |
postMap = new JSONObject(postResponse);
|
|
|
96 |
|
|
|
97 |
if (0 == ((Integer)postMap.get("status")).intValue()) {
|
|
|
98 |
log.info("Some error occurred at Payu");
|
|
|
99 |
resultMap.put(ERR_CODE, Errors.CAPTURE_FAILURE.code);
|
|
|
100 |
resultMap.put(ERROR, (String)postMap.get("msg"));
|
|
|
101 |
} else if (1 == ((Integer)postMap.get("status")).intValue()){
|
| 13549 |
amit.gupta |
102 |
if(!postMap.isNull("txn_update_id")) {
|
|
|
103 |
resultMap.put("txn_update_id", postMap.optString("txn_update_id"));
|
| 13546 |
amit.gupta |
104 |
}
|
| 13549 |
amit.gupta |
105 |
if(!postMap.isNull("request_id")){
|
|
|
106 |
resultMap.put("txn_update_id", postMap.optString("request_id"));
|
| 13540 |
amit.gupta |
107 |
}
|
| 13549 |
amit.gupta |
108 |
resultMap.put("mihpayid", postMap.optString("mihpayid"));
|
|
|
109 |
resultMap.put("msg", postMap.optString("msg"));
|
| 13540 |
amit.gupta |
110 |
}
|
|
|
111 |
} catch (JSONException e) {
|
|
|
112 |
log.error("Could not Parse json");
|
| 13518 |
amit.gupta |
113 |
resultMap.put(ERR_CODE, Errors.CAPTURE_FAILURE.code);
|
| 13540 |
amit.gupta |
114 |
resultMap.put(ERROR, "Could not parse refund");
|
| 13518 |
amit.gupta |
115 |
}
|
|
|
116 |
log.info("Parsed refund response for payment" + payment.getId());
|
|
|
117 |
return resultMap;
|
|
|
118 |
}
|
| 13286 |
amit.gupta |
119 |
|
| 13352 |
amit.gupta |
120 |
public static Map<String, String> captureTransaction(String paymentId,
|
|
|
121 |
String payuId) {
|
| 13286 |
amit.gupta |
122 |
Map<String, String> params = new HashMap<String, String>();
|
|
|
123 |
Map<String, String> resultMap = new HashMap<String, String>();
|
|
|
124 |
params.put("key", accountId);
|
|
|
125 |
params.put("command", "capture_transaction");
|
|
|
126 |
params.put("var1", payuId);
|
|
|
127 |
params.put("var2", new Date().getTime() + "");
|
|
|
128 |
try {
|
|
|
129 |
params.put("hash", getSecureHash(params));
|
|
|
130 |
} catch (Exception e) {
|
|
|
131 |
resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
|
|
132 |
resultMap.put(ERROR, Errors.CONN_FAILURE.message);
|
|
|
133 |
return resultMap;
|
|
|
134 |
}
|
|
|
135 |
String postResponse = "";
|
|
|
136 |
try {
|
|
|
137 |
postResponse = executePost(apiUrl, getUrlParams(params));
|
|
|
138 |
} catch (Exception e) {
|
|
|
139 |
resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
|
|
140 |
resultMap.put(ERROR, Errors.CONN_FAILURE.message);
|
| 13352 |
amit.gupta |
141 |
return resultMap;
|
| 13286 |
amit.gupta |
142 |
}
|
|
|
143 |
GsonBuilder gsonBuilder = new GsonBuilder();
|
|
|
144 |
gsonBuilder
|
|
|
145 |
.registerTypeAdapter(Object.class, new NaturalDeserializer());
|
|
|
146 |
Gson gson = gsonBuilder.create();
|
| 13352 |
amit.gupta |
147 |
Type t = new TypeToken<Map<String,Object>>() {}.getType();
|
|
|
148 |
Map<String, Object> postMap = gson.fromJson(postResponse, t);
|
| 13286 |
amit.gupta |
149 |
if ("1".equals(postMap.get("status"))) {
|
|
|
150 |
resultMap.put(CAPTURE_TXN_ID, params.get("var2"));
|
|
|
151 |
resultMap.put(PAYMENT_ID, paymentId);
|
| 13352 |
amit.gupta |
152 |
resultMap.put(CAPTURE_TIME, new SimpleDateFormat().format(new Date()));
|
| 13286 |
amit.gupta |
153 |
resultMap.put(REF_NO, (String) postMap.get("bank_ref_num"));
|
| 13352 |
amit.gupta |
154 |
log.info("Parsed capture response:");
|
| 13286 |
amit.gupta |
155 |
}
|
| 13352 |
amit.gupta |
156 |
if ("0".equals(postMap.get("status"))) {
|
| 13286 |
amit.gupta |
157 |
resultMap.put(ERR_CODE, Errors.CAPTURE_FAILURE.code);
|
| 13352 |
amit.gupta |
158 |
resultMap.put(ERROR, (String)postMap.get("msg"));
|
| 13286 |
amit.gupta |
159 |
}
|
| 13352 |
amit.gupta |
160 |
for(Entry<String, String> entry : resultMap.entrySet()){
|
|
|
161 |
log.info("Key: " + entry.getKey() + ", Value: " + entry.getValue());
|
|
|
162 |
}
|
| 13286 |
amit.gupta |
163 |
return resultMap;
|
|
|
164 |
|
|
|
165 |
}
|
|
|
166 |
|
| 13352 |
amit.gupta |
167 |
private static List<NameValuePair> getUrlParams(Map<String, String> params) {
|
|
|
168 |
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
|
|
|
169 |
for(Map.Entry<String, String> entry : params.entrySet()){
|
|
|
170 |
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
|
|
171 |
}
|
|
|
172 |
return pairs;
|
| 13286 |
amit.gupta |
173 |
}
|
|
|
174 |
|
| 13352 |
amit.gupta |
175 |
private static String getSecureHash(Map<String, String> params)
|
| 13286 |
amit.gupta |
176 |
throws NoSuchAlgorithmException {
|
|
|
177 |
StringBuffer sb = new StringBuffer();
|
|
|
178 |
String passString = sb.append(params.get("key")).append("|")
|
|
|
179 |
.append(params.get("command")).append("|")
|
|
|
180 |
.append(params.get("var1")).append("|").append(secretKey)
|
|
|
181 |
.toString();
|
|
|
182 |
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
|
|
183 |
md.update(passString.getBytes(), 0, passString.getBytes().length);
|
|
|
184 |
byte[] mdbytes = md.digest();
|
|
|
185 |
// convert the byte to hex format method
|
| 13535 |
amit.gupta |
186 |
StringBuffer sb1 = new StringBuffer();
|
| 13286 |
amit.gupta |
187 |
for (int i = 0; i < mdbytes.length; i++) {
|
| 13535 |
amit.gupta |
188 |
sb1.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16)
|
| 13286 |
amit.gupta |
189 |
.substring(1));
|
|
|
190 |
}
|
| 13535 |
amit.gupta |
191 |
return sb1.toString();
|
| 13286 |
amit.gupta |
192 |
}
|
|
|
193 |
|
|
|
194 |
public static String executePost(String targetURL,
|
|
|
195 |
List<NameValuePair> nameValuePairs) throws Exception {
|
|
|
196 |
HttpClient client = new DefaultHttpClient();
|
|
|
197 |
HttpPost post = new HttpPost(targetURL);
|
|
|
198 |
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
|
|
199 |
HttpResponse response = client.execute(post);
|
|
|
200 |
BufferedReader rd = new BufferedReader(new InputStreamReader(response
|
|
|
201 |
.getEntity().getContent()));
|
|
|
202 |
String line = "";
|
|
|
203 |
StringBuffer sb = new StringBuffer();
|
|
|
204 |
while ((line = rd.readLine()) != null) {
|
|
|
205 |
sb.append(line);
|
|
|
206 |
}
|
|
|
207 |
return sb.toString();
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
private static class NaturalDeserializer implements
|
|
|
211 |
JsonDeserializer<Object> {
|
|
|
212 |
public Object deserialize(JsonElement json, Type typeOfT,
|
|
|
213 |
JsonDeserializationContext context) {
|
|
|
214 |
if (json.isJsonNull())
|
|
|
215 |
return null;
|
|
|
216 |
else if (json.isJsonPrimitive())
|
|
|
217 |
return handlePrimitive(json.getAsJsonPrimitive());
|
|
|
218 |
else if (json.isJsonArray())
|
|
|
219 |
return handleArray(json.getAsJsonArray(), context);
|
|
|
220 |
else
|
|
|
221 |
return handleObject(json.getAsJsonObject(), context);
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
private Object handlePrimitive(JsonPrimitive json) {
|
|
|
225 |
if (json.isBoolean())
|
|
|
226 |
return json.getAsBoolean();
|
|
|
227 |
else if (json.isString())
|
|
|
228 |
return json.getAsString();
|
|
|
229 |
else {
|
|
|
230 |
BigDecimal bigDec = json.getAsBigDecimal();
|
|
|
231 |
// Find out if it is an int type
|
|
|
232 |
try {
|
|
|
233 |
bigDec.toBigIntegerExact();
|
|
|
234 |
try {
|
|
|
235 |
return bigDec.intValueExact();
|
|
|
236 |
} catch (ArithmeticException e) {
|
|
|
237 |
}
|
|
|
238 |
return bigDec.longValue();
|
|
|
239 |
} catch (ArithmeticException e) {
|
|
|
240 |
}
|
|
|
241 |
// Just return it as a double
|
|
|
242 |
return bigDec.doubleValue();
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
private Object handleArray(JsonArray json,
|
|
|
247 |
JsonDeserializationContext context) {
|
|
|
248 |
Object[] array = new Object[json.size()];
|
|
|
249 |
for (int i = 0; i < array.length; i++)
|
|
|
250 |
array[i] = context.deserialize(json.get(i), Object.class);
|
|
|
251 |
return array;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
private Object handleObject(JsonObject json,
|
|
|
255 |
JsonDeserializationContext context) {
|
|
|
256 |
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
257 |
for (Map.Entry<String, JsonElement> entry : json.entrySet())
|
|
|
258 |
map.put(entry.getKey(),
|
|
|
259 |
context.deserialize(entry.getValue(), Object.class));
|
|
|
260 |
return map;
|
|
|
261 |
}
|
|
|
262 |
}
|
| 13352 |
amit.gupta |
263 |
|
| 13549 |
amit.gupta |
264 |
public static void main (String arg[]) throws Exception{
|
| 13546 |
amit.gupta |
265 |
String jsonText = "{\"amit\":0, \"babu\":{\"a\":null}, \"Raka\": null}";
|
| 13540 |
amit.gupta |
266 |
/* System.out.println(jsonText);
|
| 13352 |
amit.gupta |
267 |
GsonBuilder gsonBuilder = new GsonBuilder();
|
|
|
268 |
gsonBuilder
|
|
|
269 |
.registerTypeAdapter(Object.class, new NaturalDeserializer());
|
|
|
270 |
Gson gson = gsonBuilder.create();
|
| 13539 |
amit.gupta |
271 |
Type t = new TypeToken<Map<String,Object>>() {}.getType();
|
|
|
272 |
Map<String, Object> postMap = gson.fromJson(jsonText, t);
|
| 13540 |
amit.gupta |
273 |
Map<String, Object> postMap = (Map<String, Object>) gson.fromJson(
|
|
|
274 |
jsonText, Object.class);
|
| 13352 |
amit.gupta |
275 |
System.out.println(postMap);
|
| 13540 |
amit.gupta |
276 |
System.out.println(0 == ((Integer)postMap.get("amit")).intValue());*/
|
| 13548 |
amit.gupta |
277 |
String postResponse = "{\"status\":1,\"msg\":\"Refund Request Queued\",\"request_id\":\"172046358\",\"bank_ref_num\":null,\"mihpayid\":251494242,\"error_code\":102}";
|
|
|
278 |
JSONObject postMap = new JSONObject(postResponse);
|
|
|
279 |
String a = "";
|
|
|
280 |
log.info("postMap.get(\"status\")=======" + postMap.get("status"));
|
|
|
281 |
if (0 == ((Integer)postMap.get("status")).intValue()) {
|
|
|
282 |
log.info("Some error occurred at Payu");
|
| 13549 |
amit.gupta |
283 |
a= (String)postMap.get("msg");
|
| 13548 |
amit.gupta |
284 |
} else if (1 == ((Integer)postMap.get("status")).intValue()){
|
|
|
285 |
System.out.println("in io block");
|
| 13549 |
amit.gupta |
286 |
if(!postMap.isNull("bank_ref_num")){
|
| 13548 |
amit.gupta |
287 |
a = (String) postMap.get("bank_ref_num");
|
|
|
288 |
}
|
|
|
289 |
System.out.println("after bankrefnum");
|
| 13549 |
amit.gupta |
290 |
if(!postMap.isNull("txn_update_id")) {
|
| 13548 |
amit.gupta |
291 |
a = (String)postMap.get("txn_update_id");
|
|
|
292 |
}
|
|
|
293 |
System.out.println("after txn update");
|
| 13549 |
amit.gupta |
294 |
if(!JSONObject.NULL.equals(postMap.get("request_id"))){
|
| 13548 |
amit.gupta |
295 |
a = (String)postMap.get("request_id");
|
|
|
296 |
}
|
|
|
297 |
System.out.println("after request_id");
|
|
|
298 |
((Integer)postMap.get("mihpayid")).toString();
|
|
|
299 |
System.out.println("after mihpayid");
|
|
|
300 |
a = (String)postMap.get("msg");
|
|
|
301 |
}
|
| 13549 |
amit.gupta |
302 |
System.out.println(postMap.optString("mihpayid"));
|
| 13352 |
amit.gupta |
303 |
}
|
| 13286 |
amit.gupta |
304 |
}
|