| Line 6... |
Line 6... |
| 6 |
import java.io.InputStreamReader;
|
6 |
import java.io.InputStreamReader;
|
| 7 |
import java.lang.reflect.Type;
|
7 |
import java.lang.reflect.Type;
|
| 8 |
import java.math.BigDecimal;
|
8 |
import java.math.BigDecimal;
|
| 9 |
import java.security.MessageDigest;
|
9 |
import java.security.MessageDigest;
|
| 10 |
import java.security.NoSuchAlgorithmException;
|
10 |
import java.security.NoSuchAlgorithmException;
|
| - |
|
11 |
import java.text.SimpleDateFormat;
|
| - |
|
12 |
import java.util.ArrayList;
|
| 11 |
import java.util.Date;
|
13 |
import java.util.Date;
|
| 12 |
import java.util.HashMap;
|
14 |
import java.util.HashMap;
|
| 13 |
import java.util.List;
|
15 |
import java.util.List;
|
| 14 |
import java.util.Map;
|
16 |
import java.util.Map;
|
| - |
|
17 |
import java.util.Map.Entry;
|
| 15 |
|
18 |
|
| 16 |
import org.apache.http.HttpResponse;
|
19 |
import org.apache.http.HttpResponse;
|
| 17 |
import org.apache.http.NameValuePair;
|
20 |
import org.apache.http.NameValuePair;
|
| 18 |
import org.apache.http.client.HttpClient;
|
21 |
import org.apache.http.client.HttpClient;
|
| 19 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
22 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
| 20 |
import org.apache.http.client.methods.HttpPost;
|
23 |
import org.apache.http.client.methods.HttpPost;
|
| 21 |
import org.apache.http.impl.client.DefaultHttpClient;
|
24 |
import org.apache.http.impl.client.DefaultHttpClient;
|
| - |
|
25 |
import org.apache.http.message.BasicNameValuePair;
|
| 22 |
import org.apache.log4j.Logger;
|
26 |
import org.apache.log4j.Logger;
|
| 23 |
|
27 |
|
| 24 |
import com.google.gson.Gson;
|
28 |
import com.google.gson.Gson;
|
| 25 |
import com.google.gson.GsonBuilder;
|
29 |
import com.google.gson.GsonBuilder;
|
| 26 |
import com.google.gson.JsonArray;
|
30 |
import com.google.gson.JsonArray;
|
| 27 |
import com.google.gson.JsonDeserializationContext;
|
31 |
import com.google.gson.JsonDeserializationContext;
|
| 28 |
import com.google.gson.JsonDeserializer;
|
32 |
import com.google.gson.JsonDeserializer;
|
| 29 |
import com.google.gson.JsonElement;
|
33 |
import com.google.gson.JsonElement;
|
| 30 |
import com.google.gson.JsonObject;
|
34 |
import com.google.gson.JsonObject;
|
| 31 |
import com.google.gson.JsonPrimitive;
|
35 |
import com.google.gson.JsonPrimitive;
|
| - |
|
36 |
import com.google.gson.reflect.TypeToken;
|
| 32 |
|
37 |
|
| 33 |
public class PayuPaymentHandler implements IPaymentHandler {
|
38 |
public class PayuPaymentHandler implements IPaymentHandler {
|
| 34 |
|
39 |
|
| 35 |
private static Logger log = Logger.getLogger(Class.class);
|
40 |
private static Logger log = Logger.getLogger(Class.class);
|
| 36 |
|
41 |
|
| Line 55... |
Line 60... |
| 55 |
} catch (Exception e) {
|
60 |
} catch (Exception e) {
|
| 56 |
log.error("Could not initialize Payu Handler!");
|
61 |
log.error("Could not initialize Payu Handler!");
|
| 57 |
}
|
62 |
}
|
| 58 |
}
|
63 |
}
|
| 59 |
|
64 |
|
| 60 |
public Map<String, String> captureTransaction(String paymentId,
|
65 |
public static Map<String, String> captureTransaction(String paymentId,
|
| 61 |
String payuId) throws Exception {
|
66 |
String payuId) {
|
| 62 |
Map<String, String> params = new HashMap<String, String>();
|
67 |
Map<String, String> params = new HashMap<String, String>();
|
| 63 |
Map<String, String> resultMap = new HashMap<String, String>();
|
68 |
Map<String, String> resultMap = new HashMap<String, String>();
|
| 64 |
params.put("key", accountId);
|
69 |
params.put("key", accountId);
|
| 65 |
params.put("command", "capture_transaction");
|
70 |
params.put("command", "capture_transaction");
|
| 66 |
params.put("var1", payuId);
|
71 |
params.put("var1", payuId);
|
| Line 76... |
Line 81... |
| 76 |
try {
|
81 |
try {
|
| 77 |
postResponse = executePost(apiUrl, getUrlParams(params));
|
82 |
postResponse = executePost(apiUrl, getUrlParams(params));
|
| 78 |
} catch (Exception e) {
|
83 |
} catch (Exception e) {
|
| 79 |
resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
84 |
resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
| 80 |
resultMap.put(ERROR, Errors.CONN_FAILURE.message);
|
85 |
resultMap.put(ERROR, Errors.CONN_FAILURE.message);
|
| - |
|
86 |
return resultMap;
|
| 81 |
}
|
87 |
}
|
| 82 |
GsonBuilder gsonBuilder = new GsonBuilder();
|
88 |
GsonBuilder gsonBuilder = new GsonBuilder();
|
| 83 |
gsonBuilder
|
89 |
gsonBuilder
|
| 84 |
.registerTypeAdapter(Object.class, new NaturalDeserializer());
|
90 |
.registerTypeAdapter(Object.class, new NaturalDeserializer());
|
| 85 |
Gson gson = gsonBuilder.create();
|
91 |
Gson gson = gsonBuilder.create();
|
| 86 |
Map<String, Object> postMap = (Map<String, Object>) gson.fromJson(
|
92 |
Type t = new TypeToken<Map<String,Object>>() {}.getType();
|
| 87 |
postResponse, Object.class);
|
93 |
Map<String, Object> postMap = gson.fromJson(postResponse, t);
|
| 88 |
if ("1".equals(postMap.get("status"))) {
|
94 |
if ("1".equals(postMap.get("status"))) {
|
| 89 |
resultMap.put(CAPTURE_TXN_ID, params.get("var2"));
|
95 |
resultMap.put(CAPTURE_TXN_ID, params.get("var2"));
|
| 90 |
resultMap.put(PAYMENT_ID, paymentId);
|
96 |
resultMap.put(PAYMENT_ID, paymentId);
|
| - |
|
97 |
resultMap.put(CAPTURE_TIME, new SimpleDateFormat().format(new Date()));
|
| 91 |
resultMap.put(REF_NO, (String) postMap.get("bank_ref_num"));
|
98 |
resultMap.put(REF_NO, (String) postMap.get("bank_ref_num"));
|
| - |
|
99 |
log.info("Parsed capture response:");
|
| 92 |
}
|
100 |
}
|
| 93 |
if ("0".equals(equals(postMap.get("status"))
|
101 |
if ("0".equals(postMap.get("status"))) {
|
| 94 |
&& "Capture request failed".equals(postMap.get("status")))) {
|
- |
|
| 95 |
resultMap.put(ERR_CODE, Errors.CAPTURE_FAILURE.code);
|
102 |
resultMap.put(ERR_CODE, Errors.CAPTURE_FAILURE.code);
|
| 96 |
resultMap.put(ERROR, Errors.CAPTURE_FAILURE.message);
|
103 |
resultMap.put(ERROR, (String)postMap.get("msg"));
|
| - |
|
104 |
}
|
| - |
|
105 |
for(Entry<String, String> entry : resultMap.entrySet()){
|
| - |
|
106 |
log.info("Key: " + entry.getKey() + ", Value: " + entry.getValue());
|
| 97 |
}
|
107 |
}
|
| 98 |
return resultMap;
|
108 |
return resultMap;
|
| 99 |
|
109 |
|
| 100 |
}
|
110 |
}
|
| 101 |
|
111 |
|
| 102 |
private List<NameValuePair> getUrlParams(Map<String, String> params) {
|
112 |
private static List<NameValuePair> getUrlParams(Map<String, String> params) {
|
| - |
|
113 |
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
|
| 103 |
// TODO Auto-generated method stub
|
114 |
for(Map.Entry<String, String> entry : params.entrySet()){
|
| - |
|
115 |
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
| - |
|
116 |
}
|
| 104 |
return null;
|
117 |
return pairs;
|
| 105 |
}
|
118 |
}
|
| 106 |
|
119 |
|
| 107 |
private String getSecureHash(Map<String, String> params)
|
120 |
private static String getSecureHash(Map<String, String> params)
|
| 108 |
throws NoSuchAlgorithmException {
|
121 |
throws NoSuchAlgorithmException {
|
| 109 |
StringBuffer sb = new StringBuffer();
|
122 |
StringBuffer sb = new StringBuffer();
|
| 110 |
String passString = sb.append(params.get("key")).append("|")
|
123 |
String passString = sb.append(params.get("key")).append("|")
|
| 111 |
.append(params.get("command")).append("|")
|
124 |
.append(params.get("command")).append("|")
|
| 112 |
.append(params.get("var1")).append("|").append(secretKey)
|
125 |
.append(params.get("var1")).append("|").append(secretKey)
|
| 113 |
.toString();
|
126 |
.toString();
|
| 114 |
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
127 |
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
| 115 |
md.update(passString.getBytes(), 0, passString.getBytes().length);
|
128 |
md.update(passString.getBytes(), 0, passString.getBytes().length);
|
| 116 |
byte[] mdbytes = md.digest();
|
129 |
byte[] mdbytes = md.digest();
|
| 117 |
// convert the byte to hex format method
|
130 |
// convert the byte to hex format method
|
| 118 |
StringBuffer sbNew = new StringBuffer();
|
- |
|
| 119 |
for (int i = 0; i < mdbytes.length; i++) {
|
131 |
for (int i = 0; i < mdbytes.length; i++) {
|
| 120 |
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16)
|
132 |
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16)
|
| 121 |
.substring(1));
|
133 |
.substring(1));
|
| 122 |
}
|
134 |
}
|
| 123 |
return sb.toString();
|
135 |
return sb.toString();
|
| Line 190... |
Line 202... |
| 190 |
map.put(entry.getKey(),
|
202 |
map.put(entry.getKey(),
|
| 191 |
context.deserialize(entry.getValue(), Object.class));
|
203 |
context.deserialize(entry.getValue(), Object.class));
|
| 192 |
return map;
|
204 |
return map;
|
| 193 |
}
|
205 |
}
|
| 194 |
}
|
206 |
}
|
| - |
|
207 |
|
| - |
|
208 |
public static void main (String arg[]){
|
| - |
|
209 |
String jsonText = "{\"amit\":\"Wonderful\", \"babu\":{\"a\":\"Awesome\"}}";
|
| - |
|
210 |
GsonBuilder gsonBuilder = new GsonBuilder();
|
| - |
|
211 |
gsonBuilder
|
| - |
|
212 |
.registerTypeAdapter(Object.class, new NaturalDeserializer());
|
| - |
|
213 |
Gson gson = gsonBuilder.create();
|
| - |
|
214 |
Map<String, Object> postMap = (Map<String, Object>) gson.fromJson(
|
| - |
|
215 |
jsonText, Object.class);
|
| - |
|
216 |
System.out.println(postMap);
|
| - |
|
217 |
System.out.println(new SimpleDateFormat().format(new Date()));
|
| - |
|
218 |
}
|
| 195 |
}
|
219 |
}
|