public async Task<ActionResult> DowngradePlan() { await Task.Run(() => { var cust = getCustomer(); if (cust != null) // downgrade { // retrieve customer var customerService = new StripeCustomerService(); StripeCustomer currentCustomer = customerService.Get(cust.StripeCustomerID); // change plan StripeSubscriptionService subService = new StripeSubscriptionService(); StripeSubscriptionUpdateOptions updateOptions = new StripeSubscriptionUpdateOptions(); updateOptions.PlanId = "standard"; updateOptions.TrialEnd = currentTime; // no free days allowed when downgrading var myUpdateSuscription = subService.Update(currentCustomer.Id, cust.StripeSubscriptionID, updateOptions); // record changes in db StripeCustomers newRecord = new StripeCustomers(); newRecord.StripeCustomerID = myUpdateSuscription.CustomerId; newRecord.CustomerName = User.Identity.Name; newRecord.StripeSubscriptionID = cust.StripeSubscriptionID; newRecord.SubscriptionType = "standard"; newRecord.TrialValidUntil = currentTime; newRecord.HasSubscription = true; newRecord.StartDate = currentTime; newRecord.Interval = "Monthly"; db.StripeCustomers.Add(newRecord); db.StripeCustomers.Remove(cust); db.SaveChanges(); } }); return RedirectToAction("Index", "Home"); }
private async Task CreateCustomer(StripeModel model) { await Task.Run(() => { // create customer based on validated token from stripe.js var myCustomer = new StripeCustomerCreateOptions(); // assign token to a credit card option for a user myCustomer.Card = new StripeCreditCardOptions() { TokenId = model.Token }; myCustomer.Email = User.Identity.Name; myCustomer.Description = User.Identity.Name; myCustomer.PlanId = model.SubscriptionType.ToString(); myCustomer.Quantity = 1; // create customer in stripe service var customerService = new StripeCustomerService(); StripeCustomer stripeCustomer = customerService.Create(myCustomer); // get subscription Id from created user var subscriptionID = stripeCustomer.StripeSubscriptionList.StripeSubscriptions.FirstOrDefault().Id; // save credit card optional details StripeCustomerService customerServic = new StripeCustomerService(); stripeCustomer = customerService.Get(stripeCustomer.Id); var cardId = stripeCustomer.StripeDefaultCardId; // get card id var myCard = new StripeCardUpdateOptions(); myCard.Name = model.CardHolderName; myCard.AddressLine1 = model.AddressLine1; myCard.AddressLine2 = model.AddressLine2; myCard.AddressCity = model.AddressCity; myCard.AddressZip = model.AddressPostcode; myCard.AddressCountry = model.AddressCountry; var cardService = new StripeCardService(); StripeCard stripeCard = cardService.Update(stripeCustomer.Id, cardId, myCard); //........................ // record customer in database var cust = getCustomer(); if(cust == null) // new users { // get values to create a new record in StripeCustomer table StripeCustomers customer = new StripeCustomers(); customer.CustomerName = User.Identity.Name; customer.StripeCustomerID = stripeCustomer.Id; customer.StripeSubscriptionID = subscriptionID; customer.SubscriptionType = model.SubscriptionType.ToString(); customer.HasSubscription = true; customer.Interval = "Monthly"; customer.StartDate = TimeConverter.ConvertToLocalTime(DateTime.Now, "GMT Standard Time"); if (model.SubscriptionType.ToString() == "standard") { customer.TrialValidUntil = currentTime.AddDays(30); } db.StripeCustomers.Add(customer); } else // user with db records { StripeCustomers newRecord = new StripeCustomers(); // take the data from current user newRecord.StripeCustomerID = stripeCustomer.Id; newRecord.CustomerName = User.Identity.Name; newRecord.StripeSubscriptionID = subscriptionID; newRecord.SubscriptionType = model.SubscriptionType.ToString(); newRecord.HasSubscription = true; newRecord.Interval = "Monthly"; newRecord.StartDate = currentTime; if (model.SubscriptionType.ToString() == "standard") { newRecord.TrialValidUntil = currentTime.AddDays(30); } db.StripeCustomers.Add(newRecord); // delete customer's old record in database db.StripeCustomers.Remove(cust); } db.SaveChanges(); }); }