| 13286 |
amit.gupta |
1 |
package in.shop2020.payment.service.handler;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
|
|
4 |
|
|
|
5 |
import java.io.BufferedReader;
|
|
|
6 |
import java.io.InputStreamReader;
|
|
|
7 |
import java.lang.reflect.Type;
|
|
|
8 |
import java.math.BigDecimal;
|
|
|
9 |
import java.security.MessageDigest;
|
|
|
10 |
import java.security.NoSuchAlgorithmException;
|
|
|
11 |
import java.util.Date;
|
|
|
12 |
import java.util.HashMap;
|
|
|
13 |
import java.util.List;
|
|
|
14 |
import java.util.Map;
|
|
|
15 |
|
|
|
16 |
import org.apache.http.HttpResponse;
|
|
|
17 |
import org.apache.http.NameValuePair;
|
|
|
18 |
import org.apache.http.client.HttpClient;
|
|
|
19 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
20 |
import org.apache.http.client.methods.HttpPost;
|
|
|
21 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
22 |
import org.apache.log4j.Logger;
|
|
|
23 |
|
|
|
24 |
import com.google.gson.Gson;
|
|
|
25 |
import com.google.gson.GsonBuilder;
|
|
|
26 |
import com.google.gson.JsonArray;
|
|
|
27 |
import com.google.gson.JsonDeserializationContext;
|
|
|
28 |
import com.google.gson.JsonDeserializer;
|
|
|
29 |
import com.google.gson.JsonElement;
|
|
|
30 |
import com.google.gson.JsonObject;
|
|
|
31 |
import com.google.gson.JsonPrimitive;
|
|
|
32 |
|
|
|
33 |
public class PayuPaymentHandler implements IPaymentHandler {
|
|
|
34 |
|
|
|
35 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
36 |
|
|
|
37 |
public static final String TXN_ID = "transactionId";
|
|
|
38 |
public static final String PAYMENT_ID = "paymentId";
|
|
|
39 |
public static final String AMOUNT = "amount";
|
|
|
40 |
public static final String DATE_TIME = "dateTime";
|
|
|
41 |
public static final String MODE = "mode";
|
|
|
42 |
public static final String REF_NO = "referenceNo";
|
|
|
43 |
public static final String TXN_TYPE = "transactionType";
|
|
|
44 |
|
|
|
45 |
private static String apiUrl;
|
|
|
46 |
private static String accountId;
|
|
|
47 |
private static String secretKey;
|
|
|
48 |
|
|
|
49 |
static {
|
|
|
50 |
try {
|
|
|
51 |
ConfigClient cc = ConfigClient.getClient();
|
|
|
52 |
accountId = cc.get("payu_account_id");
|
|
|
53 |
secretKey = cc.get("payu_secret_key");
|
|
|
54 |
apiUrl = cc.get("payu_api_url");
|
|
|
55 |
} catch (Exception e) {
|
|
|
56 |
log.error("Could not initialize Payu Handler!");
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public Map<String, String> captureTransaction(String paymentId,
|
|
|
61 |
String payuId) throws Exception {
|
|
|
62 |
Map<String, String> params = new HashMap<String, String>();
|
|
|
63 |
Map<String, String> resultMap = new HashMap<String, String>();
|
|
|
64 |
params.put("key", accountId);
|
|
|
65 |
params.put("command", "capture_transaction");
|
|
|
66 |
params.put("var1", payuId);
|
|
|
67 |
params.put("var2", new Date().getTime() + "");
|
|
|
68 |
try {
|
|
|
69 |
params.put("hash", getSecureHash(params));
|
|
|
70 |
} catch (Exception e) {
|
|
|
71 |
resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
|
|
72 |
resultMap.put(ERROR, Errors.CONN_FAILURE.message);
|
|
|
73 |
return resultMap;
|
|
|
74 |
}
|
|
|
75 |
String postResponse = "";
|
|
|
76 |
try {
|
|
|
77 |
postResponse = executePost(apiUrl, getUrlParams(params));
|
|
|
78 |
} catch (Exception e) {
|
|
|
79 |
resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
|
|
80 |
resultMap.put(ERROR, Errors.CONN_FAILURE.message);
|
|
|
81 |
}
|
|
|
82 |
GsonBuilder gsonBuilder = new GsonBuilder();
|
|
|
83 |
gsonBuilder
|
|
|
84 |
.registerTypeAdapter(Object.class, new NaturalDeserializer());
|
|
|
85 |
Gson gson = gsonBuilder.create();
|
|
|
86 |
Map<String, Object> postMap = (Map<String, Object>) gson.fromJson(
|
|
|
87 |
postResponse, Object.class);
|
|
|
88 |
if ("1".equals(postMap.get("status"))) {
|
|
|
89 |
resultMap.put(CAPTURE_TXN_ID, params.get("var2"));
|
|
|
90 |
resultMap.put(PAYMENT_ID, paymentId);
|
|
|
91 |
resultMap.put(REF_NO, (String) postMap.get("bank_ref_num"));
|
|
|
92 |
}
|
|
|
93 |
if ("0".equals(equals(postMap.get("status"))
|
|
|
94 |
&& "Capture request failed".equals(postMap.get("status")))) {
|
|
|
95 |
resultMap.put(ERR_CODE, Errors.CAPTURE_FAILURE.code);
|
|
|
96 |
resultMap.put(ERROR, Errors.CAPTURE_FAILURE.message);
|
|
|
97 |
}
|
|
|
98 |
return resultMap;
|
|
|
99 |
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
private List<NameValuePair> getUrlParams(Map<String, String> params) {
|
|
|
103 |
// TODO Auto-generated method stub
|
|
|
104 |
return null;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
private String getSecureHash(Map<String, String> params)
|
|
|
108 |
throws NoSuchAlgorithmException {
|
|
|
109 |
StringBuffer sb = new StringBuffer();
|
|
|
110 |
String passString = sb.append(params.get("key")).append("|")
|
|
|
111 |
.append(params.get("command")).append("|")
|
|
|
112 |
.append(params.get("var1")).append("|").append(secretKey)
|
|
|
113 |
.toString();
|
|
|
114 |
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
|
|
115 |
md.update(passString.getBytes(), 0, passString.getBytes().length);
|
|
|
116 |
byte[] mdbytes = md.digest();
|
|
|
117 |
// convert the byte to hex format method
|
|
|
118 |
StringBuffer sbNew = new StringBuffer();
|
|
|
119 |
for (int i = 0; i < mdbytes.length; i++) {
|
|
|
120 |
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16)
|
|
|
121 |
.substring(1));
|
|
|
122 |
}
|
|
|
123 |
return sb.toString();
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
public static String executePost(String targetURL,
|
|
|
127 |
List<NameValuePair> nameValuePairs) throws Exception {
|
|
|
128 |
HttpClient client = new DefaultHttpClient();
|
|
|
129 |
HttpPost post = new HttpPost(targetURL);
|
|
|
130 |
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
|
|
131 |
HttpResponse response = client.execute(post);
|
|
|
132 |
BufferedReader rd = new BufferedReader(new InputStreamReader(response
|
|
|
133 |
.getEntity().getContent()));
|
|
|
134 |
String line = "";
|
|
|
135 |
StringBuffer sb = new StringBuffer();
|
|
|
136 |
while ((line = rd.readLine()) != null) {
|
|
|
137 |
sb.append(line);
|
|
|
138 |
}
|
|
|
139 |
return sb.toString();
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
private static class NaturalDeserializer implements
|
|
|
143 |
JsonDeserializer<Object> {
|
|
|
144 |
public Object deserialize(JsonElement json, Type typeOfT,
|
|
|
145 |
JsonDeserializationContext context) {
|
|
|
146 |
if (json.isJsonNull())
|
|
|
147 |
return null;
|
|
|
148 |
else if (json.isJsonPrimitive())
|
|
|
149 |
return handlePrimitive(json.getAsJsonPrimitive());
|
|
|
150 |
else if (json.isJsonArray())
|
|
|
151 |
return handleArray(json.getAsJsonArray(), context);
|
|
|
152 |
else
|
|
|
153 |
return handleObject(json.getAsJsonObject(), context);
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
private Object handlePrimitive(JsonPrimitive json) {
|
|
|
157 |
if (json.isBoolean())
|
|
|
158 |
return json.getAsBoolean();
|
|
|
159 |
else if (json.isString())
|
|
|
160 |
return json.getAsString();
|
|
|
161 |
else {
|
|
|
162 |
BigDecimal bigDec = json.getAsBigDecimal();
|
|
|
163 |
// Find out if it is an int type
|
|
|
164 |
try {
|
|
|
165 |
bigDec.toBigIntegerExact();
|
|
|
166 |
try {
|
|
|
167 |
return bigDec.intValueExact();
|
|
|
168 |
} catch (ArithmeticException e) {
|
|
|
169 |
}
|
|
|
170 |
return bigDec.longValue();
|
|
|
171 |
} catch (ArithmeticException e) {
|
|
|
172 |
}
|
|
|
173 |
// Just return it as a double
|
|
|
174 |
return bigDec.doubleValue();
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
private Object handleArray(JsonArray json,
|
|
|
179 |
JsonDeserializationContext context) {
|
|
|
180 |
Object[] array = new Object[json.size()];
|
|
|
181 |
for (int i = 0; i < array.length; i++)
|
|
|
182 |
array[i] = context.deserialize(json.get(i), Object.class);
|
|
|
183 |
return array;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
private Object handleObject(JsonObject json,
|
|
|
187 |
JsonDeserializationContext context) {
|
|
|
188 |
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
189 |
for (Map.Entry<String, JsonElement> entry : json.entrySet())
|
|
|
190 |
map.put(entry.getKey(),
|
|
|
191 |
context.deserialize(entry.getValue(), Object.class));
|
|
|
192 |
return map;
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
}
|