public Subscription Change(string subscriptionId, string subscriptionItemId, string overageSubscriptionItemId, string monthlyPlanId, string overagePlanId) { var subscriptionService = new Stripe.SubscriptionService(); var items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Plan = monthlyPlanId, }, new SubscriptionItemOptions { Plan = overagePlanId, } }; var options = new SubscriptionUpdateOptions { CancelAtPeriodEnd = false, Items = items, }; return(subscriptionService.Update(subscriptionId, options)); }
public async Task <int> PushSubscription(Guid organizationId, bool commit = true) { _logger.LogInformation(GetLogMessage("Push Subscription: {0}; Saving Changes: {1}"), organizationId, commit); var organization = await _organizationRepository .Queryable() .Include(x => x.Customers) .Include(x => x.OrganizationBuyerAccount) .Include(x => x.OrganizationSubscription) .Where(x => x.Id == organizationId && x.OrganizationBuyerAccount != null) .FirstAsync(); _logger.LogDebug(GetLogMessage("Organization Type: {organizationType}"), organization.OrganizationType); if (organization.OrganizationType == OrganizationType.Buyer) { return(0); } Subscription stripeSubscription = null; if (organization.OrganizationSubscription != null) { stripeSubscription = _subscriptionService.Get(organization.OrganizationSubscription.StripeSubscriptionId); } if (stripeSubscription != null) { _logger.LogDebug(GetLogMessage("Found Subscription: {StripeSubscription}"), stripeSubscription.Id); var items = _stripeSubscriptionItemService.List(new SubscriptionItemListOptions() { Subscription = stripeSubscription.Id }); var plans = _plans.Queryable() .ToDictionary(x => x.UniqueId, x => x.StripeId); var hasMarketingSubscription = items.Any(x => x.Plan.Id == plans["marketing"]); var hasRecruitingSubscription = items.Any(x => x.Plan.Id == plans["staffing"]); var hasProviderSubscription = items.Any(x => x.Plan.Id == plans["provider"]); _logger.LogDebug(GetLogMessage("Has Marketing Subscription: {0}; Has Recruiting Subscription: {1}; Has Provider Subscription: {2}"), hasMarketingSubscription, hasRecruitingSubscription, hasProviderSubscription); if (!hasMarketingSubscription && organization.OrganizationType.HasFlag(OrganizationType.Marketing)) { _logger.LogDebug(GetLogMessage("Needs marketing subscription")); await ImportSubscriptionItem(_stripeSubscriptionItemService.Create(new SubscriptionItemCreateOptions() { Subscription = stripeSubscription.Id, Plan = plans["marketing"] })); } else { _logger.LogDebug(GetLogMessage("Doesn't need marketing subscription")); } if (!hasRecruitingSubscription && organization.OrganizationType.HasFlag(OrganizationType.Recruiting)) { _logger.LogDebug(GetLogMessage("Needs recruiting subscription")); await ImportSubscriptionItem(_stripeSubscriptionItemService.Create(new SubscriptionItemCreateOptions() { Subscription = stripeSubscription.Id, Plan = plans["staffing"] })); } else { _logger.LogDebug(GetLogMessage("Doesn't need recruiting subscription")); } if (!hasProviderSubscription && organization.OrganizationType.HasFlag(OrganizationType.Provider)) { _logger.LogDebug(GetLogMessage("Needs provider subscription")); await ImportSubscriptionItem(_stripeSubscriptionItemService.Create(new SubscriptionItemCreateOptions() { Subscription = stripeSubscription.Id, Plan = plans["provider"] })); } else { _logger.LogDebug(GetLogMessage("Doesn't need provider subscription")); } stripeSubscription = _subscriptionService.Update(stripeSubscription.Id, new SubscriptionUpdateOptions() { Metadata = new Dictionary <string, string>() { { "org", organization.Name }, { "org_id", organization.Id.ToString() } } }); } else { _logger.LogDebug(GetLogMessage("No subscription found")); if (!string.IsNullOrWhiteSpace(organization.OrganizationBuyerAccount.BuyerAccountId)) { _logger.LogDebug(GetLogMessage("Buyer Account Found: {0}"), organization.OrganizationBuyerAccount.BuyerAccountId); var plans = _plans.Queryable() .ToDictionary(x => x.UniqueId, x => x.StripeId); _logger.LogDebug(GetLogMessage("{@Plans}"), plans); var items = new List <SubscriptionItemOptions>(); if (organization.OrganizationType.HasFlag(OrganizationType.Marketing)) { _logger.LogDebug(GetLogMessage("Marketing Sub Added to items")); items.Add(new SubscriptionItemOptions() { Plan = plans["marketing"], }); } if (organization.OrganizationType.HasFlag(OrganizationType.Recruiting)) { _logger.LogDebug(GetLogMessage("Recruiting Sub Added to items")); items.Add(new SubscriptionItemOptions() { Plan = plans["staffing"], }); } if (organization.OrganizationType.HasFlag(OrganizationType.Provider)) { _logger.LogDebug(GetLogMessage("Provider Sub Added to items")); items.Add(new SubscriptionItemOptions() { Plan = plans["provider"] }); } stripeSubscription = _subscriptionService.Create(new SubscriptionCreateOptions() { Customer = organization.OrganizationBuyerAccount.BuyerAccountId, Items = items, CollectionMethod = "charge_automatically", PaymentBehavior = "allow_incomplete", TrialEnd = new AnyOf <DateTime?, SubscriptionTrialEnd>(DateTime.Now.AddDays(30)), Metadata = new Dictionary <string, string>() { { "org", organization.Name }, { "org_id", organization.Id.ToString() } } }); _logger.LogInformation(GetLogMessage("Stripe Subscription Created: {0}"), stripeSubscription.Id); } else { throw new ApplicationException("No buyer account setup"); } } return(await PullSubscription(stripeSubscription)); }