public virtual StripeSubscription UpdateSubscription(string customerId, string subscriptionId, StripeCustomerUpdateSubscriptionOptions updateOptions)
		{
            var url = string.Format(Urls.Subscriptions + "/{1}", customerId, subscriptionId);
			url = ParameterBuilder.ApplyAllParameters(updateOptions, url);

			var response = Requestor.PostString(url, ApiKey);

			return Mapper<StripeSubscription>.MapFromJson(response);
		}
Esempio n. 2
0
        public StripeSubscription UpdateSubscription(string customerId, StripeCustomerUpdateSubscriptionOptions updateOptions)
        {
            var url = string.Format("{0}/{1}/subscription", Urls.Customers, customerId);
            url = ParameterBuilder.ApplyAllParameters(updateOptions, url);

            var response = Requestor.PostString(url);

            return Mapper<StripeSubscription>.MapFromJson(response);
        }
        public virtual StripeSubscription UpdateSubscription(string customerId, StripeCustomerUpdateSubscriptionOptions updateOptions)
        {
            var url = string.Format("{0}/{1}/subscription", Urls.Customers, customerId);

            url = ParameterBuilder.ApplyAllParameters(updateOptions, url);

            var response = Requestor.PostString(url);

            return(Mapper <StripeSubscription> .MapFromJson(response));
        }
        public JsonResult ChangePlan(string organizationId, string planId, string stripeToken, string last4) {
            if (String.IsNullOrEmpty(organizationId) || !User.CanAccessOrganization(organizationId))
                throw new ArgumentException("Invalid organization id.", "organizationId"); // TODO: These should probably throw http Response exceptions.

            if (!Settings.Current.EnableBilling)
                return Json(new { Success = false, Message = "Plans cannot be changed while billing is disabled." });

            Organization organization = _repository.GetById(organizationId);
            if (organization == null)
                return Json(new { Success = false, Message = "Invalid OrganizationId." });

            BillingPlan plan = _billingManager.GetBillingPlan(planId);
            if (plan == null)
                return Json(new { Success = false, Message = "Invalid PlanId." });

            if (String.Equals(organization.PlanId, plan.Id) && String.Equals(BillingManager.FreePlan.Id, plan.Id))
                return Json(new { Success = true, Message = "Your plan was not changed as you were already on the free plan." });

            // Only see if they can downgrade a plan if the plans are different.
            string message;
            if (!String.Equals(organization.PlanId, plan.Id) && !_billingManager.CanDownGrade(organization, plan, User.UserEntity, out message))
                return Json(new { Success = false, Message = message });

            var customerService = new StripeCustomerService();

            try {
                // If they are on a paid plan and then downgrade to a free plan then cancel their stripe subscription.
                if (!String.Equals(organization.PlanId, BillingManager.FreePlan.Id) && String.Equals(plan.Id, BillingManager.FreePlan.Id)) {
                    if (!String.IsNullOrEmpty(organization.StripeCustomerId))
                        customerService.CancelSubscription(organization.StripeCustomerId);

                    organization.BillingStatus = BillingStatus.Trialing;
                    organization.RemoveSuspension();
                } else if (String.IsNullOrEmpty(organization.StripeCustomerId)) {
                    if (String.IsNullOrEmpty(stripeToken))
                        return Json(new { Success = false, Message = "Billing information was not set." });

                    organization.SubscribeDate = DateTime.Now;

                    StripeCustomer customer = customerService.Create(new StripeCustomerCreateOptions {
                        TokenId = stripeToken,
                        PlanId = planId,
                        Description = organization.Name
                    });

                    organization.BillingStatus = BillingStatus.Active;
                    organization.RemoveSuspension();
                    organization.StripeCustomerId = customer.Id;
                    if (customer.StripeCardList.StripeCards.Count > 0)
                        organization.CardLast4 = customer.StripeCardList.StripeCards[0].Last4;
                } else {
                    var update = new StripeCustomerUpdateSubscriptionOptions {
                        PlanId = planId
                    };
                    bool cardUpdated = false;

                    if (!String.IsNullOrEmpty(stripeToken)) {
                        update.TokenId = stripeToken;
                        cardUpdated = true;
                    }

                    customerService.UpdateSubscription(organization.StripeCustomerId, update);
                    if (cardUpdated)
                        organization.CardLast4 = last4;

                    organization.BillingStatus = BillingStatus.Active;
                    organization.RemoveSuspension();
                }

                _billingManager.ApplyBillingPlan(organization, plan, User.UserEntity);
                _repository.Update(organization);

                _notificationSender.PlanChanged(organization.Id);
            } catch (Exception e) {
                Log.Error().Exception(e).Message("An error occurred while trying to update your billing plan: " + e.Message).Report(r => r.MarkAsCritical()).Write();
                return Json(new { Success = false, Message = e.Message });
            }

            return Json(new { Success = true });
        }
        private static StripeCustomerUpdateSubscriptionOptions InitializeUpdateSubscriptionOptions(ApplicationModel.Billing.CustomerPayment payment, Organization organization)
        {
            var myCustomer = new StripeCustomerUpdateSubscriptionOptions();

            // set these properties if using a card
            myCustomer.CardNumber = payment.CardNumber; //how is this coming in?
            myCustomer.CardExpirationYear = payment.CardExpirationYear;
            myCustomer.CardExpirationMonth = payment.CardExpirationMonth;
            myCustomer.CardAddressCountry = payment.CardAddressCountry;                // optional
            myCustomer.CardAddressLine1 = payment.CardAddressLine1;    // optional
            myCustomer.CardAddressLine2 = payment.CardAddressLine2;              // optional
            myCustomer.CardAddressCity = payment.CardAddressCity;        // optional
            myCustomer.CardAddressState = payment.CardAddressState;                  // optional
            myCustomer.CardAddressZip = payment.CardAddressZip;                 // optional
            myCustomer.CardName = payment.CardName;               // optional
            myCustomer.CardCvc = payment.CardCvc;                         // optional

            // set this property if using a token
            myCustomer.PlanId = payment.PlanId;                          // only if you have a plan
            myCustomer.CouponId =  (payment.CouponId != null && payment.CouponId != String.Empty) ? payment.CouponId : null;

            myCustomer.Prorate = true;
            if (organization.Subscription.HasTrialed)
            {
                // when the customers trial ends (overrides the plan if applicable)

                // Calculate what day of the week is 36 days from this instant.
                //System.DateTime today = DateTime.Now.ToUniversalTime();
                //Add 3 hours to let Stripe update / account for timezone
                //System.TimeSpan duration = new System.TimeSpan(0, 2, 0, 0);
                //Set time in future
                //System.DateTime threeMinutesFromNow = today.Add(duration);
                //myCustomer.TrialEnd = threeMinutesFromNow;
                myCustomer.TrialEnd = null;
            }
            return myCustomer;
        }
Esempio n. 6
0
        public UpdateCustomerResponse UpdateSubscription(UpdateCustomerRequest req)
        {
            var response = new UpdateCustomerResponse();

            try
            {
                    // Update Customer
                    var myCustomer = new StripeCustomerUpdateSubscriptionOptions();

                    if (req.CreditCard.CardNumber.Trim().Length > 0)
                    {
                        myCustomer.CardNumber = req.CreditCard.CardNumber;
                        myCustomer.CardExpirationYear = req.CreditCard.ExpirationYear.ToString();
                        myCustomer.CardExpirationMonth = req.CreditCard.ExpirationMonth.ToString();
                        myCustomer.CardAddressCountry = "US";                 // optional
                        //myCustomer.CardAddressLine1 = "24 Beef Flank St";   // optional
                        //myCustomer.CardAddressLine2 = "Apt 24";             // optional
                        //myCustomer.CardAddressState = "NC";                 // optional
                        myCustomer.CardAddressZip = req.PostalCode; //        // optional
                        myCustomer.CardName = req.CreditCard.CardHolderName;  // optional
                        if (req.CreditCard.SecurityCode.Length > 0)
                        {
                            myCustomer.CardCvc = req.CreditCard.SecurityCode;
                        }
                    }

                    myCustomer.PlanId = req.PlanId;

                    var customerService = new StripeCustomerService();
                    StripeSubscription result = customerService.UpdateSubscription(req.CustomerId, myCustomer);
                    
                    response.Success = true;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = "Unable to update subscription: " + ex.Message;
            }

            return response;
        }