PageRenderTime 102ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/webccr/src/com/emis/caesar/senao/util/UU2Util.java

https://bitbucket.org/sanliou/synccr
Java | 1519 lines | 1213 code | 66 blank | 240 comment | 207 complexity | ecf8f1c8e6c93c970a8812b3405861b1 MD5 | raw file
  1. package com.emis.caesar.senao.util;
  2. import com.emis.caesar.emisString;
  3. import com.emis.caesar.senao.UU2.ICERAPIReq;
  4. import com.emis.caesar.senao.UU2.ICERAPIResp;
  5. import com.emis.caesar.senao.UU2.ICERAPIRespItem;
  6. import com.emis.caesar.senao.event.TButtonEventContext;
  7. import com.emis.caesar.senao.event.logic.TLoEndSale;
  8. import com.emis.caesar.senao.unit.TCcr;
  9. import com.emis.caesar.senao.unit.TCcrConst;
  10. import com.emis.caesar.senao.unit.sale.TPayment;
  11. import com.emis.caesarui.webccr.object.UU2_ERRMSG;
  12. import com.emis.caesarui.webccr.object.UU2_PARAMS;
  13. import com.emis.caesarui.webccr.ui.window.WinUuCard;
  14. import com.emis.caesarui.webccr.ui.window.WinUuCardDongle;
  15. import com.emis.caesarui.webccr.webccr;
  16. import com.emis.caesarui.webccr.webccrlog;
  17. import com.emis.util.emisUtil;
  18. import com.emis.util.emisXmlUtil;
  19. import com.vaadin.ui.JavaScript;
  20. import com.vaadin.ui.JavaScriptFunction;
  21. import com.vaadin.ui.UI;
  22. import de.steinwedel.messagebox.ButtonOption;
  23. import de.steinwedel.messagebox.MessageBox;
  24. import elemental.json.JsonArray;
  25. import org.apache.commons.lang.StringEscapeUtils;
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.Element;
  28. import org.w3c.dom.Node;
  29. import org.w3c.dom.NodeList;
  30. import java.io.BufferedReader;
  31. import java.io.ByteArrayInputStream;
  32. import java.lang.reflect.Method;
  33. import java.util.ArrayList;
  34. import java.util.Hashtable;
  35. import java.util.List;
  36. /**
  37. * 悠遊卡二代 API
  38. * 悠遊卡付款:
  39.  加值:
  40.  餘額查詢:
  41.  dongle交易明細查詢
  42. 加值 扣款 取消 退貨(加值 扣款) 重傳 需紀錄在EASYCARD
  43. */
  44. public class UU2Util {
  45. private static final String CHARSET = "UTF-8";
  46. public final String sNewLine = "\\r\\n";
  47. //
  48. public static final String T0100_SIGNON = "0800";
  49. public static final String T0100_EACH = "0200";
  50. public static final String T0300_SIGNON = "881999"; // 登入
  51. public static final String T0300_PAY = "606100"; // 扣款
  52. public static final String T0300_PAY_VOID = "620061"; // 扣款退貨 or 取消
  53. public static final String T0300_ADD_CANCEL = "816100"; // 現金加值 作廢交易
  54. public static final String T0300_ADD_VALUE = "801061"; // 現金加值
  55. public static final String T0300_QUERY = "296000"; // 卡片資料查詢(餘額)
  56. public static final String T0300_DONGLE = "214100"; // DONGLE
  57. //
  58. private static boolean bDevelopMode = false;
  59. //
  60. TButtonEventContext parentEvent;
  61. WinUuCardDongle parentWin;
  62. webccr webccr;
  63. webccrlog webccrlog;
  64. TCcr ccr;
  65. int iUuCardAmt;// 悠遊卡 付款或加值金額
  66. TPayment oPayment = null;// 當前付款別物件
  67. boolean chkOK = false;
  68. ArrayList<String> sNOlist = new ArrayList<String>(); //起迄序號
  69. String sNO; // 調閱序號
  70. int iRetry = 0; // retry次數
  71. int TYPE = 0; // 0:登入 1 扣款 2 加值 3 取消 4 查詢 5 dongle 6 退貨(扣款作廢) 7 加值作廢
  72. WinUuCard oWindow = null;
  73. int time = 0; // 供銷退retry用
  74. /**
  75. * 作為悠遊卡成功、失敗callBack
  76. */
  77. Runnable successRunnable = null;
  78. /**
  79. * 存放付款別的資料
  80. */
  81. List<TPayment> salePList = new ArrayList<TPayment>();
  82. // 悠遊卡 1 付款 or 2 加值
  83. public UU2Util(int iPayAmt, int iTYPE, TButtonEventContext eventContext) {
  84. this.iUuCardAmt = iPayAmt;
  85. this.TYPE = iTYPE;
  86. this.parentEvent = eventContext;
  87. webccr = (webccr) UI.getCurrent();
  88. ccr = webccr.getFoCCR();
  89. webccrlog = webccr.getlog();
  90. }
  91. // 3 取消付款
  92. public UU2Util(TPayment payment) {
  93. this.oPayment = payment;
  94. webccr = (webccr) UI.getCurrent();
  95. ccr = webccr.getFoCCR();
  96. webccrlog = webccr.getlog();
  97. TYPE = 3;
  98. }
  99. // 悠遊卡 4 查詢 or 0 登入
  100. public UU2Util() {
  101. webccr = (webccr) UI.getCurrent();
  102. ccr = webccr.getFoCCR();
  103. webccrlog = webccr.getlog();
  104. }
  105. // 悠遊卡 5 dongle 重傳
  106. public UU2Util(ArrayList<String> list, WinUuCardDongle window) {
  107. this.sNOlist = list;
  108. this.parentWin = window;
  109. webccr = (webccr) UI.getCurrent();
  110. ccr = webccr.getFoCCR();
  111. webccrlog = webccr.getlog();
  112. TYPE = 5;
  113. }
  114. // 6 扣款作廢
  115. public UU2Util(List<TPayment> paymentlist) {
  116. this.salePList = paymentlist;
  117. webccr = (webccr) UI.getCurrent();
  118. ccr = webccr.getFoCCR();
  119. webccrlog = webccr.getlog();
  120. TYPE = 6;
  121. }
  122. // 7 加值作廢
  123. public UU2Util(int iPayAmt) {
  124. this.iUuCardAmt = iPayAmt;
  125. webccr = (webccr) UI.getCurrent();
  126. ccr = webccr.getFoCCR();
  127. webccrlog = webccr.getlog();
  128. TYPE = 7;
  129. }
  130. // 0 登入
  131. public void signOnUUcard() {
  132. // webccrlog.info(webccr.getClass(),"門市機號:" + ccr.getCcrID() + " --->登入悠遊卡機");
  133. CcrUtil.setMessage("門市機號:" + ccr.getCcrID() + " --->登入悠遊卡機");
  134. TYPE = 0;
  135. ICERAPIReq _Req = new ICERAPIReq();
  136. try {
  137. // 產生物件值
  138. WriteSignOnRequest(_Req);
  139. //
  140. CallIcerApi(_Req);
  141. } catch (Exception e) {
  142. // webccrlog.error(webccr.getClass(), e);
  143. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  144. }
  145. }
  146. // 1 扣款
  147. public void DeductUUcard() {
  148. // webccrlog.info(webccr.getClass(),"-----悠遊卡扣款-----");
  149. // webccrlog.info(webccr.getClass(),"扣款金額 : " + iUuCardAmt);
  150. CcrUtil.setMessage("-----悠遊卡扣款-----");
  151. CcrUtil.setMessage("扣款金額 : " + iUuCardAmt);
  152. ICERAPIReq _Req = new ICERAPIReq();
  153. try {
  154. // 產生物件值
  155. WriteDeductRequest(_Req);
  156. //
  157. CallIcerApi(_Req);
  158. } catch (Exception e) {
  159. // webccrlog.error(webccr.getClass(), e);
  160. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  161. callBack(false, "");
  162. }
  163. }
  164. // 2 加值(DepoistUUcard)
  165. public void AddValueUUcard() {
  166. // webccrlog.info(webccr.getClass(),"-----悠遊卡加值-----");
  167. // webccrlog.info(webccr.getClass(),"加值金額 : " + iUuCardAmt);
  168. CcrUtil.setMessage("-----悠遊卡加值-----");
  169. CcrUtil.setMessage("加值金額 : " + iUuCardAmt);
  170. ICERAPIReq _Req = new ICERAPIReq();
  171. try {
  172. // 產生物件值
  173. WriteAddValueRequest(_Req);
  174. //
  175. CallIcerApi(_Req);
  176. } catch (Exception e) {
  177. // webccrlog.error(webccr.getClass(), e);
  178. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  179. callBack(false, "");
  180. }
  181. }
  182. // 3 取消扣款
  183. public void CancelDeductUUcard() {
  184. // webccrlog.info(webccr.getClass(),"-----悠遊卡取消扣款-----");
  185. CcrUtil.setMessage("-----悠遊卡取消扣款-----");
  186. // 正常不會是null
  187. if (oPayment == null) {
  188. ccr.UInotify.swErrMsg("查無悠遊卡付款項!");
  189. return;
  190. }
  191. // 呼叫卡機流程 
  192. ICERAPIReq _Req = new ICERAPIReq();
  193. try {
  194. // 產生物件值
  195. WriteCancelDeductRequest(_Req);
  196. //
  197. CallIcerApi(_Req);
  198. } catch (Exception e) {
  199. // webccrlog.error(webccr.getClass(), e);
  200. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  201. callBack(false, "");
  202. }
  203. }
  204. // 4 查詢餘額
  205. public void QueryUUcardBalance() {
  206. // webccrlog.info(webccr.getClass(),"-----悠遊卡查詢餘額-----");
  207. CcrUtil.setMessage("-----悠遊卡查詢餘額-----");
  208. TYPE = 4;
  209. ICERAPIReq _Req = new ICERAPIReq();
  210. try {
  211. // 產生物件值
  212. WriteQueryRequest(_Req);
  213. //
  214. CallIcerApi(_Req);
  215. } catch (Exception e) {
  216. // webccrlog.error(webccr.getClass(), e);
  217. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  218. callBack(false, "");
  219. }
  220. }
  221. // 5 dongle機交易明細查詢
  222. public void QueryUUcardRecord() {
  223. // webccrlog.info(webccr.getClass(),"-----悠遊卡dongle機交易明細查詢-----");
  224. CcrUtil.setMessage("-----悠遊卡dongle機交易明細查詢-----");
  225. if (sNOlist.size() > 0) {
  226. // 取當前的資料
  227. sNO = sNOlist.get(0);
  228. // webccrlog.info(webccr.getClass(),"目前查詢序號=" + sNO);
  229. CcrUtil.setMessage("目前查詢序號=" + sNO);
  230. ICERAPIReq _Req = new ICERAPIReq();
  231. try {
  232. // 產生物件值
  233. WriteQueryRecordRequest(_Req);
  234. //
  235. CallIcerApi(_Req);
  236. } catch (Exception e) {
  237. // webccrlog.error(webccr.getClass(), e);
  238. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  239. }
  240. } else {
  241. // webccrlog.info(webccr.getClass(),"陣列已經為0");
  242. CcrUtil.setMessage("陣列已經為0");
  243. if (parentWin != null) {
  244. ccr.UInotify.swMessage("重傳完畢");
  245. parentWin.close();
  246. }
  247. }
  248. }
  249. // 6 退款
  250. public void ReturnUUcard() {
  251. // webccrlog.info(webccr.getClass(),"-----悠遊卡扣款作廢-----");
  252. CcrUtil.setMessage("-----悠遊卡扣款作廢-----");
  253. if (salePList.size() != 0) {
  254. // 取當前的資料
  255. oPayment = salePList.get(0);
  256. ICERAPIReq _Req = new ICERAPIReq();
  257. try {
  258. // 產生物件值
  259. WriteReturnRequest(_Req);
  260. //
  261. CallIcerApi(_Req);
  262. } catch (Exception e) {
  263. // webccrlog.error(webccr.getClass(), e);
  264. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  265. }
  266. } else {
  267. callBack(false, ""); // 正常作廢進來要有付款項
  268. }
  269. }
  270. // 7 加值作廢
  271. public void CancelAddValueUUcard() {
  272. // webccrlog.info(webccr.getClass(),"-----悠遊卡加值作廢-----");
  273. CcrUtil.setMessage("-----悠遊卡加值作廢-----");
  274. ICERAPIReq _Req = new ICERAPIReq();
  275. try {
  276. // 產生物件值
  277. WriteCancelAddValueRequest(_Req);
  278. //
  279. CallIcerApi(_Req);
  280. } catch (Exception e) {
  281. // webccrlog.error(webccr.getClass(), e);
  282. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  283. callBack(false, "");
  284. }
  285. }
  286. private void CallIcerApi(ICERAPIReq _Req) {
  287. iRetry = 0; // retry次數
  288. // 讀檔
  289. JavaScript.getCurrent().addFunction("getOutFileContent", new JavaScriptFunction() {
  290. public void call(JsonArray arguments) {
  291. CcrUtil.setMessage("---UU_CARD getOutFileContent---");
  292. String _sRespones = arguments.getString(0);
  293. String _sXML = arguments.getString(1);
  294. CcrUtil.setMessage("ICERAPI.REQ content :" + _Req.toString());
  295. CcrUtil.setMessage("ICERAPI.RES content :" + _sRespones);
  296. if (emisUtil.isEmpty(_sRespones)) {
  297. ccr.UInotify.swErrMsg("ICERAPI無回應");
  298. callBack(false, "");
  299. return;
  300. } else {
  301. // 將回傳資料寫入 EASYCARD
  302. if (TYPE == 1 || TYPE == 2 || TYPE == 3 ||
  303. TYPE == 5 || TYPE == 6 || TYPE == 7) {
  304. String SL_KEY = ccr.getCcrID() + emisUtil.todayDateAll() + TYPE;
  305. boolean IS_RESEND = true;
  306. if (ccr.getCurrentSale() != null) {
  307. SL_KEY = ccr.getCurrentSale().getPrimarykey();
  308. if (TYPE != 5) {
  309. IS_RESEND = false;
  310. }
  311. }
  312. // 寫入table給後台傳到悠遊卡公司
  313. ccr.wrDataToDb.InsEASYCARD(SL_KEY, _sRespones, IS_RESEND, _Req, iRetry);
  314. }
  315. // 解析ICERAPI
  316. ICERAPIResp resp = ReadResponseFile(_sRespones);
  317. if (bDevelopMode) {
  318. MessageBox _oMsgBox = MessageBox.createInfo();
  319. _oMsgBox.withCaption("訊息")
  320. .withMessage("是否進行Retry?")
  321. .withYesButton(() -> {
  322. iRetry++;
  323. MessageBox _oErrBox = MessageBox.createError();
  324. _oErrBox.withCaption("錯誤")
  325. .withMessage("卡機連線Timeout, 請按下確認鍵Rerry! Retry次數:" + iRetry)
  326. .withOkButton(() -> {
  327. // webccrlog.info(webccr.class, "-------進行Retry動作-------");
  328. CcrUtil.setMessage("-------進行Retry動作-------");
  329. if (iRetry > 3) {
  330. ccr.UInotify.swErrMsg("Retry已超過3次!");
  331. callBack(false, "");
  332. return;
  333. }
  334. if (iRetry <= 3) {
  335. _Req.setT1200(emisUtil.todayTimeS());
  336. _Req.setT1300(emisUtil.todayDate());
  337. uRunIcerApi(_Req);
  338. }
  339. }, ButtonOption.focus()).open();
  340. }, ButtonOption.focus())
  341. .withNoButton(() -> {
  342. if (resp != null) {
  343. // 有回傳時, 檢查是否符合retry條件, 再進入retry程序
  344. if ( ("-125".equals(resp.getT3901()) || "6008".equals(resp.getT3904())) ) {
  345. iRetry++;
  346. MessageBox _oErrBox = MessageBox.createError();
  347. _oErrBox.withCaption("錯誤")
  348. .withMessage("卡機連線Timeout, 請按下確認鍵Rerry! Retry次數:" + iRetry)
  349. .withOkButton(() -> {
  350. // webccrlog.info(webccr.class, "-------進行Retry動作-------");
  351. CcrUtil.setMessage("-------進行Retry動作-------");
  352. if (iRetry > 3) {
  353. ccr.UInotify.swErrMsg("Retry已超過3次!");
  354. callBack(false, "");
  355. return;
  356. }
  357. if (iRetry <= 3) {
  358. _Req.setT1200(emisUtil.todayTimeS());
  359. _Req.setT1300(emisUtil.todayDate());
  360. uRunIcerApi(_Req);
  361. }
  362. }, ButtonOption.focus()).open();
  363. } else {
  364. doAction(resp, _Req);
  365. if (TYPE == 0) {
  366. // 變更XML
  367. updateINI(_sXML);
  368. }
  369. }
  370. } else {
  371. ccr.UInotify.swErrMsg("出現異常:超出解析的TAG\n" + _sRespones);
  372. callBack(false, "");
  373. return;
  374. }
  375. }).open();
  376. } else {
  377. if (resp != null) {
  378. // 有回傳時, 檢查是否符合retry條件, 再進入retry程序
  379. if ( ("-125".equals(resp.getT3901()) || "6008".equals(resp.getT3904())) ) {
  380. iRetry++;
  381. MessageBox _oErrBox = MessageBox.createError();
  382. _oErrBox.withCaption("錯誤")
  383. .withMessage("卡機連線Timeout, 請按下確認鍵Rerry! Retry次數:" + iRetry)
  384. .withOkButton(() -> {
  385. CcrUtil.setMessage("-------進行Retry動作-------");
  386. if (iRetry > 3) {
  387. ccr.UInotify.swErrMsg("Retry已超過3次!");
  388. callBack(false, "");
  389. return;
  390. }
  391. if (iRetry <= 3) {
  392. _Req.setT1200(emisUtil.todayTimeS());
  393. _Req.setT1300(emisUtil.todayDate());
  394. uRunIcerApi(_Req);
  395. }
  396. }, ButtonOption.focus()).open();
  397. } else {
  398. doAction(resp, _Req);
  399. if (TYPE == 0) {
  400. // 變更XML
  401. updateINI(_sXML);
  402. }
  403. }
  404. } else {
  405. ccr.UInotify.swErrMsg("出現異常:超出解析的TAG\n" + _sRespones);
  406. callBack(false, "");
  407. return;
  408. }
  409. }
  410. }
  411. }
  412. });
  413. JavaScript.getCurrent().addFunction("ErrorMessage",new JavaScriptFunction() {
  414. public void call(JsonArray arguments) {
  415. try {
  416. CcrUtil.setMessage("ErrorMessage:" + arguments.getString(0));
  417. int iErrType = emisUtil.parseInt(arguments.getString(0), 0);
  418. switch (iErrType) {
  419. case 1:
  420. ccr.UInotify.swErrMsg("REQ, RES 檔有問題. 請通知資訊人員處理!");
  421. break;
  422. case 2:
  423. ccr.UInotify.swErrMsg("ICERAPI無回應");
  424. break;
  425. default:
  426. ccr.UInotify.swErrMsg("非設定的錯誤訊息, 請通知資訊人員處理!");
  427. break;
  428. }
  429. } catch (Exception e) {
  430. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  431. } finally {
  432. callBack(false, "");
  433. }
  434. }
  435. });
  436. JavaScript.getCurrent().addFunction("CheckFile", new JavaScriptFunction() {
  437. public void call(JsonArray arguments) {
  438. CcrUtil.setMessage("---UU_CARD CheckFile---");
  439. String _sReq = arguments.getString(0);
  440. CcrUtil.setMessage("File => " + _sReq);
  441. if (_sReq.indexOf("<T0300>") > -1) {
  442. String T0300 = _sReq.substring(_sReq.indexOf("<T0300>") + "<T0300>".length(),
  443. _sReq.indexOf("</T0300>"));
  444. if (T0300 != null && !T0300.equals(_Req.T0300)) {
  445. ccr.UInotify.swErrMsg("RES 檔有問題. 請通知資訊人員處理!");
  446. callBack(false, "");
  447. return;
  448. }
  449. }
  450. if (_sReq.indexOf("<T1200>") > -1) {
  451. String T1200 = _sReq.substring(_sReq.indexOf("<T1200>") + "<T1200>".length(),
  452. _sReq.indexOf("</T1200>"));
  453. if (T1200 != null && !T1200.equals(_Req.T1200)) {
  454. ccr.UInotify.swErrMsg("RES 檔有問題. 請通知資訊人員處理!");
  455. callBack(false, "");
  456. return;
  457. }
  458. }
  459. if (_sReq.indexOf("<T1300>") > -1) {
  460. String T1300 = _sReq.substring(_sReq.indexOf("<T1300>") + "<T1300>".length(),
  461. _sReq.indexOf("</T1300>"));
  462. if (T1300 != null && !T1300.equals(_Req.T1300)) {
  463. ccr.UInotify.swErrMsg("RES 檔有問題. 請通知資訊人員處理!");
  464. callBack(false, "");
  465. return;
  466. }
  467. }
  468. uRunIcerApi2();
  469. }
  470. });
  471. uRunIcerApi(_Req);
  472. }
  473. private void uRunIcerApi(ICERAPIReq _Req) {
  474. String PATH = ccr.getPLUG_DISK() + "emis/senao/ICERAPI/"; // 悠遊卡
  475. String DATA_PATH = PATH + "ICERData/"; // DATA路徑
  476. String INI_XML = PATH + "ICERINI.xml"; // XML路徑
  477. String IN_FILE = DATA_PATH + "ICERAPI.REQ"; // REQ檔
  478. String IN_FILE_OK = DATA_PATH + "ICERAPI.REQ.OK"; // REQ檔
  479. String OUT_FILE = DATA_PATH + "ICERAPI.RES"; // RES檔
  480. String OUT_FILE_OK = DATA_PATH + "ICERAPI.RES.OK"; // RES檔
  481. String BAT_FILE1 = PATH + "ICERAPI1.bat"; // 刪檔bat
  482. String BAT_FILE2 = PATH + "ICERAPI2.bat"; //批次檔做為開啟exe
  483. String BAT_CONTENT1 = " del " + IN_FILE + sNewLine +
  484. " del " + IN_FILE_OK + sNewLine +
  485. " del " + OUT_FILE + sNewLine +
  486. " del " + OUT_FILE_OK;
  487. String BAT_CONTENT2 = "cd " + PATH + sNewLine + "ICERAPI.exe";
  488. String ContentXML = "";
  489. if (TYPE == 0) {
  490. ContentXML = CreateXML(_Req); // 登入
  491. } else if (TYPE == 1) {
  492. ContentXML = CreateXML1(_Req); // 扣款
  493. } else if (TYPE == 2) {
  494. ContentXML = CreateXML2(_Req); // 加值
  495. } else if (TYPE == 3) {
  496. ContentXML = CreateXML3(_Req); // 取消付款
  497. } else if (TYPE == 4) {
  498. ContentXML = CreateXML4(_Req); // 查詢
  499. } else if (TYPE == 5) {
  500. ContentXML = CreateXML5(_Req); // DONGLE 重傳
  501. } else if (TYPE == 6) {
  502. ContentXML = CreateXML6(_Req); // 扣款作廢
  503. } else if (TYPE == 7) {
  504. ContentXML = CreateXML7(_Req); // 加值作廢
  505. }
  506. JavaScript.getCurrent().execute(
  507. " var util = new emisUtility(); \n" +
  508. // 設定路徑和檔案參數
  509. " var _sINI_XML = '" + INI_XML + "'; \n" +
  510. " var _sREQ_PATH = '" + IN_FILE + "'; \n" +
  511. " var _sREQ_PATH_OK = '" + IN_FILE_OK + "'; \n" +
  512. " var _sRES_PATH = '" + OUT_FILE + "'; \n" +
  513. " var _sRES_PATH_OK = '" + OUT_FILE_OK + "'; \n" +
  514. " var _sBAT_FILE1 = '" + BAT_FILE1 + "'; \n" +
  515. " var _sBAT_FILE2 = '" + BAT_FILE2 + "'; \n" +
  516. " var _sBAT_CONTENT1 = '" + BAT_CONTENT1 + "'; \n" +
  517. " var _sBAT_CONTENT2 = '" + BAT_CONTENT2 + "'; \n" +
  518. " var _sINContent = '" + ContentXML + "'; \n" +
  519. // 寫入BAT1檔
  520. " console.log('is or not write => BAT1'); \n" +
  521. " if (!util.exists(_sBAT_FILE1)) {" +
  522. " util.writeStringToUTF8File(_sBAT_FILE1, _sBAT_CONTENT1); " +
  523. " } \n" +
  524. // 執行刪檔
  525. " console.log('deletefile by BAT1 => REQ and RES'); \n" +
  526. " util.executewait(_sBAT_FILE1); \n" +
  527. //寫入REQ檔
  528. " console.log('write => REQ'); \n" +
  529. " util.writeStringToUTF8File(_sREQ_PATH, _sINContent); \n" +
  530. //寫入REQ.OK檔
  531. " console.log('write => REQ.OK'); \n" +
  532. " util.writeStringToUTF8File(_sREQ_PATH_OK, ''); \n" +
  533. //辨識檔案是否有寫成功
  534. " console.log('check => REQ'); \n" +
  535. " if (!util.exists(_sREQ_PATH)) { \n" +
  536. " ErrorMessage('1'); \n " +
  537. " } else {\n" +
  538. " var _sStr = util.readFileAsString(_sREQ_PATH); \n" +
  539. " CheckFile(_sStr); \n" +
  540. " }"
  541. /*
  542. //寫入BAT2檔
  543. " console.log('is or not write => BAT2'); \n" +
  544. " if (!util.exists(_sBAT_FILE2)) {" +
  545. " util.writeStringToUTF8File(_sBAT_FILE2, _sBAT_CONTENT2); } \n" +
  546. //執行ICERAPI.bat
  547. " console.log('executewait => BAT2'); \n" +
  548. " util.executewait(_sBAT_FILE2); \n" +
  549. //讀取out.dat
  550. " console.log('is or not read => RES'); \n" +
  551. " if (util.exists(_sRES_PATH_OK)) { \n" +
  552. " var _sStr = util.readFileAsString(_sRES_PATH); \n" +
  553. " var _sStr2 = util.readFileAsString(_sINI_XML); \n" +
  554. " getOutFileContent(_sStr, _sStr2); \n" +
  555. " console.log('End'); \n" +
  556. " } else { \n" +
  557. " ErrorMessage('2'); \n" +
  558. " return; \n" +
  559. " }"
  560. */
  561. );
  562. }
  563. private void uRunIcerApi2() {
  564. String PATH = ccr.getPLUG_DISK() + "emis/senao/ICERAPI/"; // 悠遊卡
  565. String DATA_PATH = PATH + "ICERData/"; // DATA路徑
  566. String INI_XML = PATH + "ICERINI.xml"; // XML路徑
  567. String OUT_FILE = DATA_PATH + "ICERAPI.RES"; // RES檔
  568. String OUT_FILE_OK = DATA_PATH + "ICERAPI.RES.OK"; // RES檔
  569. String BAT_FILE2 = PATH + "ICERAPI2.bat"; //批次檔做為開啟exe
  570. String BAT_CONTENT2 = "cd " + PATH + sNewLine + "ICERAPI.exe";
  571. JavaScript.getCurrent().execute(
  572. " var util = new emisUtility(); \n" +
  573. // 設定路徑和檔案參數
  574. " var _sINI_XML = '" + INI_XML + "'; \n" +
  575. " var _sRES_PATH = '" + OUT_FILE + "'; \n" +
  576. " var _sRES_PATH_OK = '" + OUT_FILE_OK + "'; \n" +
  577. " var _sBAT_FILE2 = '" + BAT_FILE2 + "'; \n" +
  578. " var _sBAT_CONTENT2 = '" + BAT_CONTENT2 + "'; \n" +
  579. //寫入BAT2檔
  580. " console.log('is or not write => BAT2'); \n" +
  581. " if (!util.exists(_sBAT_FILE2)) {" +
  582. " util.writeStringToUTF8File(_sBAT_FILE2, _sBAT_CONTENT2); } \n" +
  583. //執行ICERAPI.bat
  584. " console.log('executewait => BAT2'); \n" +
  585. " util.executewait(_sBAT_FILE2); \n" +
  586. //讀取out.dat
  587. " console.log('is or not read => RES'); \n" +
  588. " if (util.exists(_sRES_PATH_OK)) { \n" +
  589. " var _sStr = util.readFileAsString(_sRES_PATH); \n" +
  590. " var _sStr2 = util.readFileAsString(_sINI_XML); \n" +
  591. " getOutFileContent(_sStr, _sStr2); \n" +
  592. " console.log('End'); \n" +
  593. " } else { \n" +
  594. " ErrorMessage('2'); \n" +
  595. " return; \n" +
  596. " }"
  597. );
  598. }
  599. private ICERAPIResp ReadResponseFile(String content) {
  600. BufferedReader br = null;
  601. ByteArrayInputStream bis = null;
  602. StringBuffer sb = new StringBuffer();
  603. ICERAPIResp resp = null;
  604. try {
  605. sb.append(content);
  606. //
  607. resp = new ICERAPIResp();
  608. bis = new ByteArrayInputStream(sb.toString().getBytes(CHARSET));
  609. Document doc = emisXmlUtil.getXML(bis);
  610. NodeList Nodelist = doc.getElementsByTagName("TRANS");
  611. NodeList Nodelistitem = doc.getElementsByTagName("T5548");
  612. ArrayList<ICERAPIRespItem> respItem = new ArrayList<ICERAPIRespItem>();
  613. ICERAPIRespItem item = null;
  614. for (int i = 0; i < Nodelist.getLength(); i++) {
  615. Node node = Nodelist.item(i);
  616. Node child = node.getFirstChild();
  617. while (child != null) {
  618. // System.out.println(child.getNodeName());
  619. Method method = ReflectUtils.getMethodByName(resp, "set" + child.getNodeName());
  620. if (method != null) {
  621. try {
  622. if (child.getNodeType() == node.ELEMENT_NODE) {
  623. Element element = (Element) child;
  624. if (child.getNodeName().equals(element.getTagName())) {
  625. Node nItem = Nodelistitem.item(0);
  626. if (nItem != null && child.getNodeName().equals(nItem.getNodeName())) {
  627. //
  628. item = new ICERAPIRespItem();
  629. Node childitem = nItem.getFirstChild();
  630. while (childitem != null) {
  631. // System.out.println(childitem.getNodeName());
  632. Method methodItem = ReflectUtils.getMethodByName(item, "set" + childitem.getNodeName());
  633. if (methodItem != null) {
  634. if (childitem.getNodeType() == nItem.ELEMENT_NODE) {
  635. Element elementitem = (Element) childitem;
  636. if (childitem.getNodeName().equals(elementitem.getTagName())) {
  637. if (childitem.getFirstChild() != null) {
  638. methodItem.invoke(item,
  639. emisUtil.parseString(elementitem.getFirstChild().getNodeValue()));
  640. }
  641. }
  642. }
  643. }
  644. childitem= childitem.getNextSibling();
  645. }
  646. //
  647. respItem.add(item);
  648. if (child.getFirstChild() != null) {
  649. method.invoke(resp,
  650. respItem);
  651. }
  652. } else {
  653. if (child.getFirstChild() != null) {
  654. method.invoke(resp,
  655. emisUtil.parseString(element.getFirstChild().getNodeValue()));
  656. }
  657. }
  658. }
  659. }
  660. } catch (Exception e) {
  661. // e.printStackTrace();
  662. // webccrlog.error(webccr.getClass(), e);
  663. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  664. }
  665. }
  666. child= child.getNextSibling();
  667. }
  668. }
  669. // CcrUtil.setMessage(resp.toString());
  670. } catch (Exception e) {
  671. // e.printStackTrace();
  672. // webccrlog.error(webccr.getClass(), e);
  673. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  674. return null;
  675. } finally {
  676. try {
  677. if(br != null) br.close();
  678. if(bis != null) bis.close();
  679. } catch (Exception e) {
  680. }
  681. }
  682. return resp;
  683. }
  684. private void doAction(ICERAPIResp resp, ICERAPIReq _Req) {
  685. if (TYPE == 0) {
  686. // 登入
  687. if ("0".equals(resp.getT3901())) {
  688. CcrUtil.setMessage("---UU_CARD 登入成功---");
  689. } else {
  690. // ERROR_MSG(resp);
  691. }
  692. } else if (TYPE == 1) {
  693. // 扣款
  694. if ("0".equals(resp.getT3901())) {
  695. if (!_Req.T1200.equals(resp.getT1200()) ||
  696. !_Req.T1300.equals(resp.getT1300())) {
  697. //CcrUtil.setMessage("res內容與req所回傳不一致");
  698. ccr.UInotify.swErrMsg("res內容與req所回傳不一致");
  699. callBack(false, "");
  700. return;
  701. }
  702. CcrUtil.setMessage("---UU_CARD 扣款成功---");
  703. // 悠遊卡付款
  704. oPayment = new TPayment(TCcrConst.PAY_77, TCcrConst.PAY_NAME_77);
  705. oPayment.setSn(ccr.getCurrentSale().getPaymentCount() + 1); // RECNO
  706. oPayment.setAmount(iUuCardAmt); // 付款金額
  707. oPayment.setEASY_CARDNO(emisUtil.parseString(resp.getT0200())); // 悠遊卡明碼(T0211, 隱碼為載具使用) 為T0200
  708. oPayment.setSP_FIELD1("UU");
  709. oPayment.setSP_FIELD3(emisUtil.parseString(resp.getT0200()));
  710. ccr.getCurrentSale().addPayment(oPayment);
  711. // 列印內容
  712. String SL_KEY = ccr.getCurrentSale().getPrimarykey();
  713. String content = PrintContent(resp);
  714. // 列印扣款憑證
  715. PrintUUCard(content, SL_KEY);
  716. // 更新資料避免發生錯誤
  717. ccr.UInotify.updateMainForm();
  718. if (parentEvent != null) {
  719. parentEvent.finalProcedure();
  720. }
  721. callBack(true, "");
  722. // 尚有付款金額
  723. if (ccr.getCurrentSale().getUnpaidAmout() != 0) {
  724. } else {
  725. // 回到按鈕事件
  726. if (parentEvent != null) {
  727. //add by Ares 20191218 交易完成後不顯示結束確認視窗
  728. /*
  729. String MSG = "交易款項已付清, 是否結束本交易?";
  730. CcrUtil.setMessage("悠遊卡已付款是否結束本交易:" + ccr.getCurrentSale().getPrimarykey());
  731. ccr.UInotify.askYesOrNo(true, MSG, parentEvent, "clickYes", "clickNo");
  732. */
  733. CcrUtil.setMessage("悠遊卡已付款, 確認結束交易:" + ccr.getCurrentSale().getPrimarykey());
  734. runEndSale();
  735. } else {
  736. runEndSale();
  737. }
  738. }
  739. } else {
  740. ERROR_MSG(resp);
  741. callBack(false, "");
  742. }
  743. } else if (TYPE == 2) {
  744. // 加值
  745. if ("0".equals(resp.getT3901())) {
  746. if (!_Req.T1200.equals(resp.getT1200()) ||
  747. !_Req.T1300.equals(resp.getT1300())) {
  748. ccr.UInotify.swErrMsg("res內容與req所回傳不一致");
  749. callBack(false, "");
  750. return;
  751. }
  752. CcrUtil.setMessage("---UU_CARD 加值成功 and 後續動作---");
  753. // 現金付款 
  754. oPayment = new TPayment(TCcrConst.PAY_CASH, TCcrConst.PAY_NAME_CASH);
  755. oPayment.setSn(ccr.getCurrentSale().getPaymentCount() + 1); // RECNO
  756. oPayment.setAmount(iUuCardAmt);
  757. oPayment.setSP_FIELD1("UU");
  758. oPayment.setSP_FIELD3(emisUtil.parseString(resp.getT0200()));
  759. oPayment.setEASY_CARDNO(emisUtil.parseString(resp.getT0200()));
  760. ccr.getCurrentSale().addPayment(oPayment);
  761. ccr.getCurrentSale().setbUUAddValue(true); // 判斷為悠遊卡加值
  762. ccr.UInotify.updateMainForm();
  763. // 列印內容
  764. String SL_KEY = ccr.getCurrentSale().getPrimarykey();
  765. String content = PrintContent(resp);
  766. // 列印加值憑證
  767. PrintUUCard(content, SL_KEY);
  768. // 存檔
  769. runEndSale();
  770. //
  771. callBack(true, "");
  772. } else {
  773. ERROR_MSG(resp);
  774. callBack(false, "");
  775. }
  776. } else if (TYPE == 3) {
  777. // 取消付款
  778. if ("0".equals(resp.getT3901())) {
  779. if (!_Req.T1200.equals(resp.getT1200()) ||
  780. !_Req.T1300.equals(resp.getT1300())) {
  781. ccr.UInotify.swErrMsg("res內容與req所回傳不一致");
  782. callBack(false, "");
  783. return;
  784. }
  785. CcrUtil.setMessage("---UU_CARD 取消付款成功---");
  786. oPayment.setCancel(true);
  787. oPayment.setEASY_CARDNO_T0211(emisUtil.parseString(resp.getT0211()));
  788. // 列印內容
  789. String SL_KEY = ccr.getCurrentSale().getPrimarykey();
  790. String content = PrintContent(resp);
  791. // 列印取消付款憑證
  792. PrintUUCard(content, SL_KEY);
  793. // 返回取消付款按鈕的事件
  794. callBack(true, "");
  795. } else {
  796. // 是否返回按鈕?
  797. ERROR_MSG(resp);
  798. // 返回取消付款按鈕的事件
  799. callBack(false, "");
  800. }
  801. } else if (TYPE == 4) {
  802. // 查詢
  803. if ("0".equals(resp.getT3901())) {
  804. if (!_Req.T1200.equals(resp.getT1200()) ||
  805. !_Req.T1300.equals(resp.getT1300())) {
  806. ccr.UInotify.swErrMsg("res內容與req所回傳不一致");
  807. callBack(false, "");
  808. return;
  809. }
  810. String sAmount = resp.getT0410();
  811. try {
  812. int iAmount = emisUtil.parseInt(sAmount) / 100;
  813. CcrUtil.setMessage("悠遊卡餘額 = " + iAmount);
  814. ccr.UInotify.wrMessageToMainDisplay("悠遊卡餘額 = " + iAmount);
  815. } catch (Exception e) {
  816. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  817. ccr.UInotify.swErrMsg("悠遊卡餘額 = " + sAmount);
  818. }
  819. callBack(true, "");
  820. } else {
  821. ERROR_MSG(resp);
  822. callBack(false, "");
  823. }
  824. } else if (TYPE == 5) {
  825. // DONGLE 重傳
  826. if ("0".equals(resp.getT3901())) {
  827. if (!_Req.T1200.equals(resp.getT1200()) ||
  828. !_Req.T1300.equals(resp.getT1300())) {
  829. ccr.UInotify.swErrMsg("res內容與req所回傳不一致");
  830. callBack(false, "");
  831. return;
  832. }
  833. CcrUtil.setMessage("序號:" + sNO + "---重傳成功---");
  834. //將當前處理的sNO刪除\
  835. if (sNOlist.size() > 0) {
  836. sNOlist.remove(0);
  837. }
  838. // webccrlog.info(webccr.getClass(),"移除當前序號:" + sNO);
  839. CcrUtil.setMessage("移除當前序號:" + sNO);
  840. // 一直call QueryUUcardRecord 直到 sNOlist陣列為0.
  841. QueryUUcardRecord();
  842. } else {
  843. ERROR_MSG(resp);
  844. if (parentWin != null) {
  845. parentWin.close();
  846. }
  847. }
  848. } else if (TYPE == 6) {
  849. // 扣款作廢
  850. if ("0".equals(resp.getT3901())) {
  851. if (!_Req.T1200.equals(resp.getT1200()) ||
  852. !_Req.T1300.equals(resp.getT1300())) {
  853. ccr.UInotify.swErrMsg("res內容與req所回傳不一致");
  854. callBack(false, "");
  855. return;
  856. }
  857. CcrUtil.setMessage("---UU_CARD 扣款作廢成功---");
  858. // 返回作廢動作
  859. //將當前處理的SALE_P刪除
  860. if (salePList.size() > 0) {
  861. salePList.remove(0);
  862. }
  863. // 列印內容
  864. String SL_KEY = ccr.getCurrentSale().getPrimarykey();
  865. String content = PrintContent(resp);
  866. // 列印退貨憑證
  867. PrintUUCard(content, SL_KEY);
  868. //
  869. if (salePList.size() > 0) {
  870. MessageBox _oMsgBox = MessageBox.createInfo();
  871. _oMsgBox.withCaption("訊息")
  872. .withMessage("處理完成,尚有悠遊卡" + salePList.size() + "筆交易未完成,請按確定繼續。")
  873. .withYesButton(() -> {
  874. // 取當前的資料
  875. oPayment = salePList.get(0);
  876. ICERAPIReq Req = new ICERAPIReq();
  877. try {
  878. // 產生物件值
  879. WriteReturnRequest(Req);
  880. //
  881. CallIcerApi(Req);
  882. } catch (Exception e) {
  883. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  884. }
  885. }, ButtonOption.focus())
  886. .open();
  887. } else {
  888. callBack(true, "");
  889. }
  890. } else {
  891. ERROR_MSG(resp);
  892. callBack(false, "");
  893. }
  894. } else if (TYPE == 7) {
  895. // 加值作廢
  896. if ("0".equals(resp.getT3901())) {
  897. if (!_Req.T1200.equals(resp.getT1200()) ||
  898. !_Req.T1300.equals(resp.getT1300())) {
  899. ccr.UInotify.swErrMsg("res內容與req所回傳不一致");
  900. callBack(false, "");
  901. return;
  902. }
  903. CcrUtil.setMessage("---UU_CARD 加值作廢成功---");
  904. // 列印內容
  905. String SL_KEY = ccr.getCurrentSale().getPrimarykey();
  906. String content = PrintContent(resp);
  907. // 列印退貨憑證
  908. PrintUUCard(content, SL_KEY);
  909. // 返回作廢動作
  910. callBack(true, "");
  911. } else {
  912. ERROR_MSG(resp);
  913. callBack(false, "");
  914. }
  915. }
  916. }
  917. // 修改INI.XML
  918. private void updateINI(String INI) {
  919. StringBuffer sb = new StringBuffer();
  920. INI = INI.replace("??","<");
  921. INI = INI.replaceAll("\r\n", "\\\\r\\\\n");
  922. try {
  923. String S_NO = emisString.lPadB(ccr.getStore().getFLDxS_NO(), 10, '0');
  924. String ID_NO = ccr.getCcrID().substring(6);
  925. int S_NO_S = INI.indexOf("<TMLocationID>");
  926. int S_NO_E = INI.indexOf("</TMLocationID>");
  927. int ID_NO_S = INI.indexOf("<TMID>");
  928. int ID_NO_E = INI.indexOf("</TMID>");
  929. sb.append(INI.substring(0, S_NO_S + "<TMLocationID>".length()));
  930. sb.append(S_NO);
  931. sb.append(INI.substring(S_NO_E, ID_NO_S + "<TMID>".length()));
  932. sb.append(ID_NO);
  933. sb.append(INI.substring(ID_NO_E, INI.length()));
  934. System.out.println("INI = " + sb.toString());
  935. } catch (Exception e) {
  936. // webccrlog.error(webccr.getClass(), e);
  937. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  938. }
  939. if (!emisUtil.isEmpty(sb.toString())) {
  940. String PATH = ccr.getPLUG_DISK() + "emis/senao/ICERAPI/ICERINI.xml"; // 悠遊卡
  941. JavaScript.getCurrent().execute(
  942. " var util = new emisUtility(); \n" +
  943. " var path = '" + PATH + "' \n" +
  944. " var a = '" + sb.toString() + "' \n" +
  945. " util.writeStringToUTF8File(path, a);"
  946. );
  947. }
  948. }
  949. // 錯誤訊息
  950. private void ERROR_MSG(ICERAPIResp resp) {
  951. String sT3901 = resp.getT3901();
  952. String sT3902 = resp.getT3902();
  953. String sT3904 = resp.getT3904();
  954. String sT3900 = resp.getT3900();
  955. String sT3903 = resp.getT3903();
  956. StringBuffer sb = new StringBuffer();
  957. sb.append("錯誤代碼(3901):").append(emisUtil.parseString(sT3901)).append(",").append(ERROR_CODE_NAME("T3901", sT3901)).append("\n");
  958. sb.append("錯誤代碼(3900):").append(emisUtil.parseString(sT3900)).append(",").append(ERROR_CODE_NAME("T3900", sT3900)).append("\n");
  959. sb.append("錯誤代碼(3902):").append(emisUtil.parseString(sT3902)).append(",").append(ERROR_CODE_NAME("T3902", sT3902)).append("\n");
  960. sb.append("錯誤代碼(3903):").append(emisUtil.parseString(sT3903)).append(",").append(ERROR_CODE_NAME("T3903", sT3903)).append("\n");
  961. sb.append("錯誤代碼(3904):").append(emisUtil.parseString(sT3904)).append(",").append(ERROR_CODE_NAME("T3904", sT3904));
  962. ccr.UInotify.swErrMsg(sb.toString());
  963. CcrUtil.setMessage(sb.toString());
  964. }
  965. // 0 登入物件
  966. private void WriteSignOnRequest(ICERAPIReq requset) {
  967. requset.setT0100(T0100_SIGNON);
  968. requset.setT0300(T0300_SIGNON); // 特定actoin code
  969. requset.setT1100(emisString.lPadB("", 4, '0')); // 交易流水號
  970. requset.setT1200(emisUtil.todayTimeS());
  971. requset.setT1300(emisUtil.todayDate());
  972. try {
  973. Hashtable SQL_PARAMS = UU2_PARAMS.getInstance(emisUtil.getServletContext()).getHash();
  974. requset.setT4200(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T4200"),
  975. "000000000000000")); // 按機台讀取設定值
  976. requset.setT5503(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T5503"),
  977. "000000000000000")); // 門市編號
  978. } catch (Exception e) {
  979. // e.printStackTrace();
  980. // webccrlog.error(webccr.getClass(), e);
  981. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  982. }
  983. }
  984. // 1 扣款物件
  985. private void WriteDeductRequest(ICERAPIReq requset) {
  986. requset.setT0100(T0100_EACH);
  987. requset.setT0300(T0300_PAY); // 特定actoin code
  988. requset.setT0400(iUuCardAmt + "00"); // AMT
  989. requset.setT1100(emisString.lPadB(ccr.getCurrentSale().getSn() + "", 4, '0')); // 交易流水號
  990. requset.setT1200(emisUtil.todayTimeS());
  991. requset.setT1300(emisUtil.todayDate());
  992. try {
  993. Hashtable SQL_PARAMS = UU2_PARAMS.getInstance(emisUtil.getServletContext()).getHash();
  994. requset.setT4200(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T4200"),
  995. "000000000000000")); // 按機台讀取設定值
  996. requset.setT5503(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T5503"),
  997. "000000000000000")); // 門市編號
  998. } catch (Exception e) {
  999. // e.printStackTrace();
  1000. // webccrlog.error(webccr.getClass(), e);
  1001. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  1002. }
  1003. requset.setT5501("000001"); // 固定值(1天只有1批, 卡機只批一次)
  1004. requset.setT5509("E"); // 固定填入'E'
  1005. }
  1006. // 2 加值物件
  1007. private void WriteAddValueRequest(ICERAPIReq requset) {
  1008. requset.setT0100(T0100_EACH);
  1009. requset.setT0300(T0300_ADD_VALUE); // 特定actoin code
  1010. requset.setT0400(iUuCardAmt + "00"); // AMT
  1011. requset.setT1100(emisString.lPadB(ccr.getCurrentSale().getSn() + "", 4, '0')); // 交易流水號
  1012. requset.setT1200(emisUtil.todayTimeS());
  1013. requset.setT1300(emisUtil.todayDate());
  1014. try {
  1015. Hashtable SQL_PARAMS = UU2_PARAMS.getInstance(emisUtil.getServletContext()).getHash();
  1016. requset.setT4200(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T4200"),
  1017. "000000000000000")); // 按機台讀取設定值
  1018. requset.setT5503(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T5503"),
  1019. "000000000000000")); // 門市編號
  1020. } catch (Exception e) {
  1021. // e.printStackTrace();
  1022. // webccrlog.error(webccr.getClass(), e);
  1023. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  1024. }
  1025. requset.setT5501("000001"); // 固定值(1天只有1批, 卡機只批一次)
  1026. requset.setT5509("E"); // 固定填入'E'
  1027. }
  1028. // 3 取消付款/重新付款 物件
  1029. private void WriteCancelDeductRequest(ICERAPIReq requset) {
  1030. requset.setT0100(T0100_EACH);
  1031. requset.setT0300(T0300_PAY_VOID); // 特定actoin code
  1032. requset.setT0400( ((int) Math.abs(oPayment.getAmount())) + "00" ); // AMT
  1033. requset.setT1100(emisString.lPadB(ccr.getCurrentSale().getSn() + "", 4, '0')); // 交易流水號
  1034. requset.setT1200(emisUtil.todayTimeS());
  1035. requset.setT1300(emisUtil.todayDate());
  1036. try {
  1037. Hashtable SQL_PARAMS = UU2_PARAMS.getInstance(emisUtil.getServletContext()).getHash();
  1038. requset.setT4200(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T4200"),
  1039. "000000000000000")); // 按機台讀取設定值
  1040. requset.setT5503(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T5503"),
  1041. "000000000000000")); // 門市編號
  1042. } catch (Exception e) {
  1043. // e.printStackTrace();
  1044. // webccrlog.error(webccr.getClass(), e);
  1045. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  1046. }
  1047. requset.setT5501("000001"); // 固定值(1天只有1批, 卡機只批一次)
  1048. requset.setT5509("E"); // 固定填入'E'
  1049. }
  1050. // 4 查詢餘額物件
  1051. private void WriteQueryRequest(ICERAPIReq requset) {
  1052. requset.setT0100(T0100_EACH);
  1053. requset.setT0300(T0300_QUERY); // 特定actoin code
  1054. requset.setT0400("000"); // {查詢填入000}
  1055. requset.setT1100(emisString.lPadB("", 4, '0')); // 交易流水號
  1056. requset.setT1200(emisUtil.todayTimeS());
  1057. requset.setT1300(emisUtil.todayDate());
  1058. try {
  1059. Hashtable SQL_PARAMS = UU2_PARAMS.getInstance(emisUtil.getServletContext()).getHash();
  1060. requset.setT4200(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T4200"),
  1061. "000000000000000")); // 按機台讀取設定值
  1062. requset.setT5503(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T5503"),
  1063. "000000000000000")); // 門市編號
  1064. } catch (Exception e) {
  1065. // e.printStackTrace();
  1066. // webccrlog.error(webccr.getClass(), e);
  1067. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  1068. }
  1069. }
  1070. // Dongle重傳物件
  1071. private void WriteQueryRecordRequest(ICERAPIReq requset) {
  1072. requset.setT0100(T0100_EACH);
  1073. requset.setT0300(T0300_DONGLE); // 特定actoin code
  1074. requset.setT1100(emisString.lPadB("", 4, '0')); // 交易流水號
  1075. requset.setT1200(emisUtil.todayTimeS());
  1076. requset.setT1300(emisUtil.todayDate());
  1077. try {
  1078. Hashtable SQL_PARAMS = UU2_PARAMS.getInstance(emisUtil.getServletContext()).getHash();
  1079. requset.setT4200(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T4200"),
  1080. "000000000000000")); // 按機台讀取設定值
  1081. requset.setT5503(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T5503"),
  1082. "000000000000000")); // 門市編號
  1083. } catch (Exception e) {
  1084. // e.printStackTrace();
  1085. // webccrlog.error(webccr.getClass(), e);
  1086. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  1087. }
  1088. requset.setT5594(sNO); // 調閱序號
  1089. }
  1090. // 6 扣款作廢物件
  1091. private void WriteReturnRequest(ICERAPIReq requset) {
  1092. requset.setT0100(T0100_EACH);
  1093. requset.setT0300(T0300_PAY_VOID); // 特定actoin code
  1094. requset.setT0400( ((int) Math.abs(oPayment.getAmount())) + "00" ); // AMT
  1095. requset.setT1100(emisString.lPadB(ccr.getCurrentSale().getSL_NO(), 4, '0')); // 原交易流水號
  1096. requset.setT1200(emisUtil.todayTimeS());
  1097. requset.setT1300(emisUtil.todayDate());
  1098. try {
  1099. Hashtable SQL_PARAMS = UU2_PARAMS.getInstance(emisUtil.getServletContext()).getHash();
  1100. requset.setT4200(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T4200"),
  1101. "000000000000000")); // 按機台讀取設定值
  1102. requset.setT5503(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T5503"),
  1103. "000000000000000")); // 門市編號
  1104. } catch (Exception e) {
  1105. // e.printStackTrace();
  1106. // webccrlog.error(webccr.getClass(), e);
  1107. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  1108. }
  1109. requset.setT5501("000001"); // 固定值(1天只有1批, 卡機只批一次)
  1110. requset.setT5509("E"); // 固定填入'E'
  1111. }
  1112. // 7 加值作廢物件
  1113. private void WriteCancelAddValueRequest(ICERAPIReq requset) {
  1114. requset.setT0100(T0100_EACH);
  1115. requset.setT0300(T0300_ADD_CANCEL); // 特定actoin code
  1116. requset.setT0400( iUuCardAmt + "00" ); // AMT
  1117. requset.setT1100(emisString.lPadB(ccr.getCurrentSale().getSn() + "", 4, '0')); // 交易流水號
  1118. requset.setT1200(emisUtil.todayTimeS());
  1119. requset.setT1300(emisUtil.todayDate());
  1120. try {
  1121. Hashtable SQL_PARAMS = UU2_PARAMS.getInstance(emisUtil.getServletContext()).getHash();
  1122. requset.setT4200(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T4200"),
  1123. "000000000000000")); // 按機台讀取設定值
  1124. requset.setT5503(emisUtil.parseString(SQL_PARAMS.get(ccr.getCcrID() + "T5503"),
  1125. "000000000000000")); // 門市編號
  1126. } catch (Exception e) {
  1127. // e.printStackTrace();
  1128. // webccrlog.error(webccr.getClass(), e);
  1129. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  1130. }
  1131. requset.setT5501("000001"); // 固定值(1天只有1批, 卡機只批一次)
  1132. requset.setT5509("E"); // 固定填入'E'
  1133. }
  1134. // 0 SignOn檔
  1135. private String CreateXML(ICERAPIReq requset) {
  1136. StringBuffer sb = new StringBuffer();
  1137. sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>").append(sNewLine);
  1138. sb.append("<TRANS>").append(sNewLine);
  1139. //
  1140. CreateNode(sb, "T0100", requset.T0100);
  1141. CreateNode(sb, "T0300", requset.T0300);
  1142. CreateNode(sb, "T1100", requset.T1100);
  1143. CreateNode(sb, "T1200", requset.T1200);
  1144. CreateNode(sb, "T1300", requset.T1300);
  1145. CreateNode(sb, "T4200", requset.T4200);
  1146. CreateNode(sb, "T5503", requset.T5503);
  1147. //
  1148. sb.append("</TRANS>");
  1149. return sb.toString();
  1150. }
  1151. // 1 扣款檔 
  1152. private String CreateXML1(ICERAPIReq requset) {
  1153. StringBuffer sb = new StringBuffer();
  1154. sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>").append(sNewLine);
  1155. sb.append("<TRANS>").append(sNewLine);
  1156. //
  1157. CreateNode(sb, "T0100", requset.T0100);
  1158. CreateNode(sb, "T0300", requset.T0300);
  1159. CreateNode(sb, "T0400", requset.T0400);
  1160. CreateNode(sb, "T1100", requset.T1100);
  1161. CreateNode(sb, "T1200", requset.T1200);
  1162. CreateNode(sb, "T1300", requset.T1300);
  1163. CreateNode(sb, "T4200", requset.T4200);
  1164. CreateNode(sb, "T5501", requset.T5501);
  1165. CreateNode(sb, "T5503", requset.T5503);
  1166. CreateNode(sb, "T5509", requset.T5509);
  1167. //
  1168. sb.append("</TRANS>");
  1169. return sb.toString();
  1170. }
  1171. // 2 加值檔 
  1172. private String CreateXML2(ICERAPIReq requset) {
  1173. StringBuffer sb = new StringBuffer();
  1174. sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>").append(sNewLine);
  1175. sb.append("<TRANS>").append(sNewLine);
  1176. //
  1177. CreateNode(sb, "T0100", requset.T0100);
  1178. CreateNode(sb, "T0300", requset.T0300);
  1179. CreateNode(sb, "T0400", requset.T0400);
  1180. CreateNode(sb, "T1100", requset.T1100);
  1181. CreateNode(sb, "T1200", requset.T1200);
  1182. CreateNode(sb, "T1300", requset.T1300);
  1183. CreateNode(sb, "T4200", requset.T4200);
  1184. CreateNode(sb, "T5501", requset.T5501);
  1185. CreateNode(sb, "T5503", requset.T5503);
  1186. CreateNode(sb, "T5509", requset.T5509);
  1187. //
  1188. sb.append("</TRANS>");
  1189. return sb.toString();
  1190. }
  1191. // 3 取消付款檔 
  1192. private String CreateXML3(ICERAPIReq requset) {
  1193. StringBuffer sb = new StringBuffer();
  1194. sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>").append(sNewLine);
  1195. sb.append("<TRANS>").append(sNewLine);
  1196. //
  1197. CreateNode(sb, "T0100", requset.T0100);
  1198. CreateNode(sb, "T0300", requset.T0300);
  1199. CreateNode(sb, "T0400", requset.T0400);
  1200. CreateNode(sb, "T1100", requset.T1100);
  1201. CreateNode(sb, "T1200", requset.T1200);
  1202. CreateNode(sb, "T1300", requset.T1300);
  1203. CreateNode(sb, "T4200", requset.T4200);
  1204. CreateNode(sb, "T5501", requset.T5501);
  1205. CreateNode(sb, "T5503", requset.T5503);
  1206. CreateNode(sb, "T5509", requset.T5509);
  1207. //
  1208. sb.append("</TRANS>");
  1209. return sb.toString();
  1210. }
  1211. // 4票卡查詢檔
  1212. private String CreateXML4(ICERAPIReq requset) {
  1213. StringBuffer sb = new StringBuffer();
  1214. sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>").append(sNewLine);
  1215. sb.append("<TRANS>").append(sNewLine);
  1216. //
  1217. CreateNode(sb, "T0100", requset.T0100);
  1218. CreateNode(sb, "T0300", requset.T0300);
  1219. CreateNode(sb, "T0400", requset.T0400);
  1220. CreateNode(sb, "T1100", requset.T1100);
  1221. CreateNode(sb, "T1200", requset.T1200);
  1222. CreateNode(sb, "T1300", requset.T1300);
  1223. CreateNode(sb, "T4200", requset.T4200);
  1224. CreateNode(sb, "T5503", requset.T5503);
  1225. //
  1226. sb.append("</TRANS>");
  1227. return sb.toString();
  1228. }
  1229. // 5 Dongle重傳
  1230. private String CreateXML5(ICERAPIReq requset) {
  1231. StringBuffer sb = new StringBuffer();
  1232. sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>").append(sNewLine);
  1233. sb.append("<TRANS>").append(sNewLine);
  1234. //
  1235. CreateNode(sb, "T0100", requset.T0100);
  1236. CreateNode(sb, "T0300", requset.T0300);
  1237. CreateNode(sb, "T1100", requset.T1100);
  1238. CreateNode(sb, "T1200", requset.T1200);
  1239. CreateNode(sb, "T1300", requset.T1300);
  1240. CreateNode(sb, "T4200", requset.T4200);
  1241. CreateNode(sb, "T5503", requset.T5503);
  1242. CreateNode(sb, "T5594", requset.T5594);
  1243. //
  1244. sb.append("</TRANS>");
  1245. return sb.toString();
  1246. }
  1247. // 6 扣款作廢 
  1248. private String CreateXML6(ICERAPIReq requset) {
  1249. StringBuffer sb = new StringBuffer();
  1250. sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>").append(sNewLine);
  1251. sb.append("<TRANS>").append(sNewLine);
  1252. //
  1253. CreateNode(sb, "T0100", requset.T0100);
  1254. CreateNode(sb, "T0300", requset.T0300);
  1255. CreateNode(sb, "T0400", requset.T0400);
  1256. CreateNode(sb, "T1100", requset.T1100);
  1257. CreateNode(sb, "T1200", requset.T1200);
  1258. CreateNode(sb, "T1300", requset.T1300);
  1259. CreateNode(sb, "T4200", requset.T4200);
  1260. CreateNode(sb, "T5501", requset.T5501);
  1261. CreateNode(sb, "T5503", requset.T5503);
  1262. CreateNode(sb, "T5509", requset.T5509);
  1263. //
  1264. sb.append("</TRANS>");
  1265. return sb.toString();
  1266. }
  1267. // 7 加值作廢 
  1268. private String CreateXML7(ICERAPIReq requset) {
  1269. StringBuffer sb = new StringBuffer();
  1270. sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>").append(sNewLine);
  1271. sb.append("<TRANS>").append(sNewLine);
  1272. //
  1273. CreateNode(sb, "T0100", requset.T0100);
  1274. CreateNode(sb, "T0300", requset.T0300);
  1275. CreateNode(sb, "T0400", requset.T0400);
  1276. CreateNode(sb, "T1100", requset.T1100);
  1277. CreateNode(sb, "T1200", requset.T1200);
  1278. CreateNode(sb, "T1300", requset.T1300);
  1279. CreateNode(sb, "T4200", requset.T4200);
  1280. CreateNode(sb, "T5501", requset.T5501);
  1281. CreateNode(sb, "T5503", requset.T5503);
  1282. CreateNode(sb, "T5509", requset.T5509);
  1283. //
  1284. sb.append("</TRANS>");
  1285. return sb.toString();
  1286. }
  1287. private void CreateNode(StringBuffer sb, String nodeName, String nodeValue) {
  1288. sb.append("<").append(nodeName).append(">");
  1289. if ( nodeValue == null ) {
  1290. nodeValue = "";
  1291. } else {
  1292. nodeValue = StringEscapeUtils.escapeXml(nodeValue);
  1293. }
  1294. sb.append(nodeValue);
  1295. sb.append("</").append(nodeName).append(">").append(sNewLine);
  1296. }
  1297. // 錯誤訊息代碼
  1298. private String ERROR_CODE_NAME(String TAG, String CODE) {
  1299. String result = "";
  1300. try {
  1301. Hashtable SQL_ERRMSG = UU2_ERRMSG.getInstance(emisUtil.getServletContext()).getHash();
  1302. result = emisUtil.parseString(SQL_ERRMSG.get(TAG + CODE), "");
  1303. } catch (Exception e) {
  1304. // e.printStackTrace();
  1305. // webccrlog.error(webccr.getClass(), e);
  1306. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  1307. }
  1308. return result;
  1309. }
  1310. // 交易結束
  1311. private void runEndSale() {
  1312. TLoEndSale _oEvent = new TLoEndSale();
  1313. _oEvent.go();
  1314. }
  1315. // 列印內容
  1316. private String PrintContent(ICERAPIResp resp) {
  1317. StringBuffer sb = new StringBuffer();
  1318. try {
  1319. String TYPE_NAME = T0300_PAY;
  1320. if (TYPE == 1) {
  1321. TYPE_NAME = T0300_PAY;
  1322. } else if (TYPE == 2) {
  1323. TYPE_NAME = T0300_ADD_VALUE;
  1324. } else if (TYPE == 3) {
  1325. TYPE_NAME = T0300_PAY_VOID;
  1326. } else if (TYPE == 6) {
  1327. TYPE_NAME = T0300_PAY_VOID;
  1328. } else if (TYPE == 7) {
  1329. TYPE_NAME = T0300_ADD_CANCEL;
  1330. }
  1331. //
  1332. String sYEAR = resp.getT1300().substring(0, 4) + "-"
  1333. + resp.getT1300().substring(4, 6) + "-"
  1334. + resp.getT1300().substring(6, 8);
  1335. String sTIME = resp.getT1200().substring(0,2) + ":"
  1336. + resp.getT1200().substring(2,4) + ":"
  1337. + resp.getT1200().substring(4,6);
  1338. //
  1339. String sBEF_BAL = emisUtil.parseString(resp.getT0415());
  1340. if (sBEF_BAL.length() > 0) {
  1341. sBEF_BAL = sBEF_BAL.substring(0, sBEF_BAL.length() - 2);
  1342. }
  1343. String sAMT = emisUtil.parseString(resp.getT0400());
  1344. if (sAMT.length() > 0) {
  1345. sAMT = sAMT.substring(0, sAMT.length() - 2);
  1346. }
  1347. String sAFT_BAL = emisUtil.parseString(resp.getT0410());
  1348. if (sAFT_BAL.length() > 0) {
  1349. sAFT_BAL = sAFT_BAL.substring(0, sAFT_BAL.length() - 2);
  1350. }
  1351. String sAUTO_ADD = emisUtil.parseString(resp.getT0409());
  1352. if (sAUTO_ADD.length() > 0) {
  1353. sAUTO_ADD = sAUTO_ADD.substring(0, sAUTO_ADD.length() - 2);
  1354. }
  1355. //
  1356. sb.append("[DATA]").append(sNewLine);
  1357. sb.append("STORE=").append(ccr.getStore().getFLDxS_NAME()).append(sNewLine);
  1358. sb.append("SALE_TYPE=").append(TYPE_NAME).append(sNewLine);
  1359. sb.append("CASH_ID=").append(ccr.getCcrID()).append(sNewLine);
  1360. sb.append("SA_NO=").append(ccr.getCurrentSale().getPrimarykey()).append(sNewLine);
  1361. sb.append("SALE_DATETIME=").append(sYEAR + " " + sTIME).append(sNewLine);
  1362. sb.append("CARD_NO=").append(resp.getT0215()).append(sNewLine); // 給隱碼
  1363. sb.append("BEF_BAL=").append(sBEF_BAL).append(sNewLine); //交易前餘額
  1364. sb.append("AMT=").append(sAMT).append(sNewLine); // 交易金額(加值金額)
  1365. sb.append("AFT_BAL=").append(sAFT_BAL).append(sNewLine); // 交易後餘額
  1366. sb.append("AUTO_ADD=").append(sAUTO_ADD).append(sNewLine); // 自動加值金額
  1367. sb.append("DONG_NO=").append(emisUtil.parseString(resp.getT1100())).append(sNewLine); // SL_NO
  1368. //
  1369. // webccrlog.info(webccr.getClass(), TYPE_NAME + " PrintContent:\n" + sb.toString());
  1370. CcrUtil.setMessage(TYPE_NAME + " PrintContent:\n" + sb.toString());
  1371. } catch (Exception e) {
  1372. // e.printStackTrace();
  1373. // webccrlog.error(webccr.getClass(), e);
  1374. CcrUtil.setMessage(emisUtil.getStackTrace(e));
  1375. }
  1376. return sb.toString();
  1377. }
  1378. // 列印憑證
  1379. private void PrintUUCard(String content, String SL_KEY) {
  1380. String path = ccr.getPLUG_DISK() + "emis/senao/ePrintInv/";
  1381. String filename = SL_KEY + "_" + TYPE;
  1382. JavaScript.getCurrent().execute(
  1383. " var util = new emisUtility(); \n" +
  1384. " var a = '" + content + "'; \n" +
  1385. " var c = '" + path + "RESULT.DAT'; \n" +
  1386. " var d = '" + path + filename +".UU'; \n" +
  1387. " util.deletefile(c); \n" +
  1388. " util.writeStringToUTF8File(d, a); \n"+
  1389. " util.executewait('" + path + "SenaoElecInvoicePrinter','" + filename + ".UU" + " 6'); "
  1390. );
  1391. }
  1392. // 提示訊息 --> 按關閉後,繼續執行callBack (下面不能其他動作,除非不相關)
  1393. protected void showErrMsgContinueRun(String sErrMsg_, boolean isRunOK, String info) {
  1394. MessageBox.createError()
  1395. .withCaption("錯誤")
  1396. .withMessage(sErrMsg_)
  1397. .withWidth("400px")
  1398. .withCloseButton(() -> {
  1399. // 繼續往下執行
  1400. callBack(isRunOK, info);
  1401. }, ButtonOption.focus())
  1402. .open();
  1403. }
  1404. /**
  1405. * UUCaerd執行成功、失敗
  1406. * UUCaerd完成事件之callBack,可由此method從回傳物件(自由發揮)
  1407. *
  1408. * @param isRunOK
  1409. * @return
  1410. */
  1411. private boolean callBack(boolean isRunOK, String info) {
  1412. chkOK = isRunOK;
  1413. if (this.successRunnable != null) {
  1414. successRunnable.run();
  1415. }
  1416. if (TYPE == 2 && !isRunOK) {
  1417. if (parentEvent != null) {
  1418. parentEvent.runEventMethod("PLU_ERR");
  1419. }
  1420. }
  1421. if (oWindow != null) {
  1422. oWindow.close();
  1423. }
  1424. if (!chkOK) {
  1425. time++;
  1426. }
  1427. return isRunOK;
  1428. }
  1429. public void setSuccessRunnable(Runnable successRunnable) {
  1430. this.successRunnable = successRunnable;
  1431. }
  1432. public boolean isChkOK() {
  1433. return chkOK;
  1434. }
  1435. public static void setbDevelopMode(boolean bDevelopMode1) {
  1436. bDevelopMode = bDevelopMode1;
  1437. }
  1438. public static boolean isbDevelopMode() {
  1439. return bDevelopMode;
  1440. }
  1441. public WinUuCard getoWindow() {
  1442. return oWindow;
  1443. }
  1444. public void setoWindow(WinUuCard oWindow) {
  1445. this.oWindow = oWindow;
  1446. }
  1447. public int getTime() {
  1448. return time;
  1449. }
  1450. public void setTime(int time) {
  1451. this.time = time;
  1452. }
  1453. }