1 package com.jcommercesql.gateway.authorizenet; 2 3 23 24 import java.util.Hashtable ; 25 import java.util.Properties ; 26 import java.util.StringTokenizer ; 27 import java.util.Vector ; 28 import java.security.MessageDigest ; 29 import java.io.*; 30 31 38 public abstract class AuthorizeNet { 39 40 Properties properties = null; 41 String propFileName=""; 42 Hashtable responseValues = new Hashtable (); 43 Hashtable additionalHTTPHeaderFields = new Hashtable (); 44 45 protected String host=""; 46 protected int port=443; 47 protected String path="/"; 48 protected char x_Delim_Char=','; 49 50 protected String postData=""; 51 protected String merchantMD5HashValue=""; 52 protected byte[] rawResponse=null; 53 Hashtable optionalFields=new Hashtable (); 54 Vector merchantDefinedFields= new Vector (); 55 56 protected String x_Version="3.1"; 58 protected String x_Delim_Data="True"; 59 protected String x_Login=""; 60 protected String x_Tran_Key=""; 61 protected String x_Amount=""; 62 63 68 76 77 79 protected AuthorizeNet() {} 80 81 87 protected Properties loadPropFile(String propFileName) throws IOException, InvalidPropException { 88 89 Properties properties = new Properties (); 90 properties.load(new FileInputStream(propFileName)); 91 validateProperties(properties); 92 return properties; 93 } 94 95 103 public void reloadProperties() throws IOException, InvalidPropException { 104 reloadProperties(this.propFileName); 105 } 106 107 109 116 public void reloadProperties(String propFileName) throws IOException, InvalidPropException { 117 118 Properties tempProp=loadPropFile(propFileName); 119 this.properties=tempProp; 120 this.propFileName=propFileName; 121 } 122 123 128 protected void validateProperties(Properties p) throws InvalidPropException { 129 if (p == null) { throw new InvalidPropException("validateProperties(): null Properties argument"); } 130 131 if (p.getProperty("host","").equals("")) { 132 throw new InvalidPropException("'host' is invalid or not found in properties file."); 133 } else { 134 setHost(p.getProperty("host")); 135 } 136 137 if (p.getProperty("port","").equals("")) { 138 throw new InvalidPropException("'host' is invalid or not found in properties file."); 139 } 140 141 try { 142 setPort(Integer.parseInt(p.getProperty("port"))); 143 } catch(NumberFormatException nfe) { 144 throw new InvalidPropException("port '"+ p.getProperty("port")+"' does not appear to be an integer"); 145 } 146 147 if (p.getProperty("path","").equals("")) { 148 throw new InvalidPropException("'path' is invalid or not found in properties file."); 149 } else { 150 setPath(p.getProperty("path")); 151 } 152 153 if (p.getProperty("loginid","").equals("")) { 154 throw new InvalidPropException("'login' is invalid or not found in properties file."); 155 } else { 156 setLogin(p.getProperty("loginid")); 157 } 158 159 if (p.getProperty("trankey","").equals("")) { 160 throw new InvalidPropException("'trankey' is invalid or not found in properties file."); 161 } else { 162 setTranKey(p.getProperty("trankey")); 163 } 164 165 } 166 167 172 protected void setVersion(String version ) { this.x_Version=version; } 173 174 179 protected String getVersion() { return x_Version; } 180 181 188 protected void setDelimData(boolean delimData) { 189 this.x_Delim_Data=(new Boolean (delimData).toString()).toUpperCase(); 190 } 191 192 197 198 protected boolean getDelimData() { return (new Boolean (x_Delim_Data)).booleanValue(); } 199 200 206 public void setDelimChar(char delimChar) { this.x_Delim_Char=delimChar; } 207 208 213 protected char getDelimChar() { return x_Delim_Char; } 214 215 223 public void setMerchantInfo(String loginID, String tranKey ) { 224 setLogin(loginID); 225 setTranKey(tranKey); 226 } 227 228 234 public void setLogin(String loginID ) { this.x_Login=loginID; } 235 236 241 public String getLogin() { return x_Login; } 242 243 249 public void setTranKey(String tranKey) { this.x_Tran_Key=tranKey; } 250 251 256 public String getTranKey() { return x_Tran_Key; } 257 258 263 public void setAmount(String amount) { this.x_Amount=amount; } 264 265 270 public String getAmount() { return x_Amount; } 271 272 278 public void setHost(String host) { this.host=host; } 279 280 285 public String getHost() { return host; } 286 287 295 public void setPort(int port) { this.port=port; } 296 297 302 public int getPort() { return port; } 303 304 310 public void setPath(String path) { this.path=path; } 311 312 317 public String getPath() { return path; } 318 319 326 public void setURL(String host, int port, String path) { 327 setHost(host); 328 setPort(port); 329 setPath(path); 330 } 331 332 339 public String getPostData() { return this.postData; } 340 341 348 public byte[] getResponseBytes() { return this.rawResponse; } 349 350 361 public void setTestMode(boolean value) { 362 addOptionalField(AuthorizeNetCodes.REQ_FIELD_TEST_REQUEST,(new Boolean (value).toString()).toUpperCase()); 363 } 364 365 372 public void removeTestMode() { 373 removeOptionalField(AuthorizeNetCodes.REQ_FIELD_TEST_REQUEST); 374 } 375 376 393 public void addOptionalField(String fieldName, String fieldValue) { 394 optionalFields.put(fieldName,fieldValue); 395 } 396 397 406 public String removeOptionalField(String fieldName) { 407 return (String )optionalFields.remove(fieldName); 408 } 409 410 protected void parseANetResponse(byte[] httpResponse) { 412 413 String in = new String (httpResponse); 414 String httpHeaders; 415 String response; 416 417 int offset=in.indexOf("\r\n\r\n"); 418 419 if (offset==-1) { 420 System.out.println("Header/Body Break Not Found!\n"+in); 421 } else { 422 httpHeaders=new String (httpResponse,0,offset); 423 offset+=4; 425 response=new String (httpResponse,offset,httpResponse.length-offset); 426 parseANetResponse(response); 427 } 428 this.rawResponse=httpResponse; 430 } 431 432 433 private void parseANetResponse(String responseString) { 435 String delimiter = (new Character (getDelimChar())).toString(); 437 boolean currentTokenIsDelimeter = false; 438 boolean lastTokenWasDelimeter = false; 439 int positionCounter=0; 440 StringTokenizer st = new StringTokenizer (responseString, delimiter, true); 441 while (st.hasMoreTokens()) { 442 String fieldValue = st.nextToken(); 443 444 if (fieldValue.equals(delimiter)) 446 currentTokenIsDelimeter = true; 447 else 448 currentTokenIsDelimeter = false; 449 450 if (currentTokenIsDelimeter && lastTokenWasDelimeter) 452 responseValues.put(new Integer (++positionCounter),""); 453 else if (!currentTokenIsDelimeter) 455 responseValues.put(new Integer (++positionCounter),fieldValue); 456 457 lastTokenWasDelimeter = currentTokenIsDelimeter; 459 } 460 } 461 462 468 protected boolean verifyMD5Hash() { 470 String md5hash=getResponseValue(AuthorizeNetCodes.RESP_FIELD_MD5HASH); 471 return verifyMD5Hash(md5hash); 472 } 473 474 protected boolean verifyMD5Hash(String md5HashValue) { 476 477 String hashKey; 479 hashKey=merchantMD5HashValue + x_Login + 480 getResponseValue(AuthorizeNetCodes.RESP_FIELD_TRANSACTIONID) + 481 getResponseValue(AuthorizeNetCodes.RESP_FIELD_AMOUNT); 482 String localMD5Result=calcMD5Hash(hashKey); 483 if (localMD5Result.equals(md5HashValue)) { 484 return true; 485 } else { 486 return false; 487 } 488 } 489 490 protected String calcMD5Hash(String input) { 492 try { 493 MessageDigest md = MessageDigest.getInstance("MD5"); 494 return new String ( md.digest(input.getBytes()) ); 495 } catch(Exception e) { System.out.println("Can't load MD5 algorithm"); } 496 return ""; 497 } 498 499 520 public String getResponseValue(int responsePosition) { 521 return (String )responseValues.get(new Integer (responsePosition)); 522 } 523 524 531 public String getResponseCode() { return getResponseValue(1); } 532 533 537 public String getResponseSubCode() { return getResponseValue(2); } 538 539 543 public String getResponseReasonCode() { return getResponseValue(3); } 544 545 549 public String getResponseReasonText() { return getResponseValue(4); } 550 551 555 public String getResponseApprovalCode() { return getResponseValue(5); } 556 557 561 public String getResponseAVSResultCode() { return getResponseValue(6); } 562 563 567 public String getResponseTransactionID() { return getResponseValue(7); } 568 569 591 public void addMerchantDefinedField(String fieldName, String fieldValue) { 592 merchantDefinedFields.add(fieldName); 593 merchantDefinedFields.add(fieldValue); 594 } 595 596 protected boolean verifyMinimumFieldsSet() { 598 599 boolean result=true; 600 if (x_Version==null) result = false; 601 if (x_Delim_Data==null || x_Delim_Data.equals("") ) result = false; 602 if (x_Login==null || x_Login.equals("") ) result = false; 603 if (x_Tran_Key==null || x_Tran_Key.equals("") ) result = false; 604 if (x_Amount==null || x_Amount.equals("") ) result = false; 605 606 return result; 607 } 608 609 protected void submitANetGet() { 611 612 } 613 614 protected void submitANetPost(String host, int port, String path, String postdata) throws Exception { 616 submitANetPost(host,port,path,postdata.getBytes()); 617 } 618 protected void submitANetPost(String host, int port, String path, byte[] postdata) throws Exception { 620 621 Gateway gw=new Gateway(); 622 byte[] rawResponse = gw.submitPost(host,port,path,postdata); 623 parseANetResponse(rawResponse); 624 } 625 626 public abstract void submit() throws Exception ; 628 629 } 630 | Popular Tags |