@Test public void test8583Pay() { // 用户交易明细查询 000007 String hexString = "006060000381dd602200000637020000" + "20000000c08019202466313233343536" + "37382020202020202020202020203035" + "36303030373120202020202020202020" + "20203135360000070016323031313038" + "323832303131303832385a14b9403311" + "d601"; IPacket packetHelper = new ISO8583PacketHelper(); // unpack ControlMessage cm = new ControlMessage(); cm.setChanelType(CHANEL_TYPE.POS); BusinessMessage bm = new BusinessMessage(); byte[] origPacket = ConvertUtils.hexStr2Bytes(hexString); Map<DATA_TYPE, Object> map = new HashMap<DATA_TYPE, Object>(); map.put(DATA_TYPE.ORIGREQPACKET, origPacket); map.put(DATA_TYPE.CONTROLOBJECT, cm); map.put(DATA_TYPE.BUSINESSOBJECT, bm); try { packetHelper.unpackObject(map); } catch (PacketOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } cm = (ControlMessage) map.get(DATA_TYPE.CONTROLOBJECT); bm = (BusinessMessage) map.get(DATA_TYPE.BUSINESSOBJECT); System.out.println("msgtype: " + cm.getPacketHeader().getMessageType()); assertEquals("0200", cm.getPacketHeader().getMessageType()); assertEquals("000007", bm.getTranCode()); cm.setResultCode("000000"); bm.setLocalDate("20110830"); bm.setLocalTime("123143"); bm.setPbSeqno("12405"); Map<DATA_TYPE, Object> message = new HashMap<DATA_TYPE, Object>(); message.put(DATA_TYPE.CONTROLOBJECT, cm); message.put(DATA_TYPE.BUSINESSOBJECT, bm); // pack try { packetHelper.packBuffer(message); String resultHexString = ConvertUtils.bytes2HexStr((byte[]) PacketUtils.getOrigAnsPacket(message)); System.out.println(resultHexString); } catch (PacketOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/* (non-Javadoc) * @see org.apache.camel.Processor#process(org.apache.camel.Exchange) */ @Override public void process(Exchange exchange) throws Exception { Object body = exchange.getIn().getBody(); logger.info(exchange.getIn().getHeaders().toString()); if (body.getClass() == byte.class) logger.info("Received body: " + new String((byte[]) body)); else if (body.getClass() == BigEndianHeapChannelBuffer.class) logger.info( "Received body: " + ConvertUtils.bytes2HexStr(((BigEndianHeapChannelBuffer) body).array())); else logger.info("Received body: " + body.toString()); }
public static byte[] sendSopBuffer(byte[] buf) throws Exception { // 通过Socket连接服务器 Socket server = getSocket(); // 创建网络输出流输出内容到服务器上 OutputStream netOut = server.getOutputStream(); OutputStream dataOut = new BufferedOutputStream(netOut); byte[] serverbuf = new byte[2048]; try { dataOut.write(buf, 0, buf.length); // 把文件数据写出网络缓冲区 dataOut.flush(); // 刷新缓冲区把数据写往服务器端 InputStream netIn = server.getInputStream(); InputStream dataIn = new BufferedInputStream(netIn); int servernum = dataIn.read(serverbuf); logger.info("从服务器读取的数据长度:{}", servernum); if (servernum <= 0) throw new Exception("未能从前置读取有效数据!"); /* * while (servernum != (-1)) { * logger.debug("continue reading from socket..."); servernum = * dataIn.read(serverbuf);// 继续从网络中读取数据 } */ // 得到成功标志 byte[] tmp = new byte[servernum]; for (int i = 0; i < servernum; i++) { tmp[i] = serverbuf[i]; } logger.info("result:{}", new String(tmp)); String ansHexString = ConvertUtils.bytes2HexStr(tmp); logger.info("result-hex, length[:{}]:{}", ansHexString.length(), ansHexString); } catch (Exception e) { closeSocket(); throw (e); } finally { if (!KEEP_CONNECTION) { closeSocket(); } } return serverbuf; }
@Test public void test8583Pay() { // 备付金明细查询 000005 /*00676000038009602200000857020000 20000100c08019110120001030323831 39313430363531323334353637382020 20202020202020202020303532313031 39342020202020202020202020203135 36000005001132303131303832383030 312dddd89cca21af63*/ String hexString = "00676000038009602200000857020000" + "20000100c08019110120001030323831" + "39313430363531323334353637382020" + "20202020202020202020303532313031" + "39342020202020202020202020203135" + "36000005001132303131303832383030" + "312dddd89cca21af63"; // unpack ControlMessage cm = new ControlMessage(); cm.setChanelType(CHANEL_TYPE.POS); BusinessMessage bm = new BusinessMessage(); byte[] origPacket = ConvertUtils.hexStr2Bytes(hexString); Map<DATA_TYPE, Object> map = new HashMap<DATA_TYPE, Object>(); map.put(DATA_TYPE.ORIGREQPACKET, origPacket); map.put(DATA_TYPE.CONTROLOBJECT, cm); map.put(DATA_TYPE.BUSINESSOBJECT, bm); try { packetHelperISO8583.unpackObject(map); } catch (PacketOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } cm = (ControlMessage) map.get(DATA_TYPE.CONTROLOBJECT); bm = (BusinessMessage) map.get(DATA_TYPE.BUSINESSOBJECT); System.out.println("msgtype: " + cm.getPacketHeader().getMessageType()); assertEquals("0200", cm.getPacketHeader().getMessageType()); assertEquals(new Double(0.00), new Double(bm.getAmount())); assertEquals("000005", bm.getTranCode()); cm.setResultCode("000000"); bm.setLocalDate("20110830"); bm.setLocalTime("123143"); bm.setPbSeqno("12405"); Map<DATA_TYPE, Object> message = new HashMap<DATA_TYPE, Object>(); message.put(DATA_TYPE.CONTROLOBJECT, cm); message.put(DATA_TYPE.BUSINESSOBJECT, bm); // pack try { packetHelperISO8583.packBuffer(message); String resultHexString = ConvertUtils.bytes2HexStr((byte[]) PacketUtils.getOrigAnsPacket(message)); System.out.println(resultHexString); } catch (PacketOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static void main(String args[]) throws Exception { sendSopBuffer(ConvertUtils.hexStr2Bytes(packet001002())); System.out.println("send 001002 OK!"); }
@Override public Map<DATA_TYPE, Object> packBuffer(Map<DATA_TYPE, Object> message) throws PacketOperationException { logger.info("开始进行FIXSTRING打包...."); offset = 0; String packet = null; cm = PacketUtils.getControlMessage(message); bm = PacketUtils.getBusinessMessage(message); String tranCode = cm.getTranCode(); // response code convert String resultCode = cm.getResultCode(); if (GlobalConst.RESULTCODE_SUCCESS.equals(resultCode) && !"000010".equals(bm.getTranCode())) { bm.setResponseCode(GlobalConst.RESPONSECODE_SUCCESS); } else if (resultCode.equals("999999")) { // timeout or unknown result bm.setResponseCode("99"); // else // bm.setResponseCode("01"); } fieldValues.clear(); headerHelper.pack(fieldValues, cm, bm); String header = packFIXSTRING(); logger.info("交易{}打包包头完成,报文头[{}],用户号[{}]", new Object[] {tranCode, header, bm.getUserCode()}); // pack body IPacketWEBSTRING packetHelper; String body = null; try { fieldValues.clear(); packetHelper = (IPacketWEBSTRING) SpringContextHolder.getBean(getPacketBeanName(cm.getTranCode())); packetHelper.pack(fieldValues, cm, bm); body = packFIXSTRING(); packet = header + body; // 减去报文头前10位+报文体作为MAB byte[] responseMac = getMac(packet.substring(10 + 6, packet.length()).getBytes("gb2312"), bm.getShopCode()); logger.info("响应报文MAC值[{}]", responseMac); packet += ConvertUtils.bytes2HexStr(responseMac); // 报文长度处理 packet = FieldUtils.leftAddZero4FixedLengthString( String.valueOf(packet.getBytes("gb2312").length - 6), 6) + packet.substring(6); PacketUtils.setOrigAnsPacket(message, packet); } catch (InstantiationException e) { logger.error("get packet helper class error!", e); throw new PacketOperationException(); } catch (IllegalAccessException e) { logger.error("get packet helper class error!", e); throw new PacketOperationException(); } catch (ClassNotFoundException e) { logger.error("get packet helper class error!", e); throw new PacketOperationException(); } catch (Exception e) { logger.error("get packet helper class error!", e); throw new PacketOperationException(); } logger.info("交易{}报文打包完成,报文:[{}]", tranCode, packet); return message; }
// 校验MAC private boolean checkMac(BusinessMessage bm) throws Exception { // 查询是否需要校验MAC ResourceBundle rb = null; try { rb = ResourceBundle.getBundle("conf.checkFlag"); bm.setMacFlag(Boolean.valueOf(rb.getString(bm.getChanelType() + "checkMacFlag"))); // WEBMessageHead fixMsgHeadData = (WEBMessageHead)bm.getFixMsgHeadData(); WEBMessageHead webMsgHeadData = (WEBMessageHead) bm.getWebMsgHeadData(); if ("000010".equals(bm.getTranCode()) && "7".equals(webMsgHeadData.getHandleType())) { bm.setMacFlag(false); } } catch (Exception e) { logger.error("取mac校验标志错!", e); // 默认为true bm.setMacFlag(true); } logger.info("mac校验标志:[{}],交易码:[{}]!", bm.isMacFlag(), bm.getTranCode()); if (bm.isMacFlag() == false || "000903".equals(bm.getTranCode())) { return true; } // 无论是否校验MAC,都取出MAB // 从报文头(除长度位字段)+报文体(除MAC字段) // (MAB),采用ECB算法,加密结果为64位的MAC。 byte[] macData = getMacData(); bm.setMacData(macData); // MAC String fieldName = "D_EPOS_MAC"; int length = 0; String macstr = null; try { length = fieldsConfig.getFieldAsciiLength(fieldName); macstr = FieldUtils.getFixedLengthAsciiField(fieldName, buffer, offset, length, fieldsConfig); offset += length; } catch (Exception e) { e.printStackTrace(); throw new PacketOperationException("mac获取出错"); } if (null == macstr || macstr.length() != 16) { logger.error("获取MAC错误!"); return false; } // String macstr = buffer.substring(buffer.length()-16); byte[] mac = ConvertUtils.str2Bcd(macstr); // (byte[])fieldValues.get("D_EPOS_MAC"); bm.setMac(mac); byte[] origMac = bm.getMac(); if (origMac == null) { throw new PacketOperationException("上传mac值为空!"); } // logger.info("mab:[{}]", new String(macData)); // logger.info("mac:[{}]", ConvertUtils.bytes2HexStr(origMac)); logger.info("开始进行MAC校验"); // POS不上送终端号,使用商户号作为密钥管理号 boolean result = encryptionService.checkMAC(origMac, macData, bm.getShopCode()); if (result) logger.info("MAC校验成功!"); else logger.info("MAC校验失败!"); return result; }