Exemplo n.º 1
0
        private static void GetPairingCodeAndToken()
        {
            string newToken;

            SetFacade();
            var apiTokens = new ApiTokens();

            if (env == Env.Test)
            {
                apiTokens = appConfig.BitPayConfiguration.EnvConfig.Test.ApiTokens;
            }
            if (env == Env.Prod)
            {
                apiTokens = appConfig.BitPayConfiguration.EnvConfig.Prod.ApiTokens;
            }

            if (TokenCurrentlyExists(facade, apiTokens))
            {
                SetNotification(" A token for this Facade (" + facade + ") on this Environment (" + env +
                                ") already exists.");
                Console.Clear();
                DrawTitle();
                Console.WriteLine(" Would you want to replace it for a new one? [yes|no] (default: no)");
                Console.WriteLine();
                Console.Write(" > ");
                var answer = Console.ReadLine();
                while (answer.ToLower() != "yes" && answer.ToLower() != "no" && answer.ToLower() != "")
                {
                    answer = Console.ReadLine();
                }

                if (answer.ToLower() == "no")
                {
                    return;
                }
            }

            try
            {
                var bitpay = new BitPay(confFilePath);

                var pairingCodeObj = bitpay.RequestClientAuthorization(facade);

                pairingCode = pairingCodeObj.Result;

                newToken = bitpay.GetTokenByFacade(facade);
            }
            catch (Exception e)
            {
                SetNotification(
                    " An error occurred while generating a new token pair.\n Make sure you have created a/o selected a private key.\n Error Details: " +
                    e.Message);

                return;
            }

            switch (facade)
            {
            case Facade.PointOfSale:
                apiTokens.pos = newToken;
                break;

            case Facade.Merchant:
                apiTokens.merchant = newToken;
                break;

            case Facade.Payroll:
                apiTokens.payroll = newToken;
                break;
            }

            var envUrl = "";

            if (env == Env.Test)
            {
                appConfig.BitPayConfiguration.EnvConfig.Test.ApiTokens = apiTokens;
                envUrl = appConfig.BitPayConfiguration.EnvConfig.Test.ApiUrl;
            }

            if (env == Env.Prod)
            {
                appConfig.BitPayConfiguration.EnvConfig.Prod.ApiTokens = apiTokens;
                envUrl = appConfig.BitPayConfiguration.EnvConfig.Prod.ApiUrl;
            }

            GenerateConfFile(confFilePath);

            SetNotification(" New pairing code for " + facade + " facade: " + pairingCode + "\n Please, copy the above pairing code and approve on your BitPay Account at the following link:\n \"" +
                            envUrl + "dashboard/merchant/api-tokens\".\n Once this Pairing Code is approved, your .Net Client will be ready to work with the API.\n A new token for this pairing code has been added to the configuration file.");
        }
Exemplo n.º 2
0
        private static void GetPairingCodeAndToken()
        {
            string newToken = "";

            SetFacade();
            if (String.IsNullOrEmpty(facade))
            {
                return;
            }

            Console.Clear();
            DrawTitle();
            Console.WriteLine(" Would you like to request a token for the " + facade.ToUpperInvariant() +
                              " facade? [yes|no] (default: yes)");
            Console.WriteLine();
            Console.Write(" > ");
            var answer = Console.ReadLine();

            while (answer.ToLower() != "yes" && answer.ToLower() != "no" && answer.ToLower() != "")
            {
                answer = Console.ReadLine();
            }

            if (answer.ToLower() == "no")
            {
                GetPairingCodeAndToken();
                return;
            }

            if (facade == Facade.Payroll)
            {
                SetNotification(
                    " In order to get access to the Payroll facade, you need to contact Support at [email protected]",
                    3);
                Console.Clear();
                DrawTitle();
                Console.WriteLine(" Did you contact and receive confirmation yet? [yes|no] (default: yes)");
                Console.WriteLine(
                    " If 'no', a new file will be generated in the entered location with the given name.");
                Console.WriteLine();
                Console.Write(" > ");
                answer = Console.ReadLine();
                while (answer.ToLower() != "yes" && answer.ToLower() != "no" && answer.ToLower() != "")
                {
                    answer = Console.ReadLine();
                }

                if (answer.ToLower() == "no")
                {
                    GetPairingCodeAndToken();
                    return;
                }
            }

            var apiTokens = new ApiTokens();

            if (env == Env.Test)
            {
                apiTokens = appConfig.BitPayConfiguration.EnvConfig.Test.ApiTokens;
            }
            if (env == Env.Prod)
            {
                apiTokens = appConfig.BitPayConfiguration.EnvConfig.Prod.ApiTokens;
            }

            if (TokenCurrentlyExists(facade, apiTokens))
            {
                SetNotification(" A token for this Facade (" + facade + ") on this Environment (" + env +
                                ") already exists.", 3);
                Console.Clear();
                DrawTitle();
                Console.WriteLine(" Would you like to replace it for a new one? [yes|no] (default: no)");
                Console.WriteLine();
                Console.Write(" > ");
                answer = Console.ReadLine();
                while (answer.ToLower() != "yes" && answer.ToLower() != "no")
                {
                    answer = Console.ReadLine();
                }

                if (answer.ToLower() == "no" || answer.ToLower() == "")
                {
                    return;
                }
            }

            var retry = true;

            while (retry)
            {
                try
                {
                    var bitpay = new BitPay(confFilePath);

                    pairingCode = bitpay.RequestClientAuthorization(facade).Result;

                    newToken = bitpay.GetTokenByFacade(facade);
                }
                catch (Exception e)
                {
                    SetNotification(
                        " An error occurred while generating a new token pair.\n Make sure you have created a/o selected a private key.\n Error Details: " +
                        e.Message, 2);

                    GetPairingCodeAndToken();
                    return;
                }

                switch (facade)
                {
                case Facade.Merchant:
                    apiTokens.merchant = newToken;
                    break;

                case Facade.Payroll:
                    apiTokens.payroll = newToken;
                    break;
                }

                if (env == Env.Test)
                {
                    appConfig.BitPayConfiguration.EnvConfig.Test.ApiTokens = apiTokens;
                    envUrl = "https://test.bitpay.com/";
                }

                if (env == Env.Prod)
                {
                    appConfig.BitPayConfiguration.EnvConfig.Prod.ApiTokens = apiTokens;
                    envUrl = "https://bitpay.com/";
                }

                GenerateConfFile(confFilePath);

                SetNotification(" New pairing code for " + facade + " facade: " + pairingCode +
                                "\n Please, copy the above pairing code and approve on your BitPay Account at the following link:\n \"" +
                                envUrl +
                                "dashboard/merchant/api-tokens\".\n Once this Pairing Code is approved, press Enter to run some tests.\n",
                                1);

                Console.Clear();
                DrawTitle();
                Console.Write(" If you approved the pairing code on your Dashboard, press Enter to continue: ");
                var input = Console.ReadKey();
                while (input.Key != ConsoleKey.Enter)
                {
                    input = Console.ReadKey();
                }

                while (retry = !TestTokenSuccess(facade))
                {
                    SetNotification(" Something went wrong\n Please, make sure you approved the pairing code: " +
                                    pairingCode + " for " + facade +
                                    " facade\n Copy the above pairing code and approve on your BitPay Account at the following link:\n \"" +
                                    envUrl +
                                    "dashboard/merchant/api-tokens\".\n Once this Pairing Code is approved, press Enter to run some tests again.\n\n" +
                                    "* NOTE: If you approved the pairing code and a token has been stored in the configuration file, you can\n" +
                                    "then ignore this message and close this application.\n" +
                                    "If your integration does not work with the generated files, please contact support at [email protected]\n" +
                                    "or report it as an issue on the GitHub repository for this tool.\n",
                                    2);

                    Console.Clear();
                    DrawTitle();
                    Console.Write(
                        " If you approved the pairing code on your Dashboard, press Enter to try again, N to generate a new one or X to cancel: ");
                    input = Console.ReadKey();
                    while (input.Key != ConsoleKey.Enter && input.Key != ConsoleKey.N && input.Key != ConsoleKey.X)
                    {
                        input = Console.ReadKey();
                    }

                    if (input.Key == ConsoleKey.X)
                    {
                        GetPairingCodeAndToken();

                        return;
                    }

                    if (input.Key == ConsoleKey.N)
                    {
                        retry = true;
                        break;
                    }
                }
            }

            SetNotification(
                " Token tested successfully!\n A new token for the " + facade +
                " facade has been added to the configuration file.", 1);

            Console.Clear();
            DrawTitle();
            Console.WriteLine(" Would you like to generate a token for a different facade? [yes|no] (default: no)");
            Console.WriteLine();
            Console.Write(" > ");
            var redo = Console.ReadLine();

            while (redo.ToLower() != "yes" && redo.ToLower() != "no" && redo.ToLower() != "")
            {
                redo = Console.ReadLine();
            }

            if (redo.ToLower() == "no" || redo.ToLower() == "")
            {
                return;
            }

            GetPairingCodeAndToken();
        }