Example #1
0
        /// <summary>
        /// EMV卡片交互
        /// </summary>
        /// <param name="field55"></param>
        /// <param name="AuthNo"></param>
        /// <returns></returns>
        public int EMVTransEnd(byte[] field55, string AuthNo)
        {
            int ret = 0;

            try
            {
                string temp = PubFunc.ByteArrayToHexString(field55, field55.Length);
                ret = pboc.App_EMVOnlineDataProcess(0, Encoding.Default.GetBytes(temp), temp.Length,
                                                    PubFunc.HexStringToByteArray(AuthNo), AuthNo.Length);
                if (ret == 0)
                {
                    byte[] field551   = new byte[1024];
                    int    field55Len = 0;
                    pboc.App_EMVGetField55(field551, ref field55Len);
                    EMVInfo.EndField55 = new byte[field55Len];
                    Array.Copy(field551, EMVInfo.EndField55, field55Len);
                }
            }
            catch (System.Exception ex)
            {
                Log.Error("App_EMVOnlineDataProcess Error!", ex);
                ret = -1;
            }
            finally
            {
                CardReader.CardPowerDown();
            }
            return(ret);
        }
Example #2
0
        public bool WritePowerCard(PowerCardInfo cardInfo)
        {
            bool bRet = false;

            try
            {
                //Test
                //return true;
                string powerCardNo = "";
                string temp        = "";
                if (cardInfo == null)
                {
                    Log.Warn("PowerCardInfo is null");
                    return(false);
                }
                cardInfo.ErrorMsg = "请确认您的购电卡是否正确插入,谢谢";

                //if (!GetCardNo(ref powerCardNo))
                //    return false;

                //if (!GetCardType(ref temp))
                //    return false;

                //if (!SelectDirectory(ref temp))
                //    return false;

                if (!Authenticate(cardInfo))
                {
                    Log.Debug("写卡权限验证失败");
                    cardInfo.ErrorMsg = "写卡权限验证失败";
                    return(false);
                }

                if (!SetFileInfo(cardInfo))
                {
                    Log.Debug("写文件失败");
                    cardInfo.ErrorMsg = "写文件失败";
                    return(false);
                }
                bRet = true;
            }
            catch (Exception ex)
            {
                Log.Error("WritePowerCard:" + ex);
            }
            finally
            {
                CardReader.CardPowerDown();
            }
            return(bRet);
        }
        /// <summary>
        /// EMV卡片交互
        /// </summary>
        /// <param name="field55"></param>
        /// <param name="AuthNo"></param>
        /// <returns></returns>
        public int EMVTransEnd(byte[] field55, string AuthNo)
        {
            int ret = 0;

            try
            {
                string temp = PubFunc.ByteArrayToHexString(field55, field55.Length);
                ret = pboc.App_EMVOnlineDataProcess(0, Encoding.Default.GetBytes(temp), temp.Length,
                                                    PubFunc.HexStringToByteArray(AuthNo), AuthNo.Length);
            }
            catch (System.Exception ex)
            {
                Log.Error("App_EMVOnlineDataProcess Error!", ex);
                ret = -1;
            }
            finally
            {
                CardReader.CardPowerDown();
            }
            return(ret);
        }
Example #4
0
        /// <summary>
        /// 磁条卡优先,兼容IC卡
        /// </summary>
        /// <returns></returns>
        protected Result DefaultRead3()
        {
            Result ret = DefaultRead();

            if ((ret == Result.Fail) && mIsUseICCard)
            {
                //读卡失败,如果有启动IC,则进入IC卡读卡模式
                ReportSync("none");
                EMVTransProcess emv   = new EMVTransProcess();
                int             state = emv.EMVTransInit(0, EMVTransProcess.PbocTransType.PURCHASE);
                CardReader.CardPowerDown();
                if (state == 0)
                {
                    BankCardNum = emv.EMVInfo.CardNum;
                    Track2      = emv.EMVInfo.Track2;
                    Log.Debug("IC Card In");
                    return(Result.Success);
                }
            }

            return(ret);
        }
        protected override void HandleResult(Result result)
        {
            PowerEntity entity = GetBusinessEntity() as PowerEntity;

            //Test
            //result = Result.Fail;
            switch (result)
            {
            case Result.Success:
            {
                ReportSync("PowerReadCard");
                if (!new CPowerCard().ReadPowerCard(entity.PowerCardData))
                {
                    CardReader.CardPowerDown();
                    goto case Result.Fail;
                }
                CardReader.CardPowerDown();
                StartActivity("电力支付补写卡交易");
            }
            break;

            case Result.HardwareError:
                ShowMessageAndGotoMain("读卡器故障");
                break;

            case Result.Fail:
                CardReader.CardOut();
                StartActivity("电力支付读电卡错误");
                break;

            case Result.Cancel:
                StartActivity("主界面");
                break;

            case Result.TimeOut:
                StartActivity("主界面");
                break;
            }
        }
        /// <summary>
        /// 交易完后与IC进行交互
        /// </summary>
        /// <param name="sendField55"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        private bool ICTransFunc(ref int state)
        {
            bool bRet = false;

            try
            {
                CR.CreateReverseFile();//建立冲正文件
                string temp = PubFunc.ByteArrayToHexString(EMVInfo.RecvField55, EMVInfo.RecvField55.Length);
                state = pboc.App_EMVOnlineDataProcess(0, Encoding.Default.GetBytes(temp),
                                                      temp.Length, PubFunc.HexStringToByteArray(EMVInfo.RecvField38),
                                                      EMVInfo.RecvField38.Length);
                CR.ClearReverseFile();//清除冲正文件
                bRet = true;
            }
            catch (System.Exception ex)
            {
                CR.DoReverseFile();//发起冲正
                CardReader.CardPowerDown();
                Log.Error("App_EMVOnlineDataProcess Error!", ex);
            }

            return(bRet);
        }
Example #7
0
        /// <summary>
        /// 磁条卡优先,兼容IC卡
        /// </summary>
        /// <returns></returns>
        protected Result DefaultRead3()
        {
            Result ret = DefaultRead();

            if ((ret == Result.Fail) && GlobalAppData.GetInstance().UseICCard)
            {
                //读卡失败,如果有启动IC,则进入IC卡读卡模式
                ReportSync("none");
                CardReader.CardType(0, 0);
                EMVTransProcess emv   = new EMVTransProcess();
                int             state = emv.EMVTransInit(0, EMVTransProcess.PbocTransType.PURCHASE);
                CardReader.CardPowerDown();
                if (state == 0)
                {
                    SaveBoolean("UseICCard", true);//交易变量
                    BankCardNum = emv.EMVInfo.CardNum;
                    Track2      = emv.EMVInfo.Track2;
                    Log.Debug("IC Card In");
                    return(Result.Success);
                }
            }

            return(ret);
        }
Example #8
0
        /// <summary>
        /// IC卡优先,兼容磁条卡,不降级处理
        /// </summary>
        /// <returns></returns>
        protected Result DefaultRead4()
        {
            bool   isIcCard = true;
            int    nCardType = 0;
            string trk1 = "", trk2 = "", trk3 = "";

            CardReader.Status ret = CardReader.InsertCard(ref trk1, ref trk2, ref trk3);
            if (ret == CardReader.Status.CARD_SUCC)
            {
                if (CardReader.IsUse)
                {
                    CommonData.BIsCardIn = true;//有卡进入
                }
                //读卡模式隐藏按钮
                ReportSync("none");
                //Log.Debug("Track1:" + trk1);
                //Log.Debug("Track2 : " + trk2);
                //Log.Debug("Track3 : " + trk3);
                string CardNumber = Utility.GetCardNumber(trk2, trk3);
                if (CardNumber.Trim().Length > 0)
                {
                    Log.Info("MS Card Deal");
                    Track1      = trk1;
                    Track2      = trk2;
                    Track3      = trk3;
                    BankCardNum = CardNumber;
                    ExpDate     = Utility.GetExpDate(trk2, trk3);
                    isIcCard    = Utility.CheckIcCardFlag(trk2);
                    Log.Info("isIcCard=" + isIcCard.ToString());
                    //isIcCard = true;//可能存在二磁道 ic卡判断错误的情况 所以IC卡都需要验证以防 45错误
                    nCardType = 1;
                }

                if (!CardReader.IsUse)
                {
                    isIcCard = false;
                }

                if (isIcCard && mIsUseICCard)
                {
                    //CardReader.CardType(0, 0);//防止读卡器问题类型问题
                    EMVTransProcess emv   = new EMVTransProcess();
                    int             state = emv.EMVTransInit(0, EMVTransProcess.PbocTransType.PURCHASE);
                    CardReader.CardPowerDown();
                    if (state == 0)
                    {
                        Log.Info("IC Card Deal");
                        BankCardNum = emv.EMVInfo.CardNum;
                        Track2      = emv.EMVInfo.Track2;
                        ExpDate     = emv.EMVInfo.CardExpDate;
                        CardSeqNum  = emv.EMVInfo.CardSeqNum;
                        nCardType  += 2;
                    }
                    //else
                    //    isIcCard = false;//防止纯磁条卡无法交易
                }

                BankCardType = (UserBankCardType)nCardType;

                if (mIsUseICCard && isIcCard && nCardType < 2)//开启IC卡,不降级处理
                {
                    return(Result.Fail);
                }

                if (BankCardType == UserBankCardType.None)
                {
                    return(Result.Fail);
                }
            }
            else if (ret == CardReader.Status.CARD_WAIT)
            {
                return(Result.Again);
            }
            else
            {
                Log.Error("ReaderOnce:" + ret.ToString());
                return(Result.HardwareError);
            }

            return(Result.Success);
        }
Example #9
0
        /// <summary>
        /// EMV交易,获取交易55域
        /// </summary>
        /// <returns></returns>
        public int EMVTransDeal()
        {
            int state = 0;

            try
            {
                #region 银行卡判定

                //3 App_EMVTermRiskManageProcessRestrict
                state = pboc.App_EMVTermRiskManageProcessRestrict();
                if (state != 0)
                {
                    CardReader.CardPowerDown();
                    Log.Warn("App_EMVTermRiskManageProcessRestrict:银行卡无效 state=" + state);
                    return(-1);
                }

                //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("App_EMVActionAnalysis:系统故障 state=" + state);
                    return(-1);
                }
                else if (state == 0)
                {
                    CardReader.CardPowerDown();
                    Log.Warn("App_EMVActionAnalysis:脱机交易成功 state=" + state);
                    return(-1);
                }
                else if (state == 1)
                {
                    CardReader.CardPowerDown();
                    Log.Warn("App_EMVActionAnalysis:卡片拒绝交易 state=" + state);
                    return(-1);
                }
                else if (state == 2)
                {
                    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);
                }
                else
                {
                    CardReader.CardPowerDown();
                    Log.Warn("App_EMVCardHolderValidate:交易失败 state=" + state);
                    return(-1);
                }
                #endregion
            }
            catch (System.Exception ex)
            {
                CardReader.CardPowerDown();
                Log.Error("EMV交易失败", ex);
                return(-1);
            }

            return(0);
        }
Example #10
0
        /// <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);
        }
Example #11
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();//清除冲正文件
            }
        }