Exemplo n.º 1
0
        /// <summary>
        /// Unsubscribes the given subscription
        /// NOTE: Save changes on the underlying context for the model after calling this method
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="subscription"></param>
        /// <returns></returns>
        public static StripeSubscription Unsubscribe(ICustomerEntity customer, ISubscriptionEntity subscription)
        {
            if (string.IsNullOrEmpty(subscription.PaymentSystemId) || string.IsNullOrEmpty(customer.PaymentSystemId))
            {
                return(null);
            }

            var subscriptionService = new StripeSubscriptionService();
            StripeSubscription sub  = subscriptionService.Cancel(customer.PaymentSystemId, subscription.PaymentSystemId);

            subscription.PaymentSystemId = null;

            Logger.Log <StripeManager>("Unsuscribed customer in stripe: '{0}' with new subscription id '{1}", LogLevel.Information, customer.Email, subscription.PaymentSystemId);
            return(sub);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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");
        }
Exemplo n.º 4
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);
        }