Rev 2118 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.services;import in.shop2020.config.ConfigException;import in.shop2020.thrift.clients.config.ConfigClient;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.StringReader;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.net.URLEncoder;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathFactory;import org.apache.log4j.Logger;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;public class EbsPaymentService implements IPaymentService{private static Logger log = Logger.getLogger(Class.class);public static final String STATUS = "status";public static final String ERR_CODE = "errorCode";public static final String ERROR = "error";public static final String TXN_ID = "transactionId";public static final String PAYMENT_ID = "paymentId";public static final String AMOUNT = "amount";public static final String DATE_TIME = "dateTime";public static final String MODE = "mode";public static final String REF_NO = "referenceNo";public static final String TXN_TYPE = "transactionType";private static String accountId;private static String secretKey;static{try {accountId = ConfigClient.getClient().get("ebs_account_id");secretKey = ConfigClient.getClient().get("ebs_secret_key");} catch (ConfigException e) {log.error("Unable to get EBS payment configuration.");}}private int gatewayId=2;private double amount;private long paymentId;public long createPayment(long currentCartId, long userId, long txnId){log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId);CommonPaymentService cps = new CommonPaymentService();if(!cps.createPayment(currentCartId, userId, txnId, gatewayId)){log.error("Error while creating the basic payment");return PAYMENT_NOT_CREATED;}paymentId = cps.getPaymentId();amount = cps.getAmount();return paymentId;}public static Map<String, String> capturePayment(double amount, String paymentId){System.out.println("Capturing amount: Rs " + amount + " for payment Id: " + paymentId);URL url = null;URLConnection urlConn = null;DataOutputStream printout;DataInputStream input;Map<String, String> resultMap = null;try {// URL of CGI-Bin script.url = new URL ("https://secure.ebs.in/api/1_0");// URL connection channel.urlConn = url.openConnection();//urlConn.setRequestMethod("POST");} catch (MalformedURLException e1) {log.error("Unable to initialize connection to EBS API server", e1);} catch (IOException e) {log.error("Unable to initialize connection to EBS API server", e);e.printStackTrace();}// Let the run-time system (RTS) know that we want input.urlConn.setDoInput (true);// Let the RTS know that we want to do output.urlConn.setDoOutput (true);// No caching, we want the real thing.urlConn.setUseCaches (false);// Specify the content type.urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// Send POST output.try {printout = new DataOutputStream (urlConn.getOutputStream ());String content ="Action=" + URLEncoder.encode("capture") +"&AccountID=" + URLEncoder.encode(accountId) +"&SecretKey=" + URLEncoder.encode(secretKey) +"&Amount=" + URLEncoder.encode(""+amount) +"&PaymentID=" + URLEncoder.encode(paymentId) +"&submitted=Submit";printout.writeBytes (content);printout.flush ();printout.close ();// Get response data.input = new DataInputStream (urlConn.getInputStream ());resultMap = parseCaptureOutput(input);input.close ();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return resultMap;}private static Map<String, String> parseCaptureOutput(DataInputStream in){Map<String, String> resultMap = new HashMap<String, String>();resultMap.put(STATUS, "");String str = null;try {str = in.readLine();System.out.println(str);log.info("Capture response: " + str);} catch (IOException e) {log.error("Error reading the capture response:", e);return resultMap;}InputSource inputSource = new InputSource(new StringReader(str));XPath xpath = XPathFactory.newInstance().newXPath();String expression = "/output";NodeList nodes = null;try {nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);} catch(XPathExpressionException xpee) {log.error("Input couldn't be parsed. See capture response for more info.");return resultMap;}Element elem = (Element) nodes.item(0);String status = elem.getAttribute(STATUS);resultMap.put(STATUS, status);if("".equals(status) || !"Processing".equals(status)){//We've received an error. Retrieve the error valuesresultMap.put(ERR_CODE, elem.getAttribute(ERR_CODE));resultMap.put(ERROR, elem.getAttribute(ERROR));}else{resultMap.put(TXN_ID, elem.getAttribute(TXN_ID));resultMap.put(PAYMENT_ID, elem.getAttribute(PAYMENT_ID));resultMap.put(AMOUNT, elem.getAttribute(AMOUNT));resultMap.put(DATE_TIME, elem.getAttribute(DATE_TIME));resultMap.put(MODE, elem.getAttribute(MODE));resultMap.put(REF_NO, elem.getAttribute(REF_NO));resultMap.put(TXN_TYPE, elem.getAttribute(TXN_TYPE));}log.info("Parsed capture response:");for(Entry<String, String> entry : resultMap.entrySet()){log.info("Key: " + entry.getKey() + ", Value: " + entry.getValue());}return resultMap;}public static void main(String[] args){capturePayment(30450.00, "2412653");// <output transactionId="4793507" paymentId="2411078" amount="25005" dateTime="2011-05-16 09:03:15" mode="TEST" referenceNo="4" transactionType="Captured" status="Processing" />";// <output errorCode="2" error="Invalid Account ID/Secret Key" />// <output errorCode="12" error="This payment is failed" />// <output errorCode="13" error="This payment is captured already" />}}