Esempio n. 1
0
        private static void DeleteTestPlanB()
        {
            // Arrange
            var plan = CreateMockPlanB();

            // Act
            StripeManager.DeletePlan(plan);
        }
Esempio n. 2
0
        /// <summary>
        /// Used to Seed Stripe during platform initialization
        /// </summary>
        /// <returns></returns>
        public static DataAccessResponseType DuplicatePlansToStripe()
        {
            var response = new DataAccessResponseType();

            var stripeManager = new StripeManager();

            //Get Plans & Frequencies to initialize plans on Stripe:
            var paymentPlans       = GetPaymentPlans(true, true, false);
            var paymentFrequencies = GetPaymentFrequencies(false);

            foreach (PaymentPlan paymentPlan in paymentPlans)
            {
                foreach (PaymentFrequency paymentFrequency in paymentFrequencies)
                {
                    // We ignore any payment plans that have no cost and or frequencies that are set to 0
                    // Stripe is only used to manage plans that have a cost associated to it above 0.0
                    if (paymentPlan.MonthlyRate != 0 && paymentFrequency.PaymentFrequencyMonths != 0)
                    {
                        //Create the new Stripe plan ID
                        var id = Sahara.Core.Common.Methods.Billing.GenerateStripePlanID(paymentPlan.PaymentPlanName, paymentFrequency.IntervalCount, paymentFrequency.Interval);

                        //Check if plan exists in Stripe, delete if it does:
                        if (stripeManager.PlanExists(id).isSuccess)
                        {
                            stripeManager.DeletePlan(id);
                        }


                        //Cretae the rest of the new Stripe plan
                        var name   = Sahara.Core.Common.Methods.Billing.GenerateStripePlanName(paymentPlan.PaymentPlanName, paymentFrequency.PaymentFrequencyName);
                        var amount = Sahara.Core.Common.Methods.Billing.GenerateStripePlanAmountInCents(paymentPlan.MonthlyRate, paymentFrequency.PaymentFrequencyMonths, paymentFrequency.PriceBreak);

                        stripeManager.CreatePlan(
                            id,
                            name,
                            amount,
                            paymentFrequency.Interval,
                            paymentFrequency.IntervalCount
                            );

                        response.isSuccess = true;
                    }
                }
            }


            return(response);
        }
Esempio n. 3
0
        public void TestPlans()
        {
            // Arrange
            EnsureTestPlansDeleted();
            IPlanEntity plan = CreateMockPlanA();

            plan.GeneratePaymentSystemId();

            // Act - create
            StripePlan createdPlan = StripeManager.CreatePlan(plan);

            // Assert - create
            Assert.IsNotNull(createdPlan);

            // Act - update
            plan.Title = "Unit Test Plan - Name Changed";
            StripePlan updatedPlan = StripeManager.UpdatePlan(plan);

            // Assert - update
            Assert.IsNotNull(updatedPlan);

            // Act - Delete
            StripeManager.DeletePlan(plan);

            // Assert
            try
            {
                StripePlanService planService = new StripePlanService();
                planService.Get(TestPlanA_Id);
                Assert.Fail(); // We should not get to this line
            }
            catch (Exception ex)
            {
                // We should get an exception that says "No such plan"
                Assert.IsTrue(ex.Message.Contains("No such plan"));
            }
        }
Esempio n. 4
0
        public static DataAccessResponseType PurgePlatform()
        {
            DataAccessResponseType response = new DataAccessResponseType();

            //We only allow purging on local, debug & stage
            if (Sahara.Core.Settings.Environment.Current.ToLower() == "production")
            {
                response.isSuccess    = false;
                response.ErrorMessage = "Cannot purge a production version of the platform";

                return(response);
            }
            else if (Sahara.Core.Settings.Environment.Current.ToLower() != "staging" && Sahara.Core.Settings.Environment.Current.ToLower() != "stage" && Sahara.Core.Settings.Environment.Current.ToLower() != "debug" && Sahara.Core.Settings.Environment.Current.ToLower() != "test" && Sahara.Core.Settings.Environment.Current.ToLower() != "testing" && Sahara.Core.Settings.Environment.Current.ToLower() != "local")
            {
                response.isSuccess    = false;
                response.ErrorMessage = "Can only purge a stage, test, debug or local version of the platform";

                return(response);
            }
            else
            {
                //Deprovision All Accounts:
                DeprovisionAllAccounts();

                //Clear Stripe Plans:
                var stripeManager = new StripeManager();
                var planIDs       = stripeManager.GetPlanIDs();
                foreach (string planID in planIDs)
                {
                    stripeManager.DeletePlan(planID);
                }

                //Delete All SQL Databases:
                DeleteAllSQLDatabases();

                //Clear Platform Storage Accounts:
                ClearPlatformStorageAccounts();

                /*======================================
                 *  Delete AccountPartition Document Database
                 * ========================================*/

                /*  RETIRED ---*/

                //var client = Sahara.Core.Settings.Azure.DocumentDB.DocumentClients.AccountDocumentClient;
                var databasename = Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseId;

                Database accountDatabase = Sahara.Core.Settings.Azure.DocumentDbClients.AccountDocumentClient.CreateDatabaseQuery().Where(db => db.Id == databasename).ToArray().FirstOrDefault();
                if (accountDatabase != null)
                {
                    //Create if not exists
                    accountDatabase = Sahara.Core.Settings.Azure.DocumentDbClients.AccountDocumentClient.DeleteDatabaseAsync(accountDatabase.SelfLink).Result;
                }



                /*======================================
                 *  FLUSH ALL REDIS CACHES
                 * ========================================*/

                var redisEndpoints = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.RedisMultiplexer.GetEndPoints(true);
                var redisServer    = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.RedisMultiplexer.GetServer(redisEndpoints[0]);

                redisServer.FlushAllDatabases();


                /*
                 * var platformEndpoints = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.PlatformManager_Multiplexer.GetEndPoints(true);
                 * var platformServer = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.PlatformManager_Multiplexer.GetServer(platformEndpoints[0]);
                 *
                 * var accountEndpoints = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.AccountManager_Multiplexer.GetEndPoints(true);
                 * var accountServer = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.AccountManager_Multiplexer.GetServer(accountEndpoints[0]);
                 *
                 * platformServer.FlushAllDatabases();
                 * accountServer.FlushAllDatabases();
                 */



                /*================================================================
                 *  DELETE ALL AZURE SEARCH DATASOURCES, INDEXES & INDEXERS (LEGACY USING SINGLE PLAN)
                 * ==================================================================
                 *
                 * SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient;
                 *
                 * //Delete all search indexes
                 * var indexList = searchServiceClient.Indexes.List();
                 * foreach (Microsoft.Azure.Search.Models.Index index in indexList.Indexes)
                 * {
                 *  searchServiceClient.Indexes.Delete(index.Name);
                 * }
                 *
                 * //Delete all search indexers
                 * var indexerList = searchServiceClient.Indexers.List();
                 * foreach (Microsoft.Azure.Search.Models.Indexer indexer in indexerList.Indexers)
                 * {
                 *  searchServiceClient.Indexers.Delete(indexer.Name);
                 * }
                 *
                 * //Delete all search datasources
                 * var dataSourcesList = searchServiceClient.DataSources.List();
                 * foreach (Microsoft.Azure.Search.Models.DataSource datasource in dataSourcesList.DataSources)
                 * {
                 *  searchServiceClient.DataSources.Delete(datasource.Name);
                 * }*/



                /*==============================================================================
                 *  DELETE ALL AZURE SEARCH DATASOURCES, INDEXES & INDEXERS (LOOP THROUGH ALL)
                 * =============================================================================*/

                //Refresh and get list:
                var searchPartitions = Settings.Azure.Search.RefreshSearchPartitions();

                foreach (var searchPartition in searchPartitions)
                {
                    SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartition.Name);

                    //Delete all search indexes
                    var indexList = searchServiceClient.Indexes.List();
                    foreach (Microsoft.Azure.Search.Models.Index index in indexList.Indexes)
                    {
                        searchServiceClient.Indexes.Delete(index.Name);
                    }

                    //Delete all search indexers
                    var indexerList = searchServiceClient.Indexers.List();
                    foreach (Microsoft.Azure.Search.Models.Indexer indexer in indexerList.Indexers)
                    {
                        searchServiceClient.Indexers.Delete(indexer.Name);
                    }

                    //Delete all search datasources
                    var dataSourcesList = searchServiceClient.DataSources.List();
                    foreach (Microsoft.Azure.Search.Models.DataSource datasource in dataSourcesList.DataSources)
                    {
                        searchServiceClient.DataSources.Delete(datasource.Name);
                    }
                }



                /*======================================
                 *   SEND BACK RESULTS
                 * ========================================*/

                response.isSuccess      = true;
                response.SuccessMessage = "Platform has been purged.";

                return(response);
            }
        }
Esempio n. 5
0
        public static DataAccessResponseType DeletePaymentPlan(string paymentPlanName)
        {
            var stripeManager = new StripeManager();
            var paymentPlan   = GetPaymentPlan(paymentPlanName);
            var frequencies   = GetPaymentFrequencies();

            #region Validate Plan Deletion

            if (paymentPlanName.ToLower() == "unprovisioned")
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot delete the unprovisioned plan."
                });
            }

            if (paymentPlanName.ToLower().Contains("trial") || paymentPlanName.ToLower().Contains("free"))
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot delete the Free or Trial plans."
                });
            }

            //Check if any accounts belong to the plan on SQL before deleting
            if (Sql.Statements.SelectStatements.AccountsWithPlanExists(paymentPlanName))
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot delete a plan that has accounts associated to it."
                });
            }

            #endregion


            #region 1. Delete each plan/frequency varient on Stripe (if applicable)

            if (paymentPlan.MonthlyRate != 0)
            {
                //If paid account, delete each varient (only paid accounts are on Stripe)
                foreach (var frequency in frequencies)
                {
                    if (frequency.PaymentFrequencyMonths != 0)
                    {
                        try
                        {
                            stripeManager.DeletePlan(Common.Methods.Billing.GenerateStripePlanID(paymentPlan.PaymentPlanName, frequency.IntervalCount, frequency.Interval));
                        }
                        catch (Exception e)
                        {
                            //Log exception and email platform admins
                            PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                                e,
                                "attempting to delete a payment plan on Stripe",
                                System.Reflection.MethodBase.GetCurrentMethod()
                                );
                        }
                    }
                }
            }

            #endregion

            #region 2. Delete the plan on SQL

            try
            {
                var isSuccess = Sql.Statements.DeleteStatements.DeletePlan(paymentPlanName);

                //Clear the cache and return results:
                PaymentPlanCaching.InvalidateAllCaches();
                return(new DataAccessResponseType {
                    isSuccess = isSuccess
                });
            }
            catch (Exception e)
            {
                //Log exception and email platform admins
                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to delete a payment plan in SQL",
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "An error occured while attempting to delete the plan on the databse. " + e.Message,
                });
            }

            #endregion
        }