1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-23 22:01:28 +01:00

cancel subscription service

This commit is contained in:
Kyle Spearrin 2017-04-08 18:15:20 -04:00
parent 18d2715c71
commit 52dcd6d6ab
2 changed files with 34 additions and 0 deletions

View File

@ -10,6 +10,7 @@ namespace Bit.Core.Services
{
Task<OrganizationBilling> GetBillingAsync(Organization organization);
Task ReplacePaymentMethodAsync(Guid organizationId, string paymentToken);
Task CancelSubscriptionAsync(Guid organizationId, bool endOfPeriod = false);
Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup organizationSignup);
Task<OrganizationUser> InviteUserAsync(Guid organizationId, Guid invitingUserId, string email,
Enums.OrganizationUserType type, IEnumerable<SubvaultUser> subvaults);

View File

@ -127,6 +127,39 @@ namespace Bit.Core.Services
}
}
public async Task CancelSubscriptionAsync(Guid organizationId, bool endOfPeriod = false)
{
var organization = await _organizationRepository.GetByIdAsync(organizationId);
if(organization == null)
{
throw new NotFoundException();
}
if(string.IsNullOrWhiteSpace(organization.StripeSubscriptionId))
{
throw new BadRequestException("Organization has no subscription.");
}
var subscriptionService = new StripeSubscriptionService();
var sub = await subscriptionService.GetAsync(organization.StripeCustomerId);
if(sub == null)
{
throw new BadRequestException("Organization subscription was not found.");
}
if(sub.Status == "canceled")
{
throw new BadRequestException("Organization subscription is already canceled.");
}
var canceledSub = await subscriptionService.CancelAsync(sub.Id, endOfPeriod);
if(canceledSub?.Status != "canceled")
{
throw new BadRequestException("Unable to cancel subscription.");
}
}
public async Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup signup)
{
var plan = StaticStore.Plans.FirstOrDefault(p => p.Type == signup.Plan && !p.Disabled);