1 25 package org.ofbiz.accounting.thirdparty.gosoftware; 26 27 import java.io.IOException ; 28 import java.io.PrintStream ; 29 import java.io.DataInputStream ; 30 import java.net.Socket ; 31 32 import javax.xml.parsers.ParserConfigurationException ; 33 34 import org.ofbiz.base.util.UtilXml; 35 import org.ofbiz.base.util.ObjectType; 36 import org.ofbiz.base.util.GeneralException; 37 import org.ofbiz.base.util.Debug; 38 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.xml.sax.SAXException ; 42 43 49 public class PcChargeApi { 50 51 public static final String module = PcChargeApi.class.getName(); 52 public static final String xschema = "x-schema:..\\dtd\\stnd.xdr"; 53 public static final String rootElement = "XML_FILE"; 54 public static final String reqElement = "XML_REQUEST"; 55 56 public static final String USER_ID = "USER_ID"; 57 public static final String USER_PW = "USER_PW"; 58 public static final String COMMAND = "COMMAND"; 59 public static final String TROUTD = "TROUTD"; 60 public static final String PROCESSOR_ID = "PROCESSOR_ID"; 61 public static final String MERCH_NUM = "MERCH_NUM"; 62 public static final String ACCT_NUM = "ACCT_NUM"; 63 public static final String EXP_DATE = "EXP_DATE"; 64 public static final String MANUAL_FLAG = "MANUAL_FLAG"; 65 public static final String TRANS_AMOUNT = "TRANS_AMOUNT"; 66 public static final String REFERENCE = "REFERENCE"; 67 public static final String TRACK_DATA = "TRACK_DATA"; 68 public static final String CUSTOMER_CODE = "CUSTOMER_CODE"; 69 public static final String TAX_AMOUNT = "TAX_AMOUNT"; 70 public static final String PRINT_RECEIPTS_FLAG = "PRINT_RECEIPTS_FLAG"; 71 public static final String PERIODIC_PAYMENT_FLAG = "PERIODIC_PAYMENT_FLAG"; 72 public static final String OFFLINE_FLAG = "OFFLINE_FLAG"; 73 public static final String VOID_FLAG = "VOID_FLAG"; 74 public static final String ZIP_CODE = "ZIP_CODE"; 75 public static final String STREET = "STREET"; 76 public static final String TICKET_NUM = "TICKET_NUM"; 77 public static final String CARDHOLDER = "CARDHOLDER"; 78 public static final String TRANS_STORE = "TRANS_STORE"; 79 public static final String TRANS_ID = "TRANS_ID"; 80 public static final String TOTAL_AUTH = "TOTAL_AUTH"; 81 public static final String MULTI_FLAG = "MULTI_FLAG"; 82 public static final String PRESENT_FLAG = "PRESENT_FLAG"; 83 public static final String CVV2 = "CVV2"; 84 85 public static final String AUTH_CODE = "AUTH_CODE"; 86 public static final String RESULT = "RESULT"; 87 public static final String AVS_CODE = "AVS_CODE"; 88 public static final String TRANS_DATE = "TRANS_DATE"; 89 public static final String TICKET = "TICKET"; 90 public static final String CARD_ID_CODE = "CARD_ID_CODE"; 91 public static final String CVV2_CODE = "CVV2_CODE"; 92 93 protected static final String [] validOut = { RESULT, TRANS_DATE, AVS_CODE, CVV2_CODE, CARD_ID_CODE, TICKET }; 94 protected static final String [] validIn = { PROCESSOR_ID, MERCH_NUM, ACCT_NUM, EXP_DATE, TRANS_AMOUNT, TRACK_DATA, 95 CUSTOMER_CODE, TAX_AMOUNT, PRINT_RECEIPTS_FLAG, PERIODIC_PAYMENT_FLAG, OFFLINE_FLAG, VOID_FLAG, ZIP_CODE, 96 STREET, TICKET_NUM, CARDHOLDER, TRANS_STORE, TOTAL_AUTH, MULTI_FLAG, PRESENT_FLAG, CVV2 }; 97 98 protected static final int MODE_OUT = 20; 99 protected static final int MODE_IN = 10; 100 101 protected Document document = null; 102 protected Element req = null; 103 protected String host = null; 104 protected int port = 0; 105 protected int mode = 0; 106 107 public PcChargeApi(Document document) { 108 this.document = document; 109 Element rootElement = this.document.getDocumentElement(); 110 if (reqElement.equals(rootElement.getNodeName())) { 111 this.req = rootElement; 112 } else { 113 this.req = UtilXml.firstChildElement(rootElement, reqElement); 114 } 115 this.mode = MODE_OUT; 116 } 117 118 public PcChargeApi(boolean isFile) { 119 String initialElement = rootElement; 121 if (!isFile) { 122 initialElement = reqElement; 123 } 124 125 this.document = UtilXml.makeEmptyXmlDocument(initialElement); 126 Element root = this.document.getDocumentElement(); 127 if (isFile) { 128 root.setAttribute("xmlns", xschema); 129 this.req = UtilXml.addChildElement(root, reqElement, document); 130 } else { 131 this.req = root; 132 } 133 this.mode = MODE_IN; 134 } 135 136 public PcChargeApi(String host, int port) { 137 this(false); 138 this.host = host; 139 this.port = port; 140 } 141 142 public PcChargeApi() { 143 this(true); 144 } 145 146 public void set(String name, Object value) { 147 if (!checkIn(name)) { 148 throw new IllegalArgumentException ("Field [" + name + "] is not a valid IN parameter"); 149 } 150 151 String objString = null; 152 try { 153 objString = (String ) ObjectType.simpleTypeConvert(value, "java.lang.String", null, null); 154 } catch (GeneralException e) { 155 Debug.logError(e, module); 156 throw new IllegalArgumentException ("Unable to convert value to String"); 157 } catch (ClassCastException e) { 158 Debug.logError(e, module); 159 throw new IllegalArgumentException ("Unable to convert value to String"); 160 } 161 if (objString == null && value != null) { 162 throw new IllegalArgumentException ("Unable to convert value to String"); 163 } else if (objString == null) { 164 objString = ""; 165 } 166 167 UtilXml.addChildElementValue(req, name, objString, document); 169 } 170 171 public String get(String name) { 172 if (!checkOut(name)) { 173 throw new IllegalArgumentException ("Field [" + name + "] is not a valid OUT parameter"); 174 } 175 176 return UtilXml.childElementValue(req, name); 177 } 178 179 public String toString() { 180 try { 181 return UtilXml.writeXmlDocument(document); 182 } catch (IOException e) { 183 Debug.logError(e, module); 184 throw new IllegalStateException ("Unable to write document as String"); 185 } 186 } 187 188 public Document getDocument() { 189 return this.document; 190 } 191 192 public PcChargeApi send() throws IOException , GeneralException { 193 if (host == null || port == 0) { 194 throw new GeneralException("TCP transaction not supported without valid host/port configuration"); 195 } 196 197 byte readBuffer[] = new byte[2250]; 198 if (mode == MODE_IN) { 199 Socket sock = new Socket (host, port); 200 PrintStream ps = new PrintStream (sock.getOutputStream()); 201 DataInputStream dis = new DataInputStream (sock.getInputStream()); 202 ps.print(this.toString()); 203 ps.flush(); 204 205 StringBuffer buf = new StringBuffer (); 206 int size; 207 while ((size = dis.read(readBuffer)) > -1) { 208 buf.append(new String (readBuffer, 0, size)); 209 } 210 Document outDoc = null; 211 try { 212 outDoc = UtilXml.readXmlDocument(buf.toString(), false); 213 } catch (ParserConfigurationException e) { 214 throw new GeneralException(e); 215 } catch (SAXException e) { 216 throw new GeneralException(e); 217 } 218 219 PcChargeApi out = new PcChargeApi(outDoc); 220 return out; 221 } else { 222 throw new IllegalStateException ("Cannot send output object"); 223 } 224 } 225 226 private boolean checkIn(String name) { 227 for (int i = 0; i < validOut.length; i++) { 228 if (name.equals(validOut[i])) { 229 return false; 230 } 231 } 232 return true; 233 } 234 235 private boolean checkOut(String name) { 236 for (int i = 0; i < validIn.length; i++) { 237 if (name.equals(validIn[i])) { 238 return false; 239 } 240 } 241 return true; 242 } 243 } 244 | Popular Tags |