public static StripeCustomerUpdateSubscriptionOptions ValidCard(string _planId = null, string _couponId = null, DateTime? _trialEnd = null) { var stripeCustomerUpdateSubscriptionOptions = new StripeCustomerUpdateSubscriptionOptions() { CardAddressCountry = "US", CardAddressLine1 = "A1A Beachfront Avenue", CardAddressState = "FL", CardAddressZip = "33101", CardCvc = "1661", CardExpirationMonth = "10", CardExpirationYear = "2012", CardName = "Vanilla Ice", CardNumber = "4242424242424242", }; if (_planId != null) stripeCustomerUpdateSubscriptionOptions.PlanId = _planId; if (_couponId != null) stripeCustomerUpdateSubscriptionOptions.CouponId = _couponId; if (_trialEnd != null) stripeCustomerUpdateSubscriptionOptions.TrialEnd = _trialEnd; return stripeCustomerUpdateSubscriptionOptions; }
public static StripeCustomerUpdateSubscriptionOptions ValidCard(string _planId = null, string _couponId = null, DateTime?_trialEnd = null) { var stripeCustomerUpdateSubscriptionOptions = new StripeCustomerUpdateSubscriptionOptions() { CardAddressCountry = "US", CardAddressLine1 = "A1A Beachfront Avenue", CardAddressState = "FL", CardAddressZip = "33101", CardCvc = "1661", CardExpirationMonth = "10", CardExpirationYear = "2012", CardName = "Vanilla Ice", CardNumber = "4242424242424242", }; if (_planId != null) { stripeCustomerUpdateSubscriptionOptions.PlanId = _planId; } if (_couponId != null) { stripeCustomerUpdateSubscriptionOptions.CouponId = _couponId; } if (_trialEnd != null) { stripeCustomerUpdateSubscriptionOptions.TrialEnd = _trialEnd; } return(stripeCustomerUpdateSubscriptionOptions); }
private bool UpdateStripeSubscription(string email, string token, int quantity, UserInfo userInfo) { var myUpdatedSubscription = new StripeCustomerUpdateSubscriptionOptions(); //if token has been created, then replace the previous one (new credit card) if (!string.IsNullOrEmpty(token)) { myUpdatedSubscription.TokenId = token; } myUpdatedSubscription.PlanId = Constants.StripeSubscriptionPlan; myUpdatedSubscription.Quantity = quantity; var customerService = new StripeCustomerService(); try { StripeSubscription stripeSubscription = customerService.UpdateSubscription(userInfo.PaymentCustomerId, myUpdatedSubscription); //save profile ID and profile status with user info userInfo.Subscribed = true; userInfo.PaymentCustomerStatus = stripeSubscription.Status; db.Entry(userInfo).State = EntityState.Modified; } catch (Exception e) { Utilities.LogAppError("Subscription modify failed: " + e.Message); return(false); } return(true); }
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); }
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); }
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().Write(); return(Json(new { Success = false, Message = e.Message })); } return(Json(new { Success = true })); }