KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > thirdparty > gosoftware > PcChargeApi


1 /*
2  * $Id: PcChargeApi.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.accounting.thirdparty.gosoftware;
26
27 import java.io.IOException JavaDoc;
28 import java.io.PrintStream JavaDoc;
29 import java.io.DataInputStream JavaDoc;
30 import java.net.Socket JavaDoc;
31
32 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42
43 /**
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 3.2
48  */

49 public class PcChargeApi {
50
51     public static final String JavaDoc module = PcChargeApi.class.getName();
52     public static final String JavaDoc xschema = "x-schema:..\\dtd\\stnd.xdr";
53     public static final String JavaDoc rootElement = "XML_FILE";
54     public static final String JavaDoc reqElement = "XML_REQUEST";
55
56     public static final String JavaDoc USER_ID = "USER_ID";
57     public static final String JavaDoc USER_PW = "USER_PW";
58     public static final String JavaDoc COMMAND = "COMMAND";
59     public static final String JavaDoc TROUTD = "TROUTD";
60     public static final String JavaDoc PROCESSOR_ID = "PROCESSOR_ID";
61     public static final String JavaDoc MERCH_NUM = "MERCH_NUM";
62     public static final String JavaDoc ACCT_NUM = "ACCT_NUM";
63     public static final String JavaDoc EXP_DATE = "EXP_DATE";
64     public static final String JavaDoc MANUAL_FLAG = "MANUAL_FLAG";
65     public static final String JavaDoc TRANS_AMOUNT = "TRANS_AMOUNT";
66     public static final String JavaDoc REFERENCE = "REFERENCE";
67     public static final String JavaDoc TRACK_DATA = "TRACK_DATA";
68     public static final String JavaDoc CUSTOMER_CODE = "CUSTOMER_CODE";
69     public static final String JavaDoc TAX_AMOUNT = "TAX_AMOUNT";
70     public static final String JavaDoc PRINT_RECEIPTS_FLAG = "PRINT_RECEIPTS_FLAG";
71     public static final String JavaDoc PERIODIC_PAYMENT_FLAG = "PERIODIC_PAYMENT_FLAG";
72     public static final String JavaDoc OFFLINE_FLAG = "OFFLINE_FLAG";
73     public static final String JavaDoc VOID_FLAG = "VOID_FLAG";
74     public static final String JavaDoc ZIP_CODE = "ZIP_CODE";
75     public static final String JavaDoc STREET = "STREET";
76     public static final String JavaDoc TICKET_NUM = "TICKET_NUM";
77     public static final String JavaDoc CARDHOLDER = "CARDHOLDER";
78     public static final String JavaDoc TRANS_STORE = "TRANS_STORE";
79     public static final String JavaDoc TRANS_ID = "TRANS_ID";
80     public static final String JavaDoc TOTAL_AUTH = "TOTAL_AUTH";
81     public static final String JavaDoc MULTI_FLAG = "MULTI_FLAG";
82     public static final String JavaDoc PRESENT_FLAG = "PRESENT_FLAG";
83     public static final String JavaDoc CVV2 = "CVV2";
84
85     public static final String JavaDoc AUTH_CODE = "AUTH_CODE";
86     public static final String JavaDoc RESULT = "RESULT";
87     public static final String JavaDoc AVS_CODE = "AVS_CODE";
88     public static final String JavaDoc TRANS_DATE = "TRANS_DATE";
89     public static final String JavaDoc TICKET = "TICKET";
90     public static final String JavaDoc CARD_ID_CODE = "CARD_ID_CODE";
91     public static final String JavaDoc CVV2_CODE = "CVV2_CODE";
92
93     protected static final String JavaDoc[] validOut = { RESULT, TRANS_DATE, AVS_CODE, CVV2_CODE, CARD_ID_CODE, TICKET };
94     protected static final String JavaDoc[] 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 JavaDoc document = null;
102     protected Element JavaDoc req = null;
103     protected String JavaDoc host = null;
104     protected int port = 0;
105     protected int mode = 0;
106
107     public PcChargeApi(Document JavaDoc document) {
108         this.document = document;
109         Element JavaDoc 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         // initialize the document
120
String JavaDoc initialElement = rootElement;
121         if (!isFile) {
122             initialElement = reqElement;
123         }
124
125         this.document = UtilXml.makeEmptyXmlDocument(initialElement);
126         Element JavaDoc 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 JavaDoc 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 JavaDoc name, Object JavaDoc value) {
147         if (!checkIn(name)) {
148             throw new IllegalArgumentException JavaDoc("Field [" + name + "] is not a valid IN parameter");
149         }
150
151         String JavaDoc objString = null;
152         try {
153             objString = (String JavaDoc) ObjectType.simpleTypeConvert(value, "java.lang.String", null, null);
154         } catch (GeneralException e) {
155             Debug.logError(e, module);
156             throw new IllegalArgumentException JavaDoc("Unable to convert value to String");
157         } catch (ClassCastException JavaDoc e) {
158             Debug.logError(e, module);
159             throw new IllegalArgumentException JavaDoc("Unable to convert value to String");
160         }
161         if (objString == null && value != null) {
162             throw new IllegalArgumentException JavaDoc("Unable to convert value to String");
163         } else if (objString == null) {
164             objString = "";
165         }
166
167         // append to the XML document
168
UtilXml.addChildElementValue(req, name, objString, document);
169     }
170
171     public String JavaDoc get(String JavaDoc name) {
172         if (!checkOut(name)) {
173             throw new IllegalArgumentException JavaDoc("Field [" + name + "] is not a valid OUT parameter");
174         }
175
176         return UtilXml.childElementValue(req, name);
177     }
178
179     public String JavaDoc toString() {
180         try {
181             return UtilXml.writeXmlDocument(document);
182         } catch (IOException JavaDoc e) {
183             Debug.logError(e, module);
184             throw new IllegalStateException JavaDoc("Unable to write document as String");
185         }
186     }
187
188     public Document JavaDoc getDocument() {
189         return this.document;
190     }
191
192     public PcChargeApi send() throws IOException JavaDoc, 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 JavaDoc sock = new Socket JavaDoc(host, port);
200             PrintStream JavaDoc ps = new PrintStream JavaDoc(sock.getOutputStream());
201             DataInputStream JavaDoc dis = new DataInputStream JavaDoc(sock.getInputStream());
202             ps.print(this.toString());
203             ps.flush();
204
205             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
206             int size;
207             while ((size = dis.read(readBuffer)) > -1) {
208                 buf.append(new String JavaDoc(readBuffer, 0, size));
209             }
210             Document JavaDoc outDoc = null;
211             try {
212                 outDoc = UtilXml.readXmlDocument(buf.toString(), false);
213             } catch (ParserConfigurationException JavaDoc e) {
214                 throw new GeneralException(e);
215             } catch (SAXException JavaDoc e) {
216                 throw new GeneralException(e);
217             }
218
219             PcChargeApi out = new PcChargeApi(outDoc);
220             return out;
221         } else {
222             throw new IllegalStateException JavaDoc("Cannot send output object");
223         }
224     }
225
226     private boolean checkIn(String JavaDoc 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 JavaDoc 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