Subversion Repositories SmartDukaan

Rev

Rev 1959 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1905 chandransh 1
package in.shop2020.serving.services;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.thrift.clients.config.ConfigClient;
5
 
6
import java.io.DataInputStream;
7
import java.io.DataOutputStream;
8
import java.io.IOException;
9
import java.io.StringReader;
10
import java.net.MalformedURLException;
11
import java.net.URL;
12
import java.net.URLConnection;
13
import java.net.URLEncoder;
14
import java.util.HashMap;
15
import java.util.Map;
16
import java.util.Map.Entry;
17
 
18
import javax.xml.xpath.XPath;
19
import javax.xml.xpath.XPathConstants;
20
import javax.xml.xpath.XPathExpressionException;
21
import javax.xml.xpath.XPathFactory;
22
 
23
import org.apache.log4j.Logger;
24
import org.w3c.dom.Element;
25
import org.w3c.dom.NodeList;
26
import org.xml.sax.InputSource;
27
 
28
public class EbsPaymentService implements IPaymentService{
29
 
30
	private static Logger log = Logger.getLogger(Class.class);
31
 
32
	public static final String STATUS = "status";
33
	public static final String ERR_CODE = "errorCode";
34
	public static final String ERROR = "error";
35
	public static final String TXN_ID = "transactionId";
36
	public static final String PAYMENT_ID = "paymentId";
37
	public static final String AMOUNT = "amount";
38
	public static final String DATE_TIME = "dateTime";
39
	public static final String MODE = "mode";
40
	public static final String REF_NO = "referenceNo";
41
	public static final String TXN_TYPE = "transactionType";
42
 
43
 
44
    private static String accountId;
45
    private static String secretKey;
46
 
47
	static{
48
		try {
49
			accountId = ConfigClient.getClient().get("ebs_account_id");
50
			secretKey = ConfigClient.getClient().get("ebs_secret_key");
51
		} catch (ConfigException e) {
52
			log.error("Unable to get EBS payment configuration.");
53
		}
54
	}
55
 
56
	private int gatewayId=2;
57
	private double amount;
58
	private long paymentId;
59
 
60
	public long createPayment(long currentCartId, long userId, long txnId){
61
		log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId);
62
		CommonPaymentService cps = new CommonPaymentService();
63
		if(!cps.createPayment(currentCartId, userId, txnId, gatewayId)){
64
			log.error("Error while creating the basic payment");
65
			return PAYMENT_NOT_CREATED;
66
		}
67
 
68
		paymentId = cps.getPaymentId();
69
		amount = cps.getAmount();
70
		return paymentId;
71
	}
72
 
73
	public static Map<String, String> capturePayment(double amount, String paymentId){
74
		System.out.println("Capturing amount: Rs " + amount + " for payment Id: " + paymentId);
75
		URL url = null;
76
	    URLConnection urlConn = null;
77
	    DataOutputStream printout;
78
	    DataInputStream input;
79
	    Map<String, String> resultMap = null;
80
	    try {
81
		    // URL of CGI-Bin script.
82
			url = new URL ("https://secure.ebs.in/api/1_0");
83
		    // URL connection channel.
84
		    urlConn = url.openConnection();
85
		    //urlConn.setRequestMethod("POST");
86
	    } catch (MalformedURLException e1) {
87
			log.error("Unable to initialize connection to EBS API server", e1);
88
		} catch (IOException e) {
89
			log.error("Unable to initialize connection to EBS API server", e);
90
			e.printStackTrace();
91
		}
92
 
93
	    // Let the run-time system (RTS) know that we want input.
94
	    urlConn.setDoInput (true);
95
	    // Let the RTS know that we want to do output.
96
	    urlConn.setDoOutput (true);
97
	    // No caching, we want the real thing.
98
	    urlConn.setUseCaches (false);
99
	    // Specify the content type.
100
	    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
101
	    // Send POST output.
102
	    try {
103
			printout = new DataOutputStream (urlConn.getOutputStream ());
104
		    String content =
105
			    "Action=" + URLEncoder.encode("capture") +
106
			    "&AccountID=" + URLEncoder.encode(accountId) +
107
			    "&SecretKey=" + URLEncoder.encode(secretKey) +
108
			    "&Amount=" + URLEncoder.encode(""+amount) +
109
			    "&PaymentID=" + URLEncoder.encode(paymentId) +
110
			    "&submitted=Submit";
111
		    printout.writeBytes (content);
112
		    printout.flush ();
113
		    printout.close ();
114
 
115
		    // Get response data.
116
		    input = new DataInputStream (urlConn.getInputStream ());
117
		    resultMap = parseCaptureOutput(input);
118
		    input.close ();
119
	    } catch (IOException e) {
120
			// TODO Auto-generated catch block
121
			e.printStackTrace();
122
		}
123
	    return resultMap;
124
	}
125
 
126
	private static Map<String, String> parseCaptureOutput(DataInputStream in){
127
		Map<String, String> resultMap = new HashMap<String, String>();
128
		resultMap.put(STATUS, "");
129
 
130
		String str = null;
131
		try {
132
			str = in.readLine();
133
			System.out.println(str);
134
			log.info("Capture response: " + str);
135
		} catch (IOException e) {
136
			log.error("Error reading the capture response:", e);
137
			return resultMap;
138
		}
139
 
140
		InputSource inputSource = new InputSource(new StringReader(str));
141
		XPath xpath = XPathFactory.newInstance().newXPath();
142
		String expression = "/output";
143
		NodeList nodes = null;
144
		try {
145
			nodes = (NodeList) xpath.evaluate(expression, inputSource,	XPathConstants.NODESET);
146
		} catch(XPathExpressionException xpee) {
147
			log.error("Input couldn't be parsed. See capture response for more info.");
148
			return resultMap;
149
		}
150
 
151
		Element elem = (Element) nodes.item(0);
152
		String status = elem.getAttribute(STATUS);
153
		resultMap.put(STATUS, status);
154
		if("".equals(status) || !"Processing".equals(status)){
155
			//We've received an error. Retrieve the error values
156
			resultMap.put(ERR_CODE, elem.getAttribute(ERR_CODE));
157
			resultMap.put(ERROR, elem.getAttribute(ERROR));
158
		}else{
159
			resultMap.put(TXN_ID, elem.getAttribute(TXN_ID));
160
			resultMap.put(PAYMENT_ID, elem.getAttribute(PAYMENT_ID));
161
			resultMap.put(AMOUNT, elem.getAttribute(AMOUNT));
162
			resultMap.put(DATE_TIME, elem.getAttribute(DATE_TIME));
163
			resultMap.put(MODE, elem.getAttribute(MODE));
164
			resultMap.put(REF_NO, elem.getAttribute(REF_NO));
165
			resultMap.put(TXN_TYPE, elem.getAttribute(TXN_TYPE));
166
		}
167
 
168
		log.info("Parsed capture response:");
169
		for(Entry<String, String> entry : resultMap.entrySet()){
170
			log.info("Key: " + entry.getKey() + ", Value: " + entry.getValue());
171
		}
172
 
173
		return resultMap;
174
	}
175
 
176
	public static void main(String[] args){
177
		capturePayment(30450.00, "2412653");
178
 
179
//		<output  transactionId="4793507"  paymentId="2411078"  amount="25005"  dateTime="2011-05-16 09:03:15"  mode="TEST"  referenceNo="4"  transactionType="Captured"  status="Processing"  />";
180
 
181
//		<output  errorCode="2"  error="Invalid Account ID/Secret Key"  />
182
//		<output  errorCode="12"  error="This payment is failed"  />
183
//		<output  errorCode="13"  error="This payment is captured already"  />		
184
	}
185
}