Example #1
0
        /// <summary>
        /// Deletes a plan from Stripe
        /// NOTE: Delete the model from the underlying context after calling this method
        /// </summary>
        /// <param name="plan"></param>
        public static void DeletePlan(IPlanEntity plan)
        {
            var planService = new StripePlanService();

            planService.Delete(plan.PaymentSystemId);

            Logger.Log <StripeManager>("Deleting plan in stripe: '{0}' with id '{1}", LogLevel.Information, plan.Title, plan.PaymentSystemId);
        }
Example #2
0
        /// <summary>
        /// Updates the given plan
        /// NOTE: Due to limitatons with Stripe, this can only update the name of the plan
        /// </summary>
        /// <param name="plan"></param>
        public static StripePlan UpdatePlan(IPlanEntity plan)
        {
            StripePlanUpdateOptions options = new StripePlanUpdateOptions();

            options.Name = plan.Title;

            StripePlanService planService = new StripePlanService();
            StripePlan        updatedPlan = planService.Update(plan.PaymentSystemId, options);

            Logger.Log <StripeManager>("Updated plan in stripe: '{0}' with id '{1}'", LogLevel.Information, plan.Title, plan.PaymentSystemId);

            return(updatedPlan);
        }
Example #3
0
        /// <summary>
        /// Creates an alternative mock test plan
        /// </summary>
        /// <returns></returns>
        public static IPlanEntity CreateMockPlanB()
        {
            var planMock = new Mock <IPlanEntity>();

            planMock.SetupAllProperties();

            IPlanEntity plan = planMock.Object;

            plan.Title           = "Plan B";
            plan.Price           = 50;
            plan.TrialDays       = 8;
            plan.PaymentSystemId = TestPlanB_Id;

            return(plan);
        }
Example #4
0
        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");
        }
Example #5
0
        /// <summary>
        /// Creates a new plan inside of Stripe, using the given subscription plan's information
        /// NOTE: Unlike other method calls, this requires that the plan object already have a defined PaymentSystemId property set
        /// </summary>
        /// <param name="plan"></param>
        public static StripePlan CreatePlan(IPlanEntity plan)
        {
            // Save it to Stripe
            StripePlanCreateOptions newStripePlanOptions = new StripePlanCreateOptions();

            newStripePlanOptions.Amount          = Convert.ToInt32(plan.Price * 100.0); // all amounts on Stripe are in cents, pence, etc
            newStripePlanOptions.Currency        = "usd";                               // "usd" only supported right now
            newStripePlanOptions.Interval        = "month";                             // "month" or "year"
            newStripePlanOptions.IntervalCount   = 1;                                   // optional
            newStripePlanOptions.Name            = plan.Title;
            newStripePlanOptions.TrialPeriodDays = plan.TrialDays;                      // amount of time that will lapse before the customer is billed
            newStripePlanOptions.Id = plan.PaymentSystemId;

            StripePlanService planService = new StripePlanService();
            StripePlan        newPlan     = planService.Create(newStripePlanOptions);



            Logger.Log <StripeManager>("Created new plan in stripe: '{0}' with id {1}", LogLevel.Information, plan.Title, plan.PaymentSystemId);

            return(newPlan);
        }
Example #6
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"));
            }
        }
Example #7
0
 /// <summary>
 /// Generates a new, unique payment system ID for this plan
 /// NOTE: Should only be called once when creating the plan and only if there isn't a better human-readable ID in your system
 /// </summary>
 /// <param name="plan"></param>
 public static void GeneratePaymentSystemId(this IPlanEntity plan)
 {
     plan.PaymentSystemId = Guid.NewGuid().ToString("N");
 }
Example #8
0
        /// <summary>
        /// Changes the given subscription to use the new plan
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="subscription"></param>
        /// <param name="newPlan"></param>
        /// <returns></returns>
        public static StripeSubscription ChangeSubscriptionPlan(ICustomerEntity customer, ISubscriptionEntity subscription, IPlanEntity newPlan)
        {
            StripeSubscriptionUpdateOptions options = new StripeSubscriptionUpdateOptions()
            {
                PlanId = newPlan.PaymentSystemId
            };

            var subscriptionService = new StripeSubscriptionService();
            StripeSubscription changedSubscription = subscriptionService.Update(customer.PaymentSystemId, subscription.PaymentSystemId, options);

            Logger.Log <StripeManager>("Changed subscription for customer in stripe: '{0}' with new subscription id '{1}", LogLevel.Information, customer.Email, subscription.PaymentSystemId);

            return(changedSubscription);
        }
Example #9
0
        /// <summary>
        /// Subscribes the given user to the given plan, using the payment information already in stripe for that user
        /// NOTE: Save changes on the underlying context for the model after calling this method
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="subscription"></param>
        /// <param name="plan"></param>
        /// <returns></returns>
        public static StripeSubscription Subscribe(ICustomerEntity customer, ISubscriptionEntity subscription, IPlanEntity plan)
        {
            if (!string.IsNullOrEmpty(subscription.PaymentSystemId))
            {
                return(null);
            }

            var subscriptionService            = new StripeSubscriptionService();
            StripeSubscription newSubscription = subscriptionService.Create(customer.PaymentSystemId, plan.PaymentSystemId);

            subscription.PaymentSystemId = newSubscription.Id;

            Logger.Log <StripeManager>("Subscribed customer in stripe: '{0}' with new subscription id '{1}", LogLevel.Information, customer.Email, subscription.PaymentSystemId);
            return(newSubscription);
        }