public ActionResult SubmitStripeSubscriptionForm(StripeSubscriptionCheckout model) { if (ModelState.IsValid) { var loggedOnMember = Members.GetCurrentMember(); var memberService = Services.MemberService; var member = memberService.GetById(loggedOnMember.Id); var customer = new StripeCustomerCreateOptions(); customer.Email = member.Email; customer.Description = member.Name; customer.SourceToken = model.StripeToken; customer.PlanId = model.PlanId; try { var customerService = new StripeCustomerService(SensativeInformation.StripeKeys.SecretKey); StripeCustomer stripeCustomer = customerService.Create(customer); //Log customer id on member member.SetValue("stripeUserId", stripeCustomer.Id); memberService.Save(member); return(RedirectToCurrentUmbracoPage()); } catch (StripeException e) { _log.Error(e.StripeError.Message); ModelState.AddModelError("", "There was an error setting up your subscription. Please try again. If the issue persists please contact us"); } } return(CurrentUmbracoPage()); }
public ActionResult SubmitSubscribeToStripeSubscriptionForm(StripeSubscriptionCheckout model) { var loggedOnMember = Members.GetCurrentMember(); var memberService = Services.MemberService; var member = memberService.GetById(loggedOnMember.Id); var stripeUserId = member.Properties.Contains("stripeUserId") ? member.Properties["stripeUserId"].Value as string : null; try { //Add subscription var subscriptionService = new StripeSubscriptionService(SensativeInformation.StripeKeys.SecretKey); var listOptions = new StripeSubscriptionListOptions { CustomerId = stripeUserId }; var stripeSubscriptions = subscriptionService.List(listOptions); var update = stripeSubscriptions.Any(m => m.Status == StripeSubscriptionStatuses.Active); //if existingsubscripton update else create a new subscription if (update) { var subscription = stripeSubscriptions.FirstOrDefault(m => m.Status == StripeSubscriptionStatuses.Active); if (subscription != null) { StripeSubscriptionUpdateOptions so = new StripeSubscriptionUpdateOptions { PlanId = model.PlanId }; so.PlanId = model.PlanId; subscriptionService.Update(subscription.Id, so); TempData["SuccessMessage"] = "Congratulations! You have subscribed successfully. No more worrying about subs :)"; return(RedirectToCurrentUmbracoPage()); } else { _log.Error( $"Tried to update a stripe subsciption for user with id {member.Id} but could not find any stripe subscriptions for this user."); ModelState.AddModelError("", "There was an error upgrading your subscription. Please try again. If the issue persists please contact us"); return(CurrentUmbracoPage()); } } else { StripeSubscription stripeSubscription = subscriptionService.Create(stripeUserId, model.PlanId); TempData["SuccessMessage"] = "Congratulations! You have subscribed successfully. No more worrying about subs :)"; return(RedirectToCurrentUmbracoPage()); } } catch (StripeException e) { _log.Error(e.StripeError.Message); ModelState.AddModelError("", "There was an error setting up your subscription. Please try again. If the issue persists please contact us"); } return(CurrentUmbracoPage()); }