protected override void Packet() { if (SendPackage.ExistBit(55)) { byte[] bSend55 = SendPackage.GetArrayData(55); byte[] field55 = GetICAutoField55(bSend55, bSend55.Length);//IC卡55域有变动 SendPackage.SetArrayData(55, field55, 0, field55.Length); } PackReverse(Reason); }
private TransResult Pack8583() { var ret = TransResult.E_PACKET_FAIL; try { var SendBytes = new byte[0]; PackFix(); Packet(); if (SendPackage.IsNull()) { return(TransResult.E_INVALID); } var MAC = new byte[8]; //计算mac并将mac打包进去 if (NeedCalcMac()) { byte[] macKey = null; if (EnType == EncryptType.Hardware) { macKey = KeyManager.GetEnMacKey(mSectionName); } else { macKey = KeyManager.GetDeMacKey(mSectionName); } if (macKey == null) { throw new Exception("尚未设置MAC密钥"); } if ((DType == DesType.Des && macKey.Length == 16) || (DType == DesType.TripleDes && macKey.Length == 8)) { throw new Exception("MAC密钥长度不符合DES算法"); } var macField = 64; if (SendPackage.ExistBit(1)) { macField = 128; } SendPackage.SetArrayData(macField, new byte[8]); var tmp = SendPackage.GetSendBuffer(); var macBytes = new byte[tmp.Length - 8]; Array.Copy(tmp, macBytes, macBytes.Length); if (CalcMacByMackey(macBytes, MAC)) { SendPackage.SetArrayData(macField, MAC); } else { SendPackage.ClearBitAndValue(macField); throw new Exception("计算MAC失败"); } } SendBytes = SendPackage.GetSendBuffer(); if (SendBytes.Length <= 0) { return(ret); } var head_8583 = PackBytesAtFront(SendBytes.Length); headLen_8583 = head_8583.Length; #region Json复合8583包打包(子类中重写PackSendObject())方法 SendObject = PackSendObject(); string strJson = ""; BaseCommunicateSendJson send = SendObject as BaseCommunicateSendJson; if (send != null) //对象必须是继承基类型的才采用json发送 { send.rowCount = send.DS.Length; strJson = JsonConvert.SerializeObject(SendObject); } #endregion //if (!string.IsNullOrEmpty(strJson)) //{ byte[] json = Encoding.Default.GetBytes(strJson); //} var sendLen_all = SendBytes.Length + head_8583.Length + json.Length; var t_head = PackHead(sendLen_all);//自定义报文头 headLen = t_head.Length; sendLen_all += headLen; sendstr_all = new byte[sendLen_all]; Array.Copy(t_head, sendstr_all, t_head.Length); Array.Copy(head_8583, 0, sendstr_all, headLen, head_8583.Length); Array.Copy(SendBytes, 0, sendstr_all, head_8583.Length + headLen, SendBytes.Length); if (!string.IsNullOrEmpty(strJson)) { Array.Copy(json, 0, sendstr_all, head_8583.Length + headLen + SendBytes.Length, json.Length); } ret = TransResult.E_SUCC; } catch (Exception ex) { Log.Error(GetType().Name, ex); } return(ret); }
//protected override void Packet() //{ // if (SendPackage.ExistBit(55)) // { // byte[] bSend55 = SendPackage.GetArrayData(55); // byte[] field55 = GetICAutoField55(bSend55, bSend55.Length);//IC卡55域有变动 // SendPackage.SetArrayData(55, field55, 0, field55.Length); // } // PackReverse(Reason); //} protected override void Packet() { bool bIsIC = false; if (CommonData.UserCardType == UserBankCardType.ICCard || CommonData.UserCardType == UserBankCardType.IcMagCard) { bIsIC = true; } SendPackage.SetString(0, "0400"); if (!string.IsNullOrEmpty(CommonData.BankCardNum) && CommonData.BankCardNum.Length != 0) { SendPackage.SetString(2, CommonData.BankCardNum); } SendPackage.SetString(3, "190000"); SendPackage.SetString(4, Utility.AmountToString(CommonData.Amount.ToString())); //PayEntity.PayTraceNo = GetTraceNo(); //SendPackage.SetString(11, PayEntity.PayTraceNo); if (!string.IsNullOrEmpty(CommonData.BankCardExpDate) && CommonData.BankCardExpDate.Length != 0)//卡有效期 { SendPackage.SetString(14, CommonData.BankCardExpDate); } if (bIsIC)//22 { SendPackage.SetString(22, "051"); } else { SendPackage.SetString(22, "021"); } //if (!string.IsNullOrEmpty(CommonData.BankCardSeqNum) && CommonData.BankCardSeqNum.Length != 0)//卡序列号 //{ // SendPackage.SetString(23, CommonData.BankCardSeqNum); //} SendPackage.SetString(25, "81"); //服务点条件代码 //SendPackage.SetString(26, "06"); if (!string.IsNullOrEmpty(CommonData.Track2) && CommonData.Track2.Length != 0) { SendPackage.SetString(35, CommonData.Track2.Replace('=', 'D')); } if (!string.IsNullOrEmpty(CommonData.Track3) && CommonData.Track3.Length != 0) { SendPackage.SetString(36, CommonData.Track3.Replace('=', 'D')); } SendPackage.SetString(41, GetTerminalNo()); SendPackage.SetString(42, GetMerchantNo()); SendPackage.SetArrayData(48, PacketField48()); SendPackage.SetString(49, "156"); if (SendPackage.ExistBit(55)) { byte[] bSend55 = SendPackage.GetArrayData(55); byte[] field55 = GetICAutoField55(bSend55, bSend55.Length);//IC卡55域有变动 SendPackage.SetArrayData(55, field55, 0, field55.Length); } PackReverse(Reason); //SendPackage.SetArrayData(52, Utility.str2Bcd(CommonData.BankPassWord)); //switch (DType) //{ // case DesType.Des: // SendPackage.SetString(53, "2000000000000000"); // break; // case DesType.TripleDes: // SendPackage.SetString(53, "2600000000000000"); // break; //} ////55 //if (bIsIC && PayEntity.SendField55 != null && PayEntity.SendField55.Length != 0) //{ // SendPackage.SetArrayData(55, PayEntity.SendField55); //} //if (bIsIC) // SendPackage.SetString(60, "22" + GetBatchNo() + "00050"); //else // SendPackage.SetString(60, "22" + GetBatchNo()); ////创建冲正文件 98 96 06 //CReverse_PowerPay cr = new CReverse_PowerPay(this); //cr.CreateReverseFile("98"); }
/// <summary> /// 重写通信函数 /// </summary> /// <returns></returns> internal override TransResult transact() { int headLen; TransResult ret = TransResult.E_SEND_FAIL; Socket socket = null; if (RealEnv) { if (!GPRSConnect()) { return(ret); } IPAddress ip = IPAddress.Parse(serverIP); IPEndPoint ipe = new IPEndPoint(ip, serverPort); //把ip和端口转化为IPEndPoint实例 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.SendTimeout = sendTimeOut * 1000; socket.ReceiveTimeout = recvTimeOut * 1000; socket.Connect(ipe); } catch (Exception err) { Log.Error(this.GetType().Name, err); return(ret); } } try { ret = TransResult.E_PACKET_FAIL; byte[] SendBytes = new byte[0]; PackFix(); Packet(); if (SendPackage.IsNull()) { return(TransResult.E_INVALID); } byte[] MAC = new byte[8]; //计算mac并将mac打包进去 if (NeedCalcMac()) { byte[] macKey = null; //if (EnType == EncryptType.Hardware) // macKey = KeyManager.GetEnMacKey(mSectionName); //else macKey = KeyManager.GetDeMacKey(mSectionName); if (macKey == null) { throw new Exception("尚未设置MAC密钥"); } if ((DType == DesType.Des && macKey.Length == 16) || (DType == DesType.TripleDes && macKey.Length == 8)) { throw new Exception("MAC密钥长度不符合DES算法"); } int macField = 64; if (SendPackage.ExistBit(1)) { macField = 128; } SendPackage.SetArrayData(macField, new byte[8]); byte[] tmp = SendPackage.GetSendBuffer(); byte[] macBytes = new byte[tmp.Length - 8]; Array.Copy(tmp, macBytes, macBytes.Length); //Log.Debug(Utility.bcd2str(macBytes, macBytes.Length)); if (CalcMac(macBytes, macKey, ref MAC)) { SendPackage.SetArrayData(macField, MAC); } else { SendPackage.ClearBitAndValue(macField); throw new Exception("计算MAC失败"); } } SendBytes = SendPackage.GetSendBuffer(); if (SendBytes.Length <= 0) { return(ret); } byte[] head = PackBytesAtFront(SendBytes.Length); headLen = head.Length; int sendLen_all = SendBytes.Length + head.Length; byte[] sendstr_all = new byte[sendLen_all]; Array.Copy(head, sendstr_all, head.Length); Array.Copy(SendBytes, 0, sendstr_all, head.Length, SendBytes.Length); //记录原始发送日志 //CLog.LogPackage(sendstr_all, SendPackage, CLog.LogType.Send); CLog.Info(CLog.GetLog(sendstr_all, SendPackage, this, CLog.LogType.Send)); ret = TransResult.E_SEND_FAIL; if (RealEnv) { int sendLen = 0; sendLen = socket.Send(sendstr_all, sendLen_all, 0); if (sendLen <= 0) { socket.Close(); return(ret); } } //从服务器端接受返回信息 ret = TransResult.E_RECV_FAIL; int recvLen = 0; if (RealEnv) { sRecvBuffer.Initialize(); recvLen = socket.Receive(sRecvBuffer, sRecvBuffer.Length, 0); if (recvLen <= 0) { socket.Close(); return(ret); } byte[] RecvBytes = new byte[recvLen - headLen]; Array.Copy(sRecvBuffer, headLen, RecvBytes, 0, recvLen - headLen); byte[] headBytes = new byte[headLen]; Array.Copy(sRecvBuffer, headBytes, headLen); //CLog.Info(RecvPackage); //解包 ret = TransResult.E_UNPACKET_FAIL; FrontBytes = headBytes; HandleFrontBytes(headBytes);//根据报文头来判断是否要下载密钥 RecvPackage.ParseBuffer(RecvBytes, SendPackage.ExistValue(0)); //CLog.Info(RecvBytes); //记录原始接收日志 byte[] logRecv = new byte[recvLen]; Array.Copy(sRecvBuffer, logRecv, recvLen); //CLog.LogPackage(logRecv, RecvPackage, CLog.LogType.Recv); CLog.Info(CLog.GetLog(logRecv, RecvPackage, this, CLog.LogType.Recv)); bool nRet = UnPackFix(); if (!mInvokeSetResult) { throw new Exception("should invoke SetRespInfo() in UnPackFix()"); } mInvokeSetResult = false; if (nRet) { ret = TransResult.E_SUCC; } else { ret = TransResult.E_HOST_FAIL; } } else { ret = TransResult.E_SUCC; } } catch (System.Exception ex) { Log.Error(this.GetType().Name, ex); } finally { if (socket != null && socket.Connected) { socket.Close(); } } return(ret); }