/// <summary> /// 上电获取卡片序列号 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool GetCardNo(ref string info) { bool bRet = false; if (!CardReader.IsUse) { info = "10110800000F82CA"; return(true); } try { byte[] bAnswer = new byte[512]; int nLen = 0; int nChip = 0; CardReader.Status status = CardReader.CardPowerUp(bAnswer, ref nLen, ref nChip); Log.Error("GetCardResult:" + Enum.GetName(typeof(CardReader.Status), status)); if (status == CardReader.Status.CARD_SUCC) { bRet = true; info = PubFunc.ByteArrayToHexString(bAnswer, nLen); Log.Debug("GetCardNO:" + info); info = info.Substring(info.Length - 16); } } catch (Exception ex) { Log.Error("GetCardNo:" + ex); } return(bRet); }
/// <summary> /// EMV初始化 /// </summary> /// <param name="dInAmount"></param> /// <param name="pbocType"></param> /// <returns></returns> public int EMVTransInit(double dInAmount, PbocTransType pbocType) { int state = 0; byte[] answer = new byte[128]; int pnLen = 0; int pnChipProtocol = 0; EMVInfo = new EMVData(); long hand = 0; try { #region 卡片上电初始化 if (PayType == 1) { hand = R80.GetHandle(); R80.Status cRet = R80.PowerUp(10, ""); if (cRet != R80.Status.EM_MIFARE_SUCC) { Log.Warn("上电失败"); return(-1); } } else { hand = CardReader.GetHandle(); CardReader.Status cRet = CardReader.CardPowerUp(answer, ref pnLen, ref pnChipProtocol); if (cRet != CardReader.Status.CARD_SUCC) { CardReader.CardPowerDown(); Log.Warn("上电失败"); return(-1); } } state = pboc.App_EMVLInit(PayType, hand); if (state != 0) { Log.Warn("卡片初始化失败"); return(-1); } #endregion #region 获取卡片应用 byte[] appList = new byte[256]; int nListNum = 0; pboc.App_EMVL2SelectApp(pnChipProtocol, appList, ref nListNum); string[] strEmvList = System.Text.Encoding.Default.GetString(appList).Trim().Replace("\0", "").Split('|'); if (nListNum < 1) { CardReader.CardPowerDown(); Log.Warn("卡片无可用的应用"); return(-1); } byte[] inTrace = Encoding.Default.GetBytes("000000"); byte[] inDay = Encoding.Default.GetBytes(DateTime.Now.ToString("yyMMdd")); byte[] inTime = Encoding.Default.GetBytes(DateTime.Now.ToString("HHmmss")); byte[] inAmount = Encoding.Default.GetBytes(Utility.AmountToString(dInAmount.ToString()));; byte[] inOtherAmount = Encoding.Default.GetBytes("000000000000"); int iAppId = 0; bool bEmvOk = false; foreach (string tempEmv in strEmvList) { //A000000333010101|银联 //if (!String.IsNullOrEmpty(tempEmv) && tempEmv.StartsWith("A000000333")) //{ // state = pboc.App_EMVStartEmvApp(iAppId, pnChipProtocol, (int)PbocTransType.PURCHASE, inTrace, inDay, inTime, inAmount, inOtherAmount); // if (state == 0) // { // bEmvOk = true; // break; // } //} state = pboc.App_EMVStartEmvApp(iAppId, pnChipProtocol, (int)pbocType, inTrace, inDay, inTime, inAmount, inOtherAmount); if (state == 0) { bEmvOk = true; break; } iAppId++; } if (!bEmvOk) { CardReader.CardPowerDown(); Log.Warn("没有支持的应用"); return(-1); } #endregion #region 获取卡片卡号信息 byte[] cardNo = new byte[21]; int cardNoLen = 0; byte[] track2 = new byte[38]; int track2Len = 0; byte[] expData = new byte[5]; int expLen = 0; byte[] cardSeqNum = new byte[2]; pboc.App_EMVGetCardNo(cardNo, ref cardNoLen, track2, ref track2Len, expData, ref expLen, cardSeqNum); EMVInfo.CardNum = Encoding.Default.GetString(cardNo).Trim('\0'); EMVInfo.Track2 = Encoding.Default.GetString(track2).Trim('\0'); EMVInfo.CardSeqNum = Convert.ToString(cardSeqNum[0]).Trim('\0'); EMVInfo.CardExpDate = Encoding.Default.GetString(expData).Trim('\0'); if (String.IsNullOrEmpty(EMVInfo.CardNum)) { CardReader.CardPowerDown(); Log.Warn("IC:读卡号失败"); return(-1); } else { EMVInfo.CardNum = EMVInfo.CardNum.Replace('\0', ' ').Trim(); } #endregion } catch (Exception ex) { CardReader.CardPowerDown(); Log.Error("IC:初始化异常", ex); return(-1); } return(0); }
public void EMVTransProcess(double dInAmount, PbocTransType pbocType) { int state = 0; byte[] answer = new byte[128]; int pnLen = 0; int pnChipProtocol = 0; EMVInfo = new EMVData(); try { if (BReadBankCard) { goto Init; } #region 类未注册 if (Trans == null || CR == null) { Log.Warn("交易类或冲正类没有注册"); return; } #endregion Init: #region 卡片上电初始化 long hand = CardReader.GetHandle(); CardReader.Status cRet = CardReader.CardPowerUp(answer, ref pnLen, ref pnChipProtocol); if (cRet != CardReader.Status.CARD_SUCC) { CardReader.CardPowerDown(); Log.Warn("上电失败"); return; } state = pboc.App_EMVLInit(0, hand); if (state != 0) { Log.Warn("卡片初始化失败"); return; } #endregion #region 获取卡片应用 byte[] appList = new byte[256]; int nListNum = 0; pboc.App_EMVL2SelectApp(pnChipProtocol, appList, ref nListNum); string[] strEmvList = System.Text.Encoding.Default.GetString(appList).Trim().Replace("\0", "").Split('|'); if (nListNum < 1) { CardReader.CardPowerDown(); Log.Warn("卡片无可用的应用"); return; } byte[] inTrace = Encoding.Default.GetBytes("000000"); byte[] inDay = Encoding.Default.GetBytes(DateTime.Now.ToString("yyMMdd")); byte[] inTime = Encoding.Default.GetBytes(DateTime.Now.ToString("HHmmss")); byte[] inAmount = Encoding.Default.GetBytes(Utility.AmountToString(dInAmount.ToString()));; byte[] inOtherAmount = Encoding.Default.GetBytes("000000000000"); int iAppId = 0; bool bEmvOk = false; foreach (string tempEmv in strEmvList) { //A000000333010101|银联 //if (!String.IsNullOrEmpty(tempEmv) && tempEmv.StartsWith("A000000333")) //{ // state = pboc.App_EMVStartEmvApp(iAppId, pnChipProtocol, (int)PbocTransType.PURCHASE, inTrace, inDay, inTime, inAmount, inOtherAmount); // if (state == 0) // { // bEmvOk = true; // break; // } //} state = pboc.App_EMVStartEmvApp(iAppId, pnChipProtocol, (int)pbocType, inTrace, inDay, inTime, inAmount, inOtherAmount); if (state == 0) { bEmvOk = true; break; } iAppId++; } if (!bEmvOk) { CardReader.CardPowerDown(); Log.Warn("没有支持的应用"); return; } #endregion #region 获取卡片卡号信息 byte[] cardNo = new byte[21]; int cardNoLen = 0; byte[] track2 = new byte[38]; int track2Len = 0; byte[] expData = new byte[5]; int expLen = 0; byte[] cardSeqNum = new byte[2]; pboc.App_EMVGetCardNo(cardNo, ref cardNoLen, track2, ref track2Len, expData, ref expLen, cardSeqNum); EMVInfo.CardNum = Encoding.Default.GetString(cardNo); EMVInfo.Track2 = Encoding.Default.GetString(track2); EMVInfo.CardSeqNum = Convert.ToString(cardSeqNum[0]); EMVInfo.CardExpDate = Encoding.Default.GetString(expData); #endregion #region 银行卡判定 //3 App_EMVTermRiskManageProcessRestrict state = pboc.App_EMVTermRiskManageProcessRestrict(); if (state != 0) { CardReader.CardPowerDown(); Log.Warn("银行卡无效"); return; } //4 App_EMVCardHolderValidate int cTime = 0; state = pboc.App_EMVCardHolderValidate(ref cTime); if (state != 0) { state = pboc.App_EMVContinueCardHolderValidate(1, ref cTime);//无论state为何值,直接提示联机pin以成功输入。内核对联机PIN的处理,只需要告诉是否完成PIN即可 } //5 App_EMVActionAnalysis state = pboc.App_EMVActionAnalysis(); if (state == -1) { CardReader.CardPowerDown(); Log.Warn("系统故障"); return; } else if (state == 0) { CardReader.CardPowerDown(); Log.Warn("脱机交易成功"); return; } else if (state == 1) { CardReader.CardPowerDown(); Log.Warn("卡片拒绝交易"); return; } #endregion #region 联机交易 else if (state == 2) { if (BReadBankCard) { EmvRet = TransResult.E_SUCC; return; } byte[] field55 = new byte[512]; int field55Len = 0; //6 App_EMVGetField55 pboc.App_EMVGetField55(field55, ref field55Len); EMVInfo.SendField55 = new byte[field55Len]; Array.Copy(field55, EMVInfo.SendField55, field55Len); EMVInfo.AutoField55 = GetICAutoField55(field55, field55Len); EmvRet = Trans();//IS8583后台交易 if (EmvRet == TransResult.E_SUCC) { //交易完后与IC进行交互 if (!ICTransFunc(ref state)) { EmvRet = TransResult.E_SEND_FAIL; return; } //7 获取经过IC卡座处理的55域,用于脚本通知或者冲正使用 pboc.App_EMVGetField55(field55, ref field55Len); EMVInfo.AutoField55 = GetICAutoField55(field55, field55Len); CR.CreateReverseFile();//创建冲正文件 //if (state != 4) //{ // AppLog.Write("发起脚本通知state=" + state.ToString(), AppLog.LogMessageType.Info); // CTransScript tScript = new CTransScript(); // tScript.RevTransUp = CTrans.RevTransData; // tScript.SendData.Field11 = PubFunc.GetTerminalTraceNo(); // tScript.SendData.Field32 = CTrans.RecvData.Field32; // tScript.SendData.Field37 = CTrans.RecvData.Field37; // tScript.SendData.Field55 = GetField55Script(RecvField55);//scriptField55 // tScript.TransExecute(); //} if (state == 0) { //交易成功 if (EmvSuccess != null) { EmvSuccess(); } CR.ClearReverseFile();//清除冲正文件 } else if (state == 4) { Log.Warn("写卡失败,无脚本做冲正state=" + state.ToString()); CR.DoReverseFile(); } else { Log.Warn("写卡失败,脚本通知和冲正state=" + state.ToString()); CR.DoReverseFile(); } if (state != 0) { EmvRet = TransResult.E_SEND_FAIL; } } } #endregion } catch (System.Exception ex) { Log.Error("EMVTransProcess Error!", ex); CR.DoReverseFile(); } finally { CardReader.CardPowerDown(); CR.DoReverseFile(); CR.ClearReverseFile();//清除冲正文件 } }