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