1 package com.dotmarketing.factories.creditcard; 2 3 import java.io.BufferedReader ; 4 import java.io.DataOutputStream ; 5 import java.io.InputStreamReader ; 6 import java.net.URL ; 7 import java.net.URLConnection ; 8 import java.util.Calendar ; 9 import java.util.Date ; 10 import java.util.GregorianCalendar ; 11 import java.util.Hashtable ; 12 import java.util.Iterator ; 13 import java.util.Set ; 14 import java.util.Vector ; 15 16 import javax.servlet.ServletException ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 20 import com.dotmarketing.util.Config; 21 22 public class AuthorizeCreditCardProcessor extends CreditCardProcessor{ 23 24 private final String successPageConfig = "successPageConfig"; 25 private final String failurePageConfig = "failurePageConfig"; 26 private final String testMode = "testMode"; 27 private final String login = "login"; 28 private final String password = "password"; 29 private final String tranKey = "password"; 30 private final String delimChart = "delimChart"; 31 private final String successCommand = "successCommand"; 32 private final String failureCommand = "failureCommand"; 33 34 private String _SuccessPage; 35 private String _FailurePage; 36 private boolean _TestMode; 37 private String _Login; 38 private String _Password; 39 private String _TranKey; 40 private String _DelimChart; 41 private String _SuccessCommand; 42 private String _FailureCommand; 43 private HttpServletRequest request; 44 private HttpServletResponse response; 45 46 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 getRequest() { 54 return request; 55 } 56 57 public void setRequest(HttpServletRequest request) { 58 this.request = request; 59 } 60 61 public HttpServletResponse getResponse() { 62 return response; 63 } 64 65 public void setResponse(HttpServletResponse 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 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 94 if((_SuccessPage == null || _FailurePage == null) || (_SuccessPage.equals("") || _FailurePage.equals("")) ) { 95 throw new ServletException ("The success_page and failure_page fields have to be passed to me to work"); 96 } 97 else { 98 99 StringBuffer sb = new StringBuffer (); 102 103 sb.append("x_login=" + _Login + "&"); sb.append("x_password=" + _Password + "&"); sb.append("x_tran_key=" + _TranKey +"&"); sb.append("x_version=3.1&"); 109 if (_TestMode) { 110 sb.append("x_test_request=TRUE&"); } 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 sb.append("x_description=FMINet Transaction&"); 121 122 123 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 expDate = getCreditCardExpirationDate(); 152 GregorianCalendar gc = new GregorianCalendar (); 153 gc.setTime(expDate); 154 int month = gc.get(Calendar.MONDAY); 155 month++; 156 int year = gc.get(Calendar.YEAR); 157 String 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 URL url = new URL ("https://secure.authorize.net/gateway/transact.dll"); 165 if (_TestMode) 166 { 167 url = new URL ("https://certification.authorize.net/gateway/transact.dll"); 169 } 170 171 URLConnection connection = (URLConnection ) url.openConnection(); 172 connection.setDoOutput(true); 173 connection.setUseCaches(false); 174 175 connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 177 connection.setRequestProperty("Referer","http://www.keystone.edu/credit_card_processor_asdasda124afw4asd"); 178 DataOutputStream out = new DataOutputStream ( 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 BufferedReader in = new BufferedReader (new InputStreamReader (connection.getInputStream())); 189 String line; 190 line = in.readLine(); 191 in.close(); System.err.println(line); 193 194 Vector 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 message = ccrep.elementAt(3).toString(); 210 211 StringBuffer _RedirectTo = new StringBuffer (); 212 String command = ""; 213 214 System.out.println("RESPONSE CODE=" + ccrep.elementAt(0)); 215 if(!ccrep.elementAt(0).equals("1")) 216 { 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 _RedirectTo.append("&message=" + message); 231 _RedirectTo.append("&cmd=" + command); 232 234 String redirectToURL = _RedirectTo.toString(); 235 } 237 } 238 catch(Exception e){ 239 e.printStackTrace(); 240 } 241 } 242 243 private static String recreateQueryString(Hashtable _ReqHash) { 244 StringBuffer qString = new StringBuffer (); 245 246 cleanSensitiveData(_ReqHash); 247 Set keySet = _ReqHash.keySet(); 248 Iterator it = keySet.iterator(); 249 int cnt = 0; 250 while(it.hasNext()) { 251 String key = (String )it.next(); 252 String val = ((String [])_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 _ReqHash) { 261 Object obj = new Object (); 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 public static Vector split(String pattern, String in){ 273 int s1=0, s2=-1; 274 Vector out = new Vector (30); 275 while(true){ 276 s2 = in.indexOf(pattern, s1); 277 if(s2 != -1){ 278 out.addElement(in.substring(s1, s2)); 279 }else{ 280 String _ = 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 |