Subversion Repositories SmartDukaan

Rev

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

Rev 1905 Rev 1959
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.thrift.clients.config.ConfigClient;
4
import in.shop2020.thrift.clients.config.ConfigClient;
5
 
5
 
6
import java.io.DataInputStream;
6
import java.io.BufferedReader;
7
import java.io.DataOutputStream;
7
import java.io.DataOutputStream;
8
import java.io.IOException;
8
import java.io.IOException;
-
 
9
import java.io.InputStreamReader;
9
import java.io.StringReader;
10
import java.io.StringReader;
10
import java.net.MalformedURLException;
11
import java.net.MalformedURLException;
11
import java.net.URL;
12
import java.net.URL;
12
import java.net.URLConnection;
13
import java.net.URLConnection;
13
import java.net.URLEncoder;
14
import java.net.URLEncoder;
Line 52... Line 53...
52
			log.error("Unable to get EBS payment configuration.");
53
			log.error("Unable to get EBS payment configuration.");
53
		}
54
		}
54
	}
55
	}
55
	
56
	
56
	private int gatewayId=2;
57
	private int gatewayId=2;
57
	private double amount;
-
 
58
	private long paymentId;
58
	private long paymentId;
59
	
59
	
-
 
60
	@Override
60
	public long createPayment(long currentCartId, long userId, long txnId){
61
	public long createPayment(long currentCartId, long userId, long txnId){
61
		log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId);
62
		log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId);
62
		CommonPaymentService cps = new CommonPaymentService();
63
		CommonPaymentService cps = new CommonPaymentService();
63
		if(!cps.createPayment(currentCartId, userId, txnId, gatewayId)){
64
		if(!cps.createPayment(currentCartId, userId, txnId, gatewayId)){
64
			log.error("Error while creating the basic payment");
65
			log.error("Error while creating the basic payment");
65
			return PAYMENT_NOT_CREATED;
66
			return PAYMENT_NOT_CREATED;
66
		}
67
		}
67
		
68
		
68
		paymentId = cps.getPaymentId();
69
		paymentId = cps.getPaymentId();
69
		amount = cps.getAmount();
-
 
70
		return paymentId;
70
		return paymentId;
71
	}
71
	}
72
	
72
 
-
 
73
	/**
-
 
74
	 * Capture the amount which was authorized for this payment Id. Makes
-
 
75
	 * requests over the network and so internet connectivity is must for it to
-
 
76
	 * succeed.
-
 
77
	 * 
-
 
78
	 * @param amount
-
 
79
	 *            The amount to be captured. Must be the same value which was
-
 
80
	 *            authorized for this payment.
-
 
81
	 * @param paymentId
-
 
82
	 *            The payment ID generated by the gateway.
-
 
83
	 * @return A Map. The Map will definitely have status and will have error
-
 
84
	 *         information in case of error and txn information in case of
-
 
85
	 *         success. In case there is an error in processing, the returned
-
 
86
	 *         result map will be empty.
-
 
87
	 */
73
	public static Map<String, String> capturePayment(double amount, String paymentId){
88
	public static Map<String, String> capturePayment(double amount, String paymentId){
74
		System.out.println("Capturing amount: Rs " + amount + " for payment Id: " + paymentId);
89
		System.out.println("Capturing amount: Rs " + amount + " for payment Id: " + paymentId);
75
		URL url = null;
90
		URL url = null;
76
	    URLConnection urlConn = null;
91
	    URLConnection urlConn = null;
77
	    DataOutputStream printout;
92
	    DataOutputStream printout;
78
	    DataInputStream input;
93
	    BufferedReader reader;
79
	    Map<String, String> resultMap = null;
94
	    Map<String, String> resultMap = new HashMap<String, String>();
-
 
95
 
-
 
96
		/*
-
 
97
		 * HDFC Insanity
-
 
98
		 * 
-
 
99
		 * If we don't set this property, all payments through HDFC will fail if
-
 
100
		 * the first payment is made through EBS.
-
 
101
		 * 
-
 
102
		 * The JAR file provided by HDFC requires that the instances of
-
 
103
		 * HttpsURLConnection returned by a call to URL.openConnection be an
-
 
104
		 * instance of com.sun.net.ssl.HttpsURLConnection. Java versions before
-
 
105
		 * 1.4 used to return an instance of this class which goes against the
-
 
106
		 * current policy of using instances of undocumented internal classes of
-
 
107
		 * JDK since they can change at any time. This behaviour was changed in
-
 
108
		 * JAVA 1.4 to return instances of javax.net.ssl.HttpsURLConnection.
-
 
109
		 * However, it appears, that to allow clients with JDK 1.4 and earlier,
-
 
110
		 * HDFC favours the use of the deprecated class.
-
 
111
		 */
-
 
112
	    System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
-
 
113
	    
80
	    try {
114
	    try {
81
		    // URL of CGI-Bin script.
115
		    // URL of CGI-Bin script.
82
			url = new URL ("https://secure.ebs.in/api/1_0");
116
			url = new URL("https://secure.ebs.in/api/1_0");
83
		    // URL connection channel.
117
		    // URL connection channel.
84
		    urlConn = url.openConnection();
118
		    urlConn = url.openConnection();
85
		    //urlConn.setRequestMethod("POST");
119
		    //urlConn.setRequestMethod("POST");
86
	    } catch (MalformedURLException e1) {
120
	    } catch (MalformedURLException e1) {
87
			log.error("Unable to initialize connection to EBS API server", e1);
121
			log.error("Unable to initialize connection to EBS API server", e1);
Line 100... Line 134...
100
	    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
134
	    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
101
	    // Send POST output.
135
	    // Send POST output.
102
	    try {
136
	    try {
103
			printout = new DataOutputStream (urlConn.getOutputStream ());
137
			printout = new DataOutputStream (urlConn.getOutputStream ());
104
		    String content =
138
		    String content =
105
			    "Action=" + URLEncoder.encode("capture") +
139
			    "Action=" + URLEncoder.encode("capture", "UTF-8") +
106
			    "&AccountID=" + URLEncoder.encode(accountId) +
140
			    "&AccountID=" + URLEncoder.encode(accountId, "UTF-8") +
107
			    "&SecretKey=" + URLEncoder.encode(secretKey) +
141
			    "&SecretKey=" + URLEncoder.encode(secretKey, "UTF-8") +
108
			    "&Amount=" + URLEncoder.encode(""+amount) +
142
			    "&Amount=" + URLEncoder.encode(""+amount, "UTF-8") +
109
			    "&PaymentID=" + URLEncoder.encode(paymentId) +
143
			    "&PaymentID=" + URLEncoder.encode(paymentId, "UTF-8") +
110
			    "&submitted=Submit";
144
			    "&submitted=Submit";
111
		    printout.writeBytes (content);
145
		    printout.writeBytes (content);
112
		    printout.flush ();
146
		    printout.flush ();
113
		    printout.close ();
147
		    printout.close ();
114
 
148
 
115
		    // Get response data.
149
		    // Get response data.
116
		    input = new DataInputStream (urlConn.getInputStream ());
150
		    reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
117
		    resultMap = parseCaptureOutput(input);
151
		    resultMap = parseCaptureOutput(reader);
118
		    input.close ();
152
		    reader.close ();
119
	    } catch (IOException e) {
153
	    } catch (IOException e) {
120
			// TODO Auto-generated catch block
154
			log.error("Error while capturing EBS payment", e);
121
			e.printStackTrace();
-
 
122
		}
155
		}
123
	    return resultMap;
156
	    return resultMap;
124
	}
157
	}
125
 
158
 
-
 
159
	/**
-
 
160
	 * Parses the given input stream and returns a map containing the
-
 
161
	 * transaction details
-
 
162
	 * 
-
 
163
	 * @param reader The reader containing the response of the capture request.
-
 
164
	 * @return A Map. The Map will definitely have status and will have error
-
 
165
	 *         information in case of error and txn information in case of
-
 
166
	 *         success.
-
 
167
	 */
126
	private static Map<String, String> parseCaptureOutput(DataInputStream in){
168
	private static Map<String, String> parseCaptureOutput(BufferedReader reader){
127
		Map<String, String> resultMap = new HashMap<String, String>();
169
		Map<String, String> resultMap = new HashMap<String, String>();
128
		resultMap.put(STATUS, "");
170
		resultMap.put(STATUS, "");
129
		
171
		
130
		String str = null;
172
		String str = null;
131
		try {
173
		try {
132
			str = in.readLine();
174
			str = reader.readLine();
133
			System.out.println(str);
-
 
134
			log.info("Capture response: " + str);
175
			log.info("Capture response: " + str);
135
		} catch (IOException e) {
176
		} catch (IOException e) {
136
			log.error("Error reading the capture response:", e);
177
			log.error("Error reading the capture response:", e);
137
			return resultMap;
178
			return resultMap;
138
		}
179
		}