1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-25 17:27:45 +01:00

[PM 202] Activate Organization when Stripe Subscription is Activated (#2820)

* Enable an org if the subscription is updated to active

* Remove expiration date update when activating Org

* improving readability of the code change

* Remove unnecessary directive

* Resolving a pr comment

* Refactoring the code to check to vale before assign

* resolve the lint issue
This commit is contained in:
cyprain-okeke 2023-04-11 17:09:38 +01:00 committed by GitHub
parent ecf885f4d6
commit 9b3d9f4488
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -115,41 +115,57 @@ public class StripeController : Controller
{
var subscription = await GetSubscriptionAsync(parsedEvent, true);
var ids = GetIdsFromMetaData(subscription.Metadata);
var organizationId = ids.Item1 ?? Guid.Empty;
var userId = ids.Item2 ?? Guid.Empty;
var subCanceled = subDeleted && subscription.Status == "canceled";
var subUnpaid = subUpdated && subscription.Status == "unpaid";
var subActive = subUpdated && subscription.Status == "active";
var subIncompleteExpired = subUpdated && subscription.Status == "incomplete_expired";
if (subCanceled || subUnpaid || subIncompleteExpired)
{
// org
if (ids.Item1.HasValue)
if (organizationId != null && organizationId != Guid.Empty)
{
await _organizationService.DisableAsync(ids.Item1.Value, subscription.CurrentPeriodEnd);
await _organizationService.DisableAsync(organizationId, subscription.CurrentPeriodEnd);
}
// user
else if (ids.Item2.HasValue)
else if (userId != null && userId != Guid.Empty)
{
await _userService.DisablePremiumAsync(ids.Item2.Value, subscription.CurrentPeriodEnd);
await _userService.DisablePremiumAsync(userId, subscription.CurrentPeriodEnd);
}
}
if (subActive)
{
if (organizationId != null && organizationId != Guid.Empty)
{
await _organizationService.EnableAsync(organizationId);
}
else if (userId != null && userId != Guid.Empty)
{
await _userService.EnablePremiumAsync(userId,
subscription.CurrentPeriodEnd);
}
}
if (subUpdated)
{
// org
if (ids.Item1.HasValue)
if (organizationId != null && organizationId != Guid.Empty)
{
await _organizationService.UpdateExpirationDateAsync(ids.Item1.Value,
await _organizationService.UpdateExpirationDateAsync(organizationId,
subscription.CurrentPeriodEnd);
if (IsSponsoredSubscription(subscription))
{
await _organizationSponsorshipRenewCommand.UpdateExpirationDateAsync(ids.Item1.Value, subscription.CurrentPeriodEnd);
await _organizationSponsorshipRenewCommand.UpdateExpirationDateAsync(organizationId, subscription.CurrentPeriodEnd);
}
}
// user
else if (ids.Item2.HasValue)
else if (userId != null && userId != Guid.Empty)
{
await _userService.UpdatePremiumExpirationAsync(ids.Item2.Value,
await _userService.UpdatePremiumExpirationAsync(userId,
subscription.CurrentPeriodEnd);
}
}