| Line 3... |
Line 3... |
| 3 |
import in.shop2020.config.ConfigException;
|
3 |
import in.shop2020.config.ConfigException;
|
| 4 |
import in.shop2020.payments.Attribute;
|
4 |
import in.shop2020.payments.Attribute;
|
| 5 |
import in.shop2020.payments.Payment;
|
5 |
import in.shop2020.payments.Payment;
|
| 6 |
import in.shop2020.payments.PaymentStatus;
|
6 |
import in.shop2020.payments.PaymentStatus;
|
| 7 |
import in.shop2020.payments.PaymentService.Client;
|
7 |
import in.shop2020.payments.PaymentService.Client;
|
| 8 |
import in.shop2020.serving.services.IPaymentService.Errors;
|
- |
|
| 9 |
import in.shop2020.thrift.clients.PaymentServiceClient;
|
8 |
import in.shop2020.thrift.clients.PaymentServiceClient;
|
| 10 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
9 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
| 11 |
|
10 |
|
| 12 |
import java.io.BufferedReader;
|
- |
|
| 13 |
import java.io.DataOutputStream;
|
- |
|
| 14 |
import java.io.IOException;
|
- |
|
| 15 |
import java.io.InputStreamReader;
|
- |
|
| 16 |
import java.io.StringReader;
|
- |
|
| 17 |
import java.net.MalformedURLException;
|
- |
|
| 18 |
import java.net.URL;
|
- |
|
| 19 |
import java.net.URLConnection;
|
- |
|
| 20 |
import java.net.URLEncoder;
|
- |
|
| 21 |
import java.util.ArrayList;
|
11 |
import java.util.ArrayList;
|
| 22 |
import java.util.HashMap;
|
12 |
import java.util.HashMap;
|
| 23 |
import java.util.List;
|
13 |
import java.util.List;
|
| 24 |
import java.util.Map;
|
14 |
import java.util.Map;
|
| 25 |
import java.util.Map.Entry;
|
- |
|
| 26 |
|
- |
|
| 27 |
import javax.xml.xpath.XPath;
|
- |
|
| 28 |
import javax.xml.xpath.XPathConstants;
|
- |
|
| 29 |
import javax.xml.xpath.XPathExpressionException;
|
- |
|
| 30 |
import javax.xml.xpath.XPathFactory;
|
- |
|
| 31 |
|
15 |
|
| 32 |
import org.apache.log4j.Logger;
|
16 |
import org.apache.log4j.Logger;
|
| 33 |
import org.w3c.dom.Element;
|
- |
|
| 34 |
import org.w3c.dom.NodeList;
|
- |
|
| 35 |
import org.xml.sax.InputSource;
|
- |
|
| 36 |
|
17 |
|
| 37 |
public class EbsPaymentService implements IPaymentService{
|
18 |
public class EbsPaymentService implements IPaymentService{
|
| 38 |
|
19 |
|
| 39 |
private static Logger log = Logger.getLogger(Class.class);
|
20 |
private static Logger log = Logger.getLogger(Class.class);
|
| 40 |
|
21 |
|
| Line 123... |
Line 104... |
| 123 |
resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
104 |
resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
| 124 |
resultMap.put(ERROR, "Unable to capture transaction.");
|
105 |
resultMap.put(ERROR, "Unable to capture transaction.");
|
| 125 |
}
|
106 |
}
|
| 126 |
|
107 |
|
| 127 |
return resultMap;
|
108 |
return resultMap;
|
| 128 |
|
- |
|
| 129 |
// /*
|
- |
|
| 130 |
// * HDFC Insanity
|
- |
|
| 131 |
// *
|
- |
|
| 132 |
// * If we don't set this property, all payments through HDFC will fail if
|
- |
|
| 133 |
// * the first payment is made through EBS.
|
- |
|
| 134 |
// *
|
- |
|
| 135 |
// * The JAR file provided by HDFC requires that the instances of
|
- |
|
| 136 |
// * HttpsURLConnection returned by a call to URL.openConnection be an
|
- |
|
| 137 |
// * instance of com.sun.net.ssl.HttpsURLConnection. Java versions before
|
- |
|
| 138 |
// * 1.4 used to return an instance of this class which goes against the
|
- |
|
| 139 |
// * current policy of using instances of undocumented internal classes of
|
- |
|
| 140 |
// * JDK since they can change at any time. This behaviour was changed in
|
- |
|
| 141 |
// * JAVA 1.4 to return instances of javax.net.ssl.HttpsURLConnection.
|
- |
|
| 142 |
// * However, it appears, that to allow clients with JDK 1.4 and earlier,
|
- |
|
| 143 |
// * HDFC favours the use of the deprecated class.
|
- |
|
| 144 |
// */
|
- |
|
| 145 |
// System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
|
- |
|
| 146 |
//
|
- |
|
| 147 |
// try {
|
- |
|
| 148 |
// // URL of CGI-Bin script.
|
- |
|
| 149 |
// url = new URL("https://secure.ebs.in/api/1_0");
|
- |
|
| 150 |
// // URL connection channel.
|
- |
|
| 151 |
// urlConn = url.openConnection();
|
- |
|
| 152 |
// //urlConn.setRequestMethod("POST");
|
- |
|
| 153 |
// } catch (MalformedURLException e1) {
|
- |
|
| 154 |
// log.error(Errors.CONN_FAILURE.message, e1);
|
- |
|
| 155 |
// resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
- |
|
| 156 |
// resultMap.put(ERROR, Errors.CONN_FAILURE.message);
|
- |
|
| 157 |
// return resultMap;
|
- |
|
| 158 |
// } catch (IOException e) {
|
- |
|
| 159 |
// log.error("Unable to initialize connection to EBS API server", e);
|
- |
|
| 160 |
// resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
|
- |
|
| 161 |
// resultMap.put(ERROR, Errors.CONN_FAILURE.message);
|
- |
|
| 162 |
// return resultMap;
|
- |
|
| 163 |
// }
|
- |
|
| 164 |
//
|
- |
|
| 165 |
// // Let the run-time system (RTS) know that we want input.
|
- |
|
| 166 |
// urlConn.setDoInput (true);
|
- |
|
| 167 |
// // Let the RTS know that we want to do output.
|
- |
|
| 168 |
// urlConn.setDoOutput (true);
|
- |
|
| 169 |
// // No caching, we want the real thing.
|
- |
|
| 170 |
// urlConn.setUseCaches (false);
|
- |
|
| 171 |
// // Specify the content type.
|
- |
|
| 172 |
// urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
- |
|
| 173 |
// // Send POST output.
|
- |
|
| 174 |
// try {
|
- |
|
| 175 |
// printout = new DataOutputStream (urlConn.getOutputStream ());
|
- |
|
| 176 |
// String content =
|
- |
|
| 177 |
// "Action=" + URLEncoder.encode("capture", "UTF-8") +
|
- |
|
| 178 |
// "&AccountID=" + URLEncoder.encode(accountId, "UTF-8") +
|
- |
|
| 179 |
// "&SecretKey=" + URLEncoder.encode(secretKey, "UTF-8") +
|
- |
|
| 180 |
// "&Amount=" + URLEncoder.encode(""+amount, "UTF-8") +
|
- |
|
| 181 |
// "&PaymentID=" + URLEncoder.encode(paymentId, "UTF-8") +
|
- |
|
| 182 |
// "&submitted=Submit";
|
- |
|
| 183 |
// printout.writeBytes (content);
|
- |
|
| 184 |
// printout.flush ();
|
- |
|
| 185 |
// printout.close ();
|
- |
|
| 186 |
//
|
- |
|
| 187 |
// // Get response data.
|
- |
|
| 188 |
// reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
|
- |
|
| 189 |
// resultMap = parseCaptureOutput(reader);
|
- |
|
| 190 |
// reader.close ();
|
- |
|
| 191 |
// } catch (IOException e) {
|
- |
|
| 192 |
// log.error(Errors.CAPTURE_FAILURE.message, e);
|
- |
|
| 193 |
// resultMap.clear();
|
- |
|
| 194 |
// resultMap.put(STATUS, "");
|
- |
|
| 195 |
// resultMap.put(ERR_CODE, Errors.CAPTURE_FAILURE.code);
|
- |
|
| 196 |
// resultMap.put(ERROR, Errors.CAPTURE_FAILURE.message);
|
- |
|
| 197 |
// }
|
- |
|
| 198 |
// return resultMap;
|
- |
|
| 199 |
}
|
109 |
}
|
| 200 |
|
110 |
|
| 201 |
/**
|
- |
|
| 202 |
* Parses the given input stream and returns a map containing the
|
- |
|
| 203 |
* transaction details
|
- |
|
| 204 |
*
|
- |
|
| 205 |
* @param reader The reader containing the response of the capture request.
|
- |
|
| 206 |
* @return A Map. The Map will definitely have status and will have error
|
- |
|
| 207 |
* information in case of error and txn information in case of
|
- |
|
| 208 |
* success.
|
- |
|
| 209 |
*/
|
- |
|
| 210 |
private static Map<String, String> parseCaptureOutput(BufferedReader reader){
|
- |
|
| 211 |
Map<String, String> resultMap = new HashMap<String, String>();
|
- |
|
| 212 |
resultMap.put(STATUS, "");
|
- |
|
| 213 |
|
- |
|
| 214 |
String str = null;
|
- |
|
| 215 |
try {
|
- |
|
| 216 |
str = reader.readLine();
|
- |
|
| 217 |
log.info("Capture response: " + str);
|
- |
|
| 218 |
} catch (IOException e) {
|
- |
|
| 219 |
log.error("Error reading the capture response:", e);
|
- |
|
| 220 |
return resultMap;
|
- |
|
| 221 |
}
|
- |
|
| 222 |
|
- |
|
| 223 |
InputSource inputSource = new InputSource(new StringReader(str));
|
- |
|
| 224 |
XPath xpath = XPathFactory.newInstance().newXPath();
|
- |
|
| 225 |
String expression = "/output";
|
- |
|
| 226 |
NodeList nodes = null;
|
- |
|
| 227 |
try {
|
- |
|
| 228 |
nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
|
- |
|
| 229 |
} catch(XPathExpressionException xpee) {
|
- |
|
| 230 |
log.error("Input couldn't be parsed. See capture response for more info.");
|
- |
|
| 231 |
return resultMap;
|
- |
|
| 232 |
}
|
- |
|
| 233 |
|
- |
|
| 234 |
Element elem = (Element) nodes.item(0);
|
- |
|
| 235 |
String status = elem.getAttribute(STATUS);
|
- |
|
| 236 |
resultMap.put(STATUS, status);
|
- |
|
| 237 |
if("".equals(status) || !"Processing".equals(status)){
|
- |
|
| 238 |
//We've received an error. Retrieve the error values
|
- |
|
| 239 |
resultMap.put(ERR_CODE, elem.getAttribute(ERR_CODE));
|
- |
|
| 240 |
resultMap.put(ERROR, elem.getAttribute(ERROR));
|
- |
|
| 241 |
}else{
|
- |
|
| 242 |
resultMap.put(CAPTURE_TXN_ID, elem.getAttribute(TXN_ID));
|
- |
|
| 243 |
resultMap.put(PAYMENT_ID, elem.getAttribute(PAYMENT_ID));
|
- |
|
| 244 |
resultMap.put(AMOUNT, elem.getAttribute(AMOUNT));
|
- |
|
| 245 |
resultMap.put(CAPTURE_TIME, elem.getAttribute(DATE_TIME));
|
- |
|
| 246 |
resultMap.put(MODE, elem.getAttribute(MODE));
|
- |
|
| 247 |
resultMap.put(REF_NO, elem.getAttribute(REF_NO));
|
- |
|
| 248 |
resultMap.put(TXN_TYPE, elem.getAttribute(TXN_TYPE));
|
- |
|
| 249 |
}
|
- |
|
| 250 |
|
- |
|
| 251 |
log.info("Parsed capture response:");
|
- |
|
| 252 |
for(Entry<String, String> entry : resultMap.entrySet()){
|
- |
|
| 253 |
log.info("Key: " + entry.getKey() + ", Value: " + entry.getValue());
|
- |
|
| 254 |
}
|
- |
|
| 255 |
|
- |
|
| 256 |
return resultMap;
|
- |
|
| 257 |
}
|
- |
|
| 258 |
|
- |
|
| 259 |
public static void main(String[] args){
|
111 |
public static void main(String[] args){
|
| 260 |
//capturePayment(30450.00, "2412653");
|
112 |
//capturePayment(30450.00, "2412653");
|
| 261 |
|
113 |
|
| 262 |
// <output transactionId="4793507" paymentId="2411078" amount="25005" dateTime="2011-05-16 09:03:15" mode="TEST" referenceNo="4" transactionType="Captured" status="Processing" />";
|
114 |
// <output transactionId="4793507" paymentId="2411078" amount="25005" dateTime="2011-05-16 09:03:15" mode="TEST" referenceNo="4" transactionType="Captured" status="Processing" />";
|
| 263 |
|
115 |
|