KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > factories > creditcard > AuthorizeCreditCardProcessor


1 package com.dotmarketing.factories.creditcard;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.DataOutputStream JavaDoc;
5 import java.io.InputStreamReader JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.net.URLConnection JavaDoc;
8 import java.util.Calendar JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.GregorianCalendar JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import javax.servlet.ServletException JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20 import com.dotmarketing.util.Config;
21
22 public class AuthorizeCreditCardProcessor extends CreditCardProcessor{
23     
24     private final String JavaDoc successPageConfig = "successPageConfig";
25     private final String JavaDoc failurePageConfig = "failurePageConfig";
26     private final String JavaDoc testMode = "testMode";
27     private final String JavaDoc login = "login";
28     private final String JavaDoc password = "password";
29     private final String JavaDoc tranKey = "password";
30     private final String JavaDoc delimChart = "delimChart";
31     private final String JavaDoc successCommand = "successCommand";
32     private final String JavaDoc failureCommand = "failureCommand";
33     
34     private String JavaDoc _SuccessPage;
35     private String JavaDoc _FailurePage;
36     private boolean _TestMode;
37     private String JavaDoc _Login;
38     private String JavaDoc _Password;
39     private String JavaDoc _TranKey;
40     private String JavaDoc _DelimChart;
41     private String JavaDoc _SuccessCommand;
42     private String JavaDoc _FailureCommand;
43     private HttpServletRequest JavaDoc request;
44     private HttpServletResponse JavaDoc response;
45     
46 // table to convert a nibble to a hex character
47
static char[] hexChar = {
48         '0' , '1' , '2' , '3' ,
49         '4' , '5' , '6' , '7' ,
50         '8' , '9' , 'A' , 'B' ,
51         'C' , 'D' , 'E' , 'F' };
52     
53     public HttpServletRequest JavaDoc getRequest() {
54         return request;
55     }
56
57     public void setRequest(HttpServletRequest JavaDoc request) {
58         this.request = request;
59     }
60
61     public HttpServletResponse JavaDoc getResponse() {
62         return response;
63     }
64
65     public void setResponse(HttpServletResponse JavaDoc response) {
66         this.response = response;
67     }
68
69     public void configure()
70     {
71         _SuccessPage = Config.getStringProperty(successPageConfig);
72         _FailurePage = Config.getStringProperty(failurePageConfig);
73         _TestMode = Config.getBooleanProperty(testMode);
74         _Login = Config.getStringProperty(login);
75         _Password = Config.getStringProperty(password);
76         _TranKey = Config.getStringProperty(tranKey);
77         _DelimChart = Config.getStringProperty(delimChart);
78         _SuccessCommand = Config.getStringProperty(successCommand);
79         _FailureCommand = Config.getStringProperty(failureCommand);
80     }
81     
82     public void process() {
83         
84         try{
85             //in case the urls end with ? or & let's get rid of that.
86
if(_SuccessPage.endsWith("?") || _SuccessPage.endsWith("&"))
87                 _SuccessPage = _SuccessPage.substring(0,_SuccessPage.length() - 1);
88             
89             if(_FailurePage.endsWith("?") || _FailurePage.endsWith("&"))
90                 _FailurePage = _FailurePage.substring(0,_FailurePage.length() - 1);
91             
92             
93             /* if the _SuccessPage and _FailurePage are null or empty throw an exception we don't even want to deal*/
94             if((_SuccessPage == null || _FailurePage == null) || (_SuccessPage.equals("") || _FailurePage.equals("")) ) {
95                 throw new ServletException JavaDoc("The success_page and failure_page fields have to be passed to me to work");
96             }
97             else {
98                 
99                 // standard variables for basic Java AIM test
100
// use your own values where appropriate
101
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
102                 
103                 // mandatory name/value pairs for all AIM CC transactions
104
// as well as some "good to have" values
105
sb.append("x_login=" + _Login + "&"); // replace with your own
106
sb.append("x_password=" + _Password + "&"); // replace with your own
107
sb.append("x_tran_key=" + _TranKey +"&"); // replace with your own
108
sb.append("x_version=3.1&");
109                 if (_TestMode) {
110                     sb.append("x_test_request=TRUE&"); // for testing
111
}
112                 
113                 sb.append("x_method=CC&");
114                 sb.append("x_type=AUTH_CAPTURE&");
115                 sb.append("x_delim_data=TRUE&");
116                 sb.append("x_delim_char=" + _DelimChart + "&");
117                 sb.append("x_relay_response=FALSE&");
118                 
119                 // not required...but my test account is set up to require it
120
sb.append("x_description=FMINet Transaction&");
121                 
122                 
123                 //### AUTHORIZE.NET ###
124
sb.append("&success_page=" + _SuccessPage);
125                 sb.append("&success_cmd=" + _SuccessCommand);
126                 sb.append("&failure_page=" + _FailureCommand);
127                 sb.append("&failure_cmd=" + _FailureCommand);
128                 
129                 sb.append("&x_merchant_email=eestremera@fminet.com");
130                 sb.append("&x_email_customer=" + "false");
131                 sb.append("&x_first_name=" + getBillingFirstName());
132                 sb.append("&x_last_name=" + getBillingLastName());
133                 sb.append("&x_email=" + getBillingEmailAdress());
134                 sb.append("&x_company=" + getBillingCompany());
135                 sb.append("&x_address=" + getBillingStreet());
136                 sb.append("&x_city=" + getBillingCity());
137                 sb.append("&x_state=" + getBillingState());
138                 sb.append("&x_zip=" + getBillingZip());
139                 sb.append("&x_country=" + getBillingCountry());
140                 sb.append("&x_phone=" + getBillingPhone());
141                 sb.append("&x_ship_to_first_name=" + getShippingFirstName());
142                 sb.append("&x_ship_to_last_name=" + getShippingLastName());
143                 sb.append("&x_ship_to_company=" + getShippingCompany());
144                 sb.append("&x_ship_to_address=" + getShippingStreet());
145                 sb.append("&x_ship_to_city=" + getShippingCity());
146                 sb.append("&x_ship_to_state=" + getShippingState());
147                 sb.append("&x_ship_to_zip=" + getShippingZip());
148                 sb.append("&x_ship_to_country=" + getShippingCountry());
149                 sb.append("&x_ship_to_phone=" + getShippingPhone());
150                 sb.append("&x_card_num=" + getCreditCardNumber());
151                 Date JavaDoc expDate = getCreditCardExpirationDate();
152                 GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
153                 gc.setTime(expDate);
154                 int month = gc.get(Calendar.MONDAY);
155                 month++;
156                 int year = gc.get(Calendar.YEAR);
157                 String JavaDoc expdDateString = Integer.toString(month) + Integer.toString(year);
158                 sb.append("&x_exp_date=" + expdDateString);
159                 sb.append("&x_card_code=" + getCreditCardCVV());
160                                                 
161                 
162                 
163                 // open secure connection
164
URL JavaDoc url = new URL JavaDoc("https://secure.authorize.net/gateway/transact.dll");
165                 if (_TestMode)
166                 {
167                     //only for test mode
168
url = new URL JavaDoc("https://certification.authorize.net/gateway/transact.dll");
169                 }
170                 
171                 URLConnection JavaDoc connection = (URLConnection JavaDoc) url.openConnection();
172                 connection.setDoOutput(true);
173                 connection.setUseCaches(false);
174                 
175                 // not necessarily required but fixes a bug with some servers
176
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
177                 connection.setRequestProperty("Referer","http://www.keystone.edu/credit_card_processor_asdasda124afw4asd");
178                 // POST the data in the string buffer
179
DataOutputStream JavaDoc out = new DataOutputStream JavaDoc( connection.getOutputStream() );
180                 out.write(sb.toString().getBytes());
181                 out.flush();
182                 out.close();
183                 
184                 System.out.println("\n\n\nSENDING TO=https://secure.authorize.net/gateway/transact.dll");
185                 System.out.println("SENDING=" + sb.toString());
186                 
187                 // process and read the gateway response
188
BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(connection.getInputStream()));
189                 String JavaDoc line;
190                 line = in.readLine();
191                 in.close(); // no more data
192
System.err.println(line);
193                 
194                 // ONLY FOR THOSE WHO WANT TO CAPTURE GATEWAY RESPONSE INFORMATION
195
// make the reply readable (be sure to use the x_delim_char for the split operation)
196
Vector JavaDoc ccrep = split(_DelimChart, line);
197                 
198                 System.out.print("Response Code: ");
199                 System.out.println(ccrep.elementAt(0));
200                 System.out.print("Human Readable Response Code: ");
201                 System.out.println(ccrep.elementAt(3));
202                 System.out.print("Approval Code: ");
203                 System.out.println(ccrep.elementAt(4));
204                 System.out.print("Trans ID: ");
205                 System.out.println(ccrep.elementAt(6));
206                 System.out.print("MD5 Hash Server: ");
207                 System.out.println(ccrep.elementAt(37));
208                 
209                 String JavaDoc message = ccrep.elementAt(3).toString();
210                 
211                 StringBuffer JavaDoc _RedirectTo = new StringBuffer JavaDoc();
212                 String JavaDoc command = "";
213                 
214                 System.out.println("RESPONSE CODE=" + ccrep.elementAt(0));
215                 if(!ccrep.elementAt(0).equals("1"))
216                 { //something is wrong
217
//we are here because the transactiopn did not go through
218
System.err.println("RESPONSE CODE: " + ccrep.elementAt(0) + "!!!!!!!!!!");
219                     _RedirectTo.append(_FailurePage);
220                     command = _SuccessCommand;
221                 }
222                 else
223                 {
224                     System.out.println("Redirect to success page: " + _SuccessPage + "!!!!!!!!!!");
225                     _RedirectTo.append(_SuccessPage);
226                     command = _FailureCommand;
227                 }
228                 
229                 //### add the parameters ###
230
_RedirectTo.append("&message=" + message);
231                 _RedirectTo.append("&cmd=" + command);
232                 //### end add the parameters ###
233

234                 String JavaDoc redirectToURL = _RedirectTo.toString();
235                 //response.sendRedirect(redirectToURL);
236
}
237         }
238         catch(Exception JavaDoc e){
239             e.printStackTrace();
240         }
241     }
242     
243     private static String JavaDoc recreateQueryString(Hashtable JavaDoc _ReqHash) {
244         StringBuffer JavaDoc qString = new StringBuffer JavaDoc();
245         /* never include sensitive data in this one */
246         cleanSensitiveData(_ReqHash);
247         Set JavaDoc keySet = _ReqHash.keySet();
248         Iterator JavaDoc it = keySet.iterator();
249         int cnt = 0;
250         while(it.hasNext()) {
251             String JavaDoc key = (String JavaDoc)it.next();
252             String JavaDoc val = ((String JavaDoc[])_ReqHash.get(key))[0];
253             if (!key.equals("x_exp_date") && !key.equals("x_card_num") && !key.equals("cmd")) {
254                 qString.append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + val + "\">\n");
255             }
256         }
257         return qString.toString();
258     }
259     
260     private static void cleanSensitiveData(Hashtable JavaDoc _ReqHash) {
261         Object JavaDoc obj = new Object JavaDoc();
262         obj = _ReqHash.remove("x_exp_date");
263         obj = _ReqHash.remove("x_card_num");
264         obj = _ReqHash.remove("success_page");
265         obj = _ReqHash.remove("failure_page");
266         obj = _ReqHash.remove("x_Description");
267         obj = _ReqHash.remove("submit");
268     }
269     
270     
271     // utility functions
272
public static Vector JavaDoc split(String JavaDoc pattern, String JavaDoc in){
273         int s1=0, s2=-1;
274         Vector JavaDoc out = new Vector JavaDoc(30);
275         while(true){
276             s2 = in.indexOf(pattern, s1);
277             if(s2 != -1){
278                 out.addElement(in.substring(s1, s2));
279             }else{
280                 //the end part of the string (string not pattern terminated)
281
String JavaDoc _ = in.substring(s1);
282                 if(_ != null && !_.equals("")){
283                     out.addElement(_);
284                 }
285                 break;
286             }
287             s1 = s2;
288             s1 += pattern.length();
289         }
290         return out;
291     }
292 }
293     
294     
295
296
297
Popular Tags