KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > device > impl > Msr


1 /*
2  * $Id: Msr.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.pos.device.impl;
26
27 import jpos.JposException;
28
29 import org.ofbiz.base.util.Debug;
30 import org.ofbiz.base.util.UtilValidate;
31 import org.ofbiz.pos.adaptor.DataEventAdaptor;
32 import org.ofbiz.pos.adaptor.ErrorEventAdaptor;
33 import org.ofbiz.pos.device.GenericDevice;
34 import org.ofbiz.pos.screen.PosScreen;
35
36 /**
37  *
38  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
39  * @version $Rev: 5462 $
40  * @since 3.2
41  */

42 public class Msr extends GenericDevice {
43
44     public static final String JavaDoc module = Msr.class.getName();
45     public static final int MSR_CREDIT_CARD = 100;
46     public static final int MSR_GIFT_CARD = 101;
47     public static final int MSR_ATM_CARD = 102;
48     public static final int MSR_CUST_CARD = 701;
49     public static final int MSR_CLERK_CARD = 801;
50     public static final int MSR_UNKNOWN = 999;
51
52     public Msr(String JavaDoc deviceName, int timeout) {
53         super(deviceName, timeout);
54         this.control = new jpos.MSR();
55     }
56
57     protected void initialize() throws JposException {
58         Debug.logInfo("MSR [" + control.getPhysicalDeviceName() + "] Claimed : " + control.getClaimed(), module);
59         final jpos.MSR msr = (jpos.MSR) control;
60         msr.setDecodeData(true);
61         msr.setTracksToRead(2);
62
63         // create the data listner
64
msr.addDataListener(new DataEventAdaptor() {
65
66             public void dataOccurred(jpos.events.DataEvent event) {
67                 String JavaDoc[] decodedData = new String JavaDoc[7];
68                 byte[] track1 = null;
69                 byte[] track2 = null;
70
71                 try {
72                     // get the raw track data
73
track1 = msr.getTrack1Data();
74                     track2 = msr.getTrack2Data();
75
76                     // get the decoded data
77
decodedData[0] = msr.getTitle();
78                     decodedData[1] = msr.getFirstName();
79                     decodedData[2] = msr.getMiddleInitial();
80                     decodedData[3] = msr.getSurname();
81                     decodedData[4] = msr.getSuffix();
82                     decodedData[5] = msr.getAccountNumber();
83
84                     // verify the acct num exists
85
if (UtilValidate.isEmpty(decodedData[5])) {
86                         PosScreen.currentScreen.showDialog("dialog/error/cardreaderror");
87                         msr.clearInput();
88                         return;
89                     }
90
91                     // fix expDate (reversed)
92
if (msr.getExpirationDate() != null && msr.getExpirationDate().length() > 3) {
93                         decodedData[6] = msr.getExpirationDate().substring(2) + msr.getExpirationDate().substring(0, 2);
94                     } else {
95                         PosScreen.currentScreen.showDialog("dialog/error/cardreaderror");
96                         msr.clearInput();
97                         return;
98                     }
99
100                     msr.clearInput();
101                 } catch (jpos.JposException e) {
102                     Debug.logError(e, module);
103                 }
104
105                 processMsrData(decodedData, track1, track2);
106             }
107         });
108
109         // create the error listener
110
msr.addErrorListener(new ErrorEventAdaptor() {
111
112             public void errorOccurred(jpos.events.ErrorEvent event) {
113                 Debug.log("Error Occurred : " + event.getErrorCodeExtended(), module);
114                 PosScreen.currentScreen.showDialog("dialog/error/cardreaderror");
115                 try {
116                     msr.clearInput();
117                 } catch (jpos.JposException e) {
118                     Debug.logError(e, module);
119                 }
120             }
121         });
122     }
123
124     protected void processMsrData(String JavaDoc[] decodedData, byte[] track1, byte[] track2) {
125         StringBuffer JavaDoc msrStr = new StringBuffer JavaDoc();
126         msrStr.append(decodedData[5]);
127         msrStr.append("|");
128         msrStr.append(decodedData[6]);
129         msrStr.append("|");
130         msrStr.append(decodedData[1]);
131         msrStr.append("|");
132         msrStr.append(decodedData[3]);
133         Debug.log("Msr Info : " + msrStr.toString(), module);
134
135         // implemented validation
136
int msrType = MSR_UNKNOWN;
137         try {
138             if (UtilValidate.isAnyCard(decodedData[5])) {
139                 msrType = MSR_CREDIT_CARD;
140             } else if (UtilValidate.isGiftCard(decodedData[5])) {
141                 msrType = MSR_GIFT_CARD;
142             }
143         } catch (NumberFormatException JavaDoc e) {
144         }
145
146         // all implemented types
147
switch (msrType) {
148             case MSR_CREDIT_CARD:
149                 // make sure we are on the POS pay screen
150
this.setPayPanel();
151                 PosScreen.currentScreen.getButtons().setLock(true);
152
153                 String JavaDoc[] credInfo = PosScreen.currentScreen.getInput().getFunction("CREDIT");
154                 if (credInfo == null) {
155                     PosScreen.currentScreen.getInput().setFunction("CREDIT", "");
156                 }
157                 PosScreen.currentScreen.getInput().setFunction("MSRINFO", msrStr.toString());
158                 PosScreen.currentScreen.getOutput().print("Credit Card Read");
159                 PosScreen.currentScreen.getInput().clearInput();
160                 this.callEnter();
161                 break;
162             case MSR_GIFT_CARD:
163                 // make sure we are on the POS pay screen
164
this.setPayPanel();
165                 PosScreen.currentScreen.getButtons().setLock(true);
166
167                 PosScreen.currentScreen.getInput().setFunction("MSRINFO", msrStr.toString());
168                 PosScreen.currentScreen.getOutput().print("Gift Card Read");
169                 PosScreen.currentScreen.getInput().clearInput();
170                 this.callEnter();
171                 break;
172             case MSR_UNKNOWN:
173                 PosScreen.currentScreen.showDialog("dialog/error/unknowncardtype");
174                 break;
175         }
176     }
177
178     private void setPayPanel() {
179         if (!"main/paypanel".equals(PosScreen.currentScreen.getName())) {
180             PosScreen pos = PosScreen.currentScreen.showPage("paypanel", false);
181             pos.getInput().setFunction("TOTAL", "");
182             pos.refresh();
183             Debug.log("Switched to paypanel.xml; triggered TOTAL function", module);
184         }
185     }
186 }
187
Popular Tags