Subversion Repositories SmartDukaan

Rev

Rev 2334 | Rev 2899 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2334 Rev 2708
Line 1... Line 1...
1
package in.shop2020.serving.services;
1
package in.shop2020.serving.services;
2
 
2
 
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.PaymentStatus;
6
import in.shop2020.payments.PaymentStatus;
-
 
7
import in.shop2020.payments.PaymentService.Client;
-
 
8
import in.shop2020.serving.services.IPaymentService.Errors;
6
import in.shop2020.thrift.clients.PaymentServiceClient;
9
import in.shop2020.thrift.clients.PaymentServiceClient;
7
import in.shop2020.thrift.clients.config.ConfigClient;
10
import in.shop2020.thrift.clients.config.ConfigClient;
8
 
11
 
9
import java.io.BufferedReader;
12
import java.io.BufferedReader;
10
import java.io.DataOutputStream;
13
import java.io.DataOutputStream;
Line 94... Line 97...
94
	 * succeed.
97
	 * succeed.
95
	 * 
98
	 * 
96
	 * @param amount
99
	 * @param amount
97
	 *            The amount to be captured. Must be the same value which was
100
	 *            The amount to be captured. Must be the same value which was
98
	 *            authorized for this payment.
101
	 *            authorized for this payment.
99
	 * @param paymentId
102
	 * @param gatewayPaymentId
100
	 *            The payment ID generated by the gateway.
103
	 *            The payment ID generated by the gateway.
101
	 * @return A Map. The Map will definitely have status and will have error
104
	 * @return A Map. The Map will definitely have status and will have error
102
	 *         information in case of error and txn information in case of
105
	 *         information in case of error and txn information in case of
103
	 *         success. In case there is an error in processing, the returned
106
	 *         success. In case there is an error in processing, the returned
104
	 *         result map will be empty.
107
	 *         result map will be empty.
105
	 */
108
	 */
106
	public static Map<String, String> capturePayment(double amount, String paymentId){
109
	public static Map<String, String> capturePayment(Payment payment, String gatewayPaymentId){
-
 
110
		String amount = "" + payment.getAmount();
107
		log.info("Capturing amount: Rs " + amount + " for payment Id: " + paymentId);
111
		log.info("Capturing amount: Rs " + amount + " for payment Id: " + gatewayPaymentId);
108
		URL url = null;
-
 
109
	    URLConnection urlConn = null;
-
 
110
	    DataOutputStream printout;
-
 
111
	    BufferedReader reader;
-
 
112
	    Map<String, String> resultMap = new HashMap<String, String>();
112
	    Map<String, String> resultMap = new HashMap<String, String>();
113
 
113
 
114
	    //Prepare resultMap to elicit failure behaviour in case anything goes wrong.
114
	    //Prepare resultMap to elicit failure behaviour in case anything goes wrong.
115
	    resultMap.put(STATUS, "");
115
	    resultMap.put(STATUS, "");
116
	    
116
	    
117
		/*
-
 
118
		 * HDFC Insanity
-
 
119
		 * 
117
		try {
120
		 * If we don't set this property, all payments through HDFC will fail if
118
			PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
121
		 * the first payment is made through EBS.
119
			Client paymentClient = paymentServiceClient.getClient();
122
		 * 
-
 
123
		 * The JAR file provided by HDFC requires that the instances of
-
 
124
		 * HttpsURLConnection returned by a call to URL.openConnection be an
-
 
125
		 * instance of com.sun.net.ssl.HttpsURLConnection. Java versions before
-
 
126
		 * 1.4 used to return an instance of this class which goes against the
-
 
127
		 * current policy of using instances of undocumented internal classes of
-
 
128
		 * JDK since they can change at any time. This behaviour was changed in
-
 
129
		 * JAVA 1.4 to return instances of javax.net.ssl.HttpsURLConnection.
-
 
130
		 * However, it appears, that to allow clients with JDK 1.4 and earlier,
-
 
131
		 * HDFC favours the use of the deprecated class.
-
 
132
		 */
-
 
133
	    System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
-
 
134
	    
-
 
135
	    try {
-
 
136
		    // URL of CGI-Bin script.
-
 
137
			url = new URL("https://secure.ebs.in/api/1_0");
-
 
138
		    // URL connection channel.
-
 
139
		    urlConn = url.openConnection();
-
 
140
		    //urlConn.setRequestMethod("POST");
-
 
141
	    } catch (MalformedURLException e1) {
-
 
142
			log.error(Errors.CONN_FAILURE.message, e1);
-
 
143
			resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
-
 
144
			resultMap.put(ERROR, Errors.CONN_FAILURE.message);
120
			resultMap = paymentClient.captureEbsPayment(payment.getPaymentId());
145
			return resultMap;
-
 
146
		} catch (IOException e) {
121
		} catch (Exception e) {
147
			log.error("Unable to initialize connection to EBS API server", e);
122
			log.error("Unable to capture payment", e);
148
			resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
123
			resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
149
			resultMap.put(ERROR, Errors.CONN_FAILURE.message);
124
			resultMap.put(ERROR, "Unable to capture transaction.");
150
			return resultMap;
-
 
151
		}
125
		}
-
 
126
	    
-
 
127
		return resultMap;
152
 
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
//
153
	    // Let the run-time system (RTS) know that we want input.
165
//	    // Let the run-time system (RTS) know that we want input.
154
	    urlConn.setDoInput (true);
166
//	    urlConn.setDoInput (true);
155
	    // Let the RTS know that we want to do output.
167
//	    // Let the RTS know that we want to do output.
156
	    urlConn.setDoOutput (true);
168
//	    urlConn.setDoOutput (true);
157
	    // No caching, we want the real thing.
169
//	    // No caching, we want the real thing.
158
	    urlConn.setUseCaches (false);
170
//	    urlConn.setUseCaches (false);
159
	    // Specify the content type.
171
//	    // Specify the content type.
160
	    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
172
//	    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
161
	    // Send POST output.
173
//	    // Send POST output.
162
	    try {
174
//	    try {
163
			printout = new DataOutputStream (urlConn.getOutputStream ());
175
//			printout = new DataOutputStream (urlConn.getOutputStream ());
164
		    String content =
176
//		    String content =
165
			    "Action=" + URLEncoder.encode("capture", "UTF-8") +
177
//			    "Action=" + URLEncoder.encode("capture", "UTF-8") +
166
			    "&AccountID=" + URLEncoder.encode(accountId, "UTF-8") +
178
//			    "&AccountID=" + URLEncoder.encode(accountId, "UTF-8") +
167
			    "&SecretKey=" + URLEncoder.encode(secretKey, "UTF-8") +
179
//			    "&SecretKey=" + URLEncoder.encode(secretKey, "UTF-8") +
168
			    "&Amount=" + URLEncoder.encode(""+amount, "UTF-8") +
180
//			    "&Amount=" + URLEncoder.encode(""+amount, "UTF-8") +
169
			    "&PaymentID=" + URLEncoder.encode(paymentId, "UTF-8") +
181
//			    "&PaymentID=" + URLEncoder.encode(paymentId, "UTF-8") +
170
			    "&submitted=Submit";
182
//			    "&submitted=Submit";
171
		    printout.writeBytes (content);
183
//		    printout.writeBytes (content);
172
		    printout.flush ();
184
//		    printout.flush ();
173
		    printout.close ();
185
//		    printout.close ();
174
 
186
//
175
		    // Get response data.
187
//		    // Get response data.
176
		    reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
188
//		    reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
177
		    resultMap = parseCaptureOutput(reader);
189
//		    resultMap = parseCaptureOutput(reader);
178
		    reader.close ();
190
//		    reader.close ();
179
	    } catch (IOException e) {
191
//	    } catch (IOException e) {
180
			log.error(Errors.CAPTURE_FAILURE.message, e);
192
//			log.error(Errors.CAPTURE_FAILURE.message, e);
181
			resultMap.clear();
193
//			resultMap.clear();
182
			resultMap.put(STATUS, "");
194
//			resultMap.put(STATUS, "");
183
			resultMap.put(ERR_CODE, Errors.CAPTURE_FAILURE.code);
195
//			resultMap.put(ERR_CODE, Errors.CAPTURE_FAILURE.code);
184
			resultMap.put(ERROR, Errors.CAPTURE_FAILURE.message);
196
//			resultMap.put(ERROR, Errors.CAPTURE_FAILURE.message);
185
		}
197
//		}
186
	    return resultMap;
198
//	    return resultMap;
187
	}
199
	}
188
 
200
 
189
	/**
201
	/**
190
	 * Parses the given input stream and returns a map containing the
202
	 * Parses the given input stream and returns a map containing the
191
	 * transaction details
203
	 * transaction details
Line 243... Line 255...
243
		
255
		
244
		return resultMap;
256
		return resultMap;
245
	}
257
	}
246
	
258
	
247
	public static void main(String[] args){
259
	public static void main(String[] args){
248
		capturePayment(30450.00, "2412653");
260
		//capturePayment(30450.00, "2412653");
249
		
261
		
250
//		<output  transactionId="4793507"  paymentId="2411078"  amount="25005"  dateTime="2011-05-16 09:03:15"  mode="TEST"  referenceNo="4"  transactionType="Captured"  status="Processing"  />";
262
//		<output  transactionId="4793507"  paymentId="2411078"  amount="25005"  dateTime="2011-05-16 09:03:15"  mode="TEST"  referenceNo="4"  transactionType="Captured"  status="Processing"  />";
251
 
263
 
252
//		<output  errorCode="2"  error="Invalid Account ID/Secret Key"  />
264
//		<output  errorCode="2"  error="Invalid Account ID/Secret Key"  />
253
//		<output  errorCode="12"  error="This payment is failed"  />
265
//		<output  errorCode="12"  error="This payment is failed"  />