Пример #1
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);
        }
Пример #2
0
        public static DataAccessResponseType CreatePaymentPlan(PaymentPlan paymentPlan)
        {
            var response      = new DataAccessResponseType();
            var stripeManager = new StripeManager();
            var frequencies   = GetPaymentFrequencies();

            #region Validate Input

            //Validate Plan Name
            var validationResponse = ValidationManager.IsValidPaymentPlanName(paymentPlan.PaymentPlanName);

            if (!validationResponse.isValid)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = validationResponse.validationMessage
                });
            }


            if (paymentPlan.MaxCategorizationsPerSet > 80)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "Categories cannot be grouped in amounts greater than 80 per set"
                });
            }

            if (paymentPlan.MaxProductsPerSet > 300)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "Products cannot be grouped in amounts greater than 300 per set"
                });
            }

            if (paymentPlan.MaxProperties > 160)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot have more than 160 properties on an account"
                });
            }

            if (paymentPlan.MaxValuesPerProperty > 60)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot have more than 60 values per property on an account"
                });
            }

            if (paymentPlan.MaxTags > 5000)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot have more than 5000 tags on an account"
                });
            }

            if (paymentPlan.MaxUsers > 300)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot have more than 300 users on an account"
                });
            }

            if (paymentPlan.MaxImageGroups > 60)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot have more than 60 image groups on an account"
                });
            }

            if (paymentPlan.MaxImageFormats > 240)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot have more than 240 image formats on an account"
                });
            }

            if (paymentPlan.MaxImageGalleries > 30)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot have more than 30 image galleries on an account"
                });
            }

            if (paymentPlan.MaxImagesPerGallery > 50)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot have more than 50 images per gallery on an account"
                });
            }

            #endregion

            #region 1. STRIPE transaction (if applicable)

            if (paymentPlan.MonthlyRate != 0)
            {
                //Add to stripe first, if fails, respond with error and stop the process.
                foreach (PaymentFrequency frequency in frequencies)
                {
                    // 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 (frequency.PaymentFrequencyMonths != 0)
                    {
                        //Create the new Stripe plan ID
                        var id = Sahara.Core.Common.Methods.Billing.GenerateStripePlanID(paymentPlan.PaymentPlanName, frequency.IntervalCount, frequency.Interval);

                        //Check if plan exists in Stripe, return an error if it does
                        if (stripeManager.PlanExists(id).isSuccess)
                        {
                            return(new DataAccessResponseType {
                                isSuccess = false, ErrorMessage = "Plan variant exists on Stripe. Operation aborted."
                            });
                        }

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

                        try
                        {
                            stripeManager.CreatePlan(
                                id,
                                name,
                                amount,
                                frequency.Interval,
                                frequency.IntervalCount
                                );
                        }
                        catch (Exception e)
                        {
                            //Log exception and email platform admins
                            PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                                e,
                                "attempting to create a payment plan on Stripe",
                                System.Reflection.MethodBase.GetCurrentMethod()
                                );

                            return(new DataAccessResponseType {
                                isSuccess = false, ErrorMessage = "An error occurred while attempting to add a plan varient to Stripe. Operation aborted."
                            });
                        }
                    }
                }
            }
            else
            {
            }

            #endregion

            #region 2. SQL Transaction

            try
            {
                response.isSuccess = Sql.Statements.InsertStatements.InsertPaymentPlan(paymentPlan.PaymentPlanName, paymentPlan.Visible, paymentPlan.MonthlyRate, paymentPlan.MaxUsers,
                                                                                       paymentPlan.MaxCategorizationsPerSet, paymentPlan.MaxProductsPerSet, paymentPlan.MaxProperties, paymentPlan.MaxValuesPerProperty, paymentPlan.MaxTags, paymentPlan.AllowSalesLeads,
                                                                                       paymentPlan.MonthlySupportHours, paymentPlan.AllowLocationData, paymentPlan.AllowCustomOrdering, paymentPlan.AllowThemes, paymentPlan.AllowImageEnhancements, paymentPlan.MaxImageGroups, paymentPlan.MaxImageFormats, paymentPlan.MaxImageGalleries, paymentPlan.MaxImagesPerGallery).isSuccess;
                //Clear the cache and return results:
                PaymentPlanCaching.InvalidateAllCaches();
                return(response);
            }
            catch (Exception e)
            {
                //Log exception and email platform admins
                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to insert a payment plan into SQL",
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "Stripe transaction was successful, but there was an issue while attempting to add plan to the database. Operation aborted. " + e.Message
                });
            }

            #endregion
        }