public StripeSubscriptionServiceTest() { this.service = new StripeSubscriptionService(); this.createOptions = new StripeSubscriptionCreateOptions() { Items = new List <StripeSubscriptionItemOption> { new StripeSubscriptionItemOption { PlanId = "plan_123", Quantity = 2 }, new StripeSubscriptionItemOption { PlanId = "plan_124", Quantity = 3 }, }, }; this.updateOptions = new StripeSubscriptionUpdateOptions() { Metadata = new Dictionary <string, string>() { { "key", "value" }, }, }; this.listOptions = new StripeSubscriptionListOptions() { Limit = 1, }; }
public async Task <IActionResult> StripeSubscriptions(StripeSubscriptionListOptions options) { options = options ?? new StripeSubscriptionListOptions(); options.Limit = 10; options.Expand = new List <string>() { "data.customer", "data.latest_invoice" }; options.SelectAll = false; var subscriptions = await _stripeAdapter.SubscriptionListAsync(options); options.StartingAfter = subscriptions.LastOrDefault()?.Id; options.EndingBefore = await StripeSubscriptionsGetHasPreviousPage(subscriptions, options) ? subscriptions.FirstOrDefault()?.Id : null; var model = new StripeSubscriptionsModel() { Items = subscriptions.Select(s => new StripeSubscriptionRowModel(s)).ToList(), Prices = (await _stripeAdapter.PriceListAsync(new Stripe.PriceListOptions() { Limit = 100 })).Data, TestClocks = await _stripeAdapter.TestClockListAsync(), Filter = options }; return(View(model)); }
public ActionResult ManageSubscription() { var model = new ManageSubscriptionViewModel(); try { //GetUptodate plan prices from stripe var planService = new StripePlanService(SensativeInformation.StripeKeys.SecretKey); var ordPlan = planService.Get(StaticIdentifiers.OrdinaryMemberPlanId); var socialPlan = planService.Get(StaticIdentifiers.SocialMemberPlanId); var patronPlan = planService.Get(StaticIdentifiers.PatronPlanId); if (ordPlan != null) { model.OrdinaryPrice = (ordPlan.Amount / 100m).ToString("N"); } if (socialPlan != null) { model.SocialPrice = (socialPlan.Amount / 100m).ToString("N"); } if (patronPlan != null) { model.PatronPrice = (patronPlan.Amount / 100m).ToString("N"); } } catch (StripeException e) { _log.Error($"There was a stripe error whilst trying to load the current subscriptions prices for the manage membership page. The error was {e.Message}."); } try { var stripeAccountId = new Member(Members.GetCurrentMember()).StripeUserId; var dm = new DataManager(); if (stripeAccountId.IsNotNullOrEmpty()) { model.IsStripeUser = true; model.HasExistingSubscription = false; //Get plan status var subscriptionService = new StripeSubscriptionService(SensativeInformation.StripeKeys.SecretKey); var listOption = new StripeSubscriptionListOptions { CustomerId = stripeAccountId }; var stripeSubscriptions = subscriptionService.List(listOption); if (stripeSubscriptions != null && stripeSubscriptions.Any()) { model.IsOrdinaryMember = stripeSubscriptions.Any(m => m.StripePlan.Id == StaticIdentifiers.OrdinaryMemberPlanId && m.Status == StripeSubscriptionStatuses.Active); model.IsSocialMember = stripeSubscriptions.Any(m => m.StripePlan.Id == StaticIdentifiers.SocialMemberPlanId && m.Status == StripeSubscriptionStatuses.Active); model.IsPatron = stripeSubscriptions.Any(m => m.StripePlan.Id == StaticIdentifiers.PatronPlanId && m.Status == StripeSubscriptionStatuses.Active); model.HasExistingSubscription = stripeSubscriptions.Any(m => m.Status == StripeSubscriptionStatuses.Active); } } } catch (StripeException e) { _log.Error($"There was a stripe error whilst trying to load the current subscriptions for the managemembership page for user {Members.GetCurrentMember().Name} and id {Members.GetCurrentMember().Id}. The error was {e.Message}."); } return(PartialView("ManageSubscription", model)); }
public IViewComponentResult Invoke(int numberOfItems) { var user = GetCurrentUserAsync(); if (!string.IsNullOrEmpty(user.StripeCustomerId)) { var customerService = new StripeSubscriptionService(_stripeSettings.Value.SecretKey); var StripSubscriptionListOption = new StripeSubscriptionListOptions() { CustomerId = user.StripeCustomerId, Limit = 100 }; var customerSubscription = new CustomerPaymentViewModel(); try { var subscriptions = customerService.List(StripSubscriptionListOption); customerSubscription = new CustomerPaymentViewModel { UserName = user.Email, Subscriptions = subscriptions.Select(s => new CustomerSubscriptionViewModel { Id = s.Id, Name = s.StripePlan.Id, Amount = s.StripePlan.Amount, Currency = s.StripePlan.Currency, Status = s.Status }).ToList() }; } catch (StripeException sex) { ModelState.AddModelError("CustmoerNotFound", sex.Message); } return(View("View", customerSubscription)); } var subscription = new CustomerPaymentViewModel { UserName = user.Email, Subscriptions = new List <CustomerSubscriptionViewModel>() }; return(View("View", subscription)); }
// GET: Purchase public async Task <ActionResult> Index() { var userId = User.Identity.GetUserId(); ApplicationUser user = await UserManager.FindByIdAsync(userId); if (!string.IsNullOrEmpty(user.CustomerIdentifier)) { StripeConfiguration.SetApiKey("sk_test_ILBG36E21hK6nO8C6fOpQvWs"); var subscriptionSerive = new StripeSubscriptionService(); StripeSubscriptionListOptions listOptions = new StripeSubscriptionListOptions(); listOptions.CustomerId = user.CustomerIdentifier; IEnumerable <StripeSubscription> response = subscriptionSerive.List(listOptions); ViewBag.StripeKey = "pk_test_rAvojOoog9JrtM0vSmKm4r0D"; } return(View()); }
public async Task <List <Stripe.Subscription> > SubscriptionListAsync(StripeSubscriptionListOptions options) { if (!options.SelectAll) { return((await _subscriptionService.ListAsync(options.ToStripeApiOptions())).Data); } options.Limit = 100; var items = new List <Stripe.Subscription>(); await foreach (var i in _subscriptionService.ListAutoPagingAsync(options.ToStripeApiOptions())) { items.Add(i); } return(items); }
// This requires a redundant API call to Stripe because of the way they handle pagination. // The StartingBefore value has to be infered from the list we get, and isn't supplied by Stripe. private async Task <bool> StripeSubscriptionsGetHasPreviousPage(List <Stripe.Subscription> subscriptions, StripeSubscriptionListOptions options) { var hasPreviousPage = false; if (subscriptions.FirstOrDefault()?.Id != null) { var previousPageSearchOptions = new StripeSubscriptionListOptions() { EndingBefore = subscriptions.FirstOrDefault().Id, Limit = 1, Status = options.Status, CurrentPeriodEndDate = options.CurrentPeriodEndDate, CurrentPeriodEndRange = options.CurrentPeriodEndRange, Price = options.Price }; hasPreviousPage = (await _stripeAdapter.SubscriptionListAsync(previousPageSearchOptions)).Count > 0; } return(hasPreviousPage); }
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()); }