1 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 42 public class Msr extends GenericDevice { 43 44 public static final String 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 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 msr.addDataListener(new DataEventAdaptor() { 65 66 public void dataOccurred(jpos.events.DataEvent event) { 67 String [] decodedData = new String [7]; 68 byte[] track1 = null; 69 byte[] track2 = null; 70 71 try { 72 track1 = msr.getTrack1Data(); 74 track2 = msr.getTrack2Data(); 75 76 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 if (UtilValidate.isEmpty(decodedData[5])) { 86 PosScreen.currentScreen.showDialog("dialog/error/cardreaderror"); 87 msr.clearInput(); 88 return; 89 } 90 91 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 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 [] decodedData, byte[] track1, byte[] track2) { 125 StringBuffer msrStr = new StringBuffer (); 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 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 e) { 144 } 145 146 switch (msrType) { 148 case MSR_CREDIT_CARD: 149 this.setPayPanel(); 151 PosScreen.currentScreen.getButtons().setLock(true); 152 153 String [] 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 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 |