private static void AddSubscriptionPlan(Angjobs.Models.DBContext context) { string paymentSystemId = "Up100PerMo"; if (context.SubscriptionPlans.FirstOrDefault(p => p.PaymentSystemId == paymentSystemId) == null) { var plan = new SubscriptionPlan(); plan.Description = "Up to 100 job applications - monthly plan"; plan.PaymentSystemId = paymentSystemId; plan.Price = 3.99f; plan.Currency = "gbp"; plan.State = SubscriptionPlan.SubscriptionState.Available; plan.Title = "Up to 100 job applications per month"; plan.CreatedDateUtc = DateTime.UtcNow; plan.IsSoftDeleted = false; context.SubscriptionPlans.Add(plan); try { StripeManager.CreatePlan(plan); } catch (Exception ex) { Debug.WriteLine("Error:", ex.Message); } } }
/// <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); }
public void TestSubscriptions() { // Arrange EnsureTestPlansDeleted(); // NOTE: Due to the reliance on the API, we must create these for real IPlanEntity planA = CreateMockPlanA(); StripeManager.CreatePlan(planA); ICustomerEntity customer = CreateMockCustomer(); StripeManager.CreateCustomer(customer); ISubscriptionEntity subscription = CreateMockSubscription(); // CREATE // Subscribe // Act StripeSubscription newSub = StripeManager.Subscribe(customer, subscription, planA); Assert.IsNotNull(newSub); // CHANGE // ChangeSubscriptionPlan IPlanEntity planB = CreateMockPlanB(); StripeManager.CreatePlan(planB); StripeSubscription changedSub = StripeManager.ChangeSubscriptionPlan(customer, subscription, planB); Assert.IsNotNull(changedSub); // DELETE StripeSubscription cancelledSub = StripeManager.Unsubscribe(customer, subscription); Assert.IsNotNull(cancelledSub); Assert.IsTrue(cancelledSub.Status == "canceled"); }
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")); } }
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 }