void CardConnectedCallback(SCardChannel cardChannel)
        {
            /* The CardConnected function is called as a delegate (callback) by the SCardReader object */
            /* within its backgroung thread. Therefore it is not allowed to use the UI objects.        */

            if (!GoogleVasLicense.ReadDeviceId(cardChannel))
            {
                Logger.Error("Read device ID error");
                ShowResult("Not a SpringCard device?", null, true);
                return;
            }

            if (!GoogleVasLicense.LoadCollectorId(config.CollectorId_4))
            {
                Logger.Error("Wrong Collector ID");
                ShowResult("Wrong Collector ID?", null, true);
                return;
            }

            if (!GoogleVasLicense.Allowed(out string msg))
            {
                Logger.Error("Not allowed to execute");
                ShowResult(string.Format("Not allowed to execute ({0})", msg), null, true);
                return;
            }

            Logger.Trace("Card connected - Trying to run the transaction");

            GoogleVasTerminal terminal = new GoogleVasTerminal(config);

            if (terminal.DoTransaction(cardChannel, out GoogleVasData data, out GoogleVasError error))
            {
                ShowResult("Message is OK", data, true);
            }
Exemple #2
0
        int Run(string[] args)
        {
            if (!ParseArgs(args))
            {
                return(1);
            }

            Logger.Debug("Loading the list of PC/SC readers");

            string[] ReaderNames = (new SCardReaderList()).Readers;

            if (ListReaders)
            {
                Console.WriteLine(string.Format("{0} PC/SC Reader(s) found", ReaderNames.Length));
                for (int i = 0; i < ReaderNames.Length; i++)
                {
                    Console.WriteLine(string.Format("{0}: {1}", i, ReaderNames[i]));
                }
                return(0);
            }


            if (!GoogleVasLicense.AutoLoad())
            {
                Logger.Info("No license file");
            }

            GoogleVasConfig config;

            if (ConfigFileName != null)
            {
                config = GoogleVasConfig.LoadFromJsonFile(ConfigFileName);
            }
            else
            {
                config = GoogleVasConfig.GoogleDemo();
            }

            if (ExportedFileName != null)
            {
                GoogleVasConfig.SaveToSpringCoreConfigFile(config, ExportedFileName);
                Logger.Info("Export done, treminate program");
                return(0);
            }

            if (ReaderName == null)
            {
                if (ReaderIndex < 0)
                {
                    ReaderIndex = 0;
                }

                Logger.Debug("Selecting the PC/SC reader at index {0}", ReaderIndex);

                if ((ReaderIndex >= ReaderNames.Length))
                {
                    Console.WriteLine("No PC/SC Reader at index {0}", ReaderIndex);
                    Console.WriteLine("Use " + ProgName + " --list-readers");
                    return(1);
                }

                ReaderName = ReaderNames[ReaderIndex];
            }

            Logger.Debug("Using PC/SC reader {0}", ReaderName);

            Logger.Debug("Opening the PC/SC reader");

            SCardReader reader = new SCardReader(ReaderName);

            Logger.Debug("Expecting to find a 'smartcard'");

            SCardChannel channel = new SCardChannel(reader);

            if (!channel.CardPresent)
            {
                ConsoleError("No NFC card or smartphone on the reader");
                return(1);
            }

            Logger.Debug("Connecting to the 'smartcard'");

            if (!channel.Connect())
            {
                ConsoleError("Failed to open the communication with the NFC card or smartphone");
                return(1);
            }

            if (!GoogleVasLicense.ReadDeviceId(channel))
            {
                ConsoleError("Not a SpringCard device?");
                return(1);
            }

            if (!GoogleVasLicense.LoadCollectorId(config.CollectorId_4))
            {
                ConsoleError("Wrong Collector ID?");
                return(1);
            }

            GoogleVasError error;

            GoogleVasTerminal terminal = new GoogleVasTerminal(config);

            if (terminal.DoTransaction(channel, out GoogleVasData data, out error))
            {
                Logger.Debug($"Message is OK, transaction done in {terminal.TransactionDuration} ms");
            }
        void CardConnectedCallback(SCardChannel cardChannel)
        {
            /* The CardConnected function is called as a delegate (callback) by the SCardReader object */
            /* within its backgroung thread. Therefore it is not allowed to use the UI objects.        */


            /* Apple exhange */

            if (!AppleVasLicense.ReadDeviceId(cardChannel))
            {
                Logger.Error("Read device ID error");
                ShowResult("Not a SpringCard device?");
                return;
            }

            if (!AppleVasLicense.Allowed(out string msg2))
            {
                Logger.Error("Not allowed to execute");
                ShowResult(string.Format("Not allowed to execute ({0})", msg2));
                return;
            }

            AppleVasTerminal appleTerminal = new AppleVasTerminal();

            foreach (AppleVasConfig merchConfig in appleConfig.Merchants)
            {
                appleTerminal.AddConfig(merchConfig);
            }

            if (appleTerminal.DoTransaction(cardChannel, out AppleVasData data2, out AppleVasError error2, out RAPDU selectOseresponseApple))
            {
                ShowResult("Apple Wallet\n" + data2.Text);
                Task.Delay(5000).ContinueWith(t => resetUiEvent());
                return;
            }

            if (!GoogleVasLicense.ReadDeviceId(cardChannel))
            {
                Logger.Error("Read device ID error");
                ShowResult("Not a SpringCard device?");
                return;
            }

            if (!GoogleVasLicense.LoadCollectorId(googleConfig.CollectorId_4))
            {
                Logger.Error("Wrong Collector ID");
                ShowResult("Wrong Collector ID?");
                return;
            }

            if (!GoogleVasLicense.Allowed(out string msg1))
            {
                Logger.Error("Not allowed to execute");
                ShowResult(string.Format("Not allowed to execute ({0})", msg1));
                return;
            }

            Logger.Trace("Card connected - Trying to run the transaction");

            /* Google exhange */

            GoogleVasTerminal googleTerminal = new GoogleVasTerminal(googleConfig);

            /* Provide selectOseResponseApple to speed up the transaction */
            if (googleTerminal.DoTransaction(cardChannel, out GoogleVasData data1, out GoogleVasError error1, selectOseresponseApple))
            {
                /* Convert JSON to SpringPass data with regex */
                Regex rx = new Regex(@"[0-9a-zA-Z]+\|[a-z0-9.-_]+@[a-z.]+", RegexOptions.Compiled | RegexOptions.IgnoreCase);

                MatchCollection matches = rx.Matches(data1.GetJsonString());

                if (matches.Count >= 1)
                {
                    ShowResult("Google Smart Tap\n" + matches[0].Value);
                    Task.Delay(5000).ContinueWith(t => resetUiEvent());
                }
                else
                {
                    ShowResult("Read succeded but no SpringPass data found");
                }
                return;
            }
        }