mirror of
https://github.com/bitwarden/server.git
synced 2025-04-16 18:48:16 -05:00
disable organization when subscription is canceled
This commit is contained in:
parent
3a5f667683
commit
7907d839c9
@ -1,5 +1,10 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Stripe;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Bit.Billing.Controllers
|
||||
{
|
||||
@ -7,20 +12,53 @@ namespace Bit.Billing.Controllers
|
||||
public class StripeController : Controller
|
||||
{
|
||||
private readonly BillingSettings _billingSettings;
|
||||
private readonly IHostingEnvironment _hostingEnvironment;
|
||||
private readonly IOrganizationService _organizationService;
|
||||
|
||||
public StripeController(IOptions<BillingSettings> billingSettings)
|
||||
public StripeController(
|
||||
IOptions<BillingSettings> billingSettings,
|
||||
IHostingEnvironment hostingEnvironment,
|
||||
IOrganizationService organizationService)
|
||||
{
|
||||
_billingSettings = billingSettings?.Value;
|
||||
_hostingEnvironment = hostingEnvironment;
|
||||
_organizationService = organizationService;
|
||||
}
|
||||
|
||||
[HttpPost("webhook")]
|
||||
public IActionResult PostWebhook([FromBody]dynamic body, [FromQuery] string key)
|
||||
public async Task<IActionResult> PostWebhook([FromBody]dynamic body, [FromQuery] string key)
|
||||
{
|
||||
if(key != _billingSettings.StripeWebhookKey)
|
||||
if(body == null || key != _billingSettings.StripeWebhookKey)
|
||||
{
|
||||
return new BadRequestResult();
|
||||
}
|
||||
|
||||
var parsedEvent = StripeEventUtility.ParseEventDataItem<StripeEvent>(body) as StripeEvent;
|
||||
if(string.IsNullOrWhiteSpace(parsedEvent?.Id))
|
||||
{
|
||||
return new BadRequestResult();
|
||||
}
|
||||
|
||||
if(_hostingEnvironment.IsProduction() && !parsedEvent.LiveMode)
|
||||
{
|
||||
return new BadRequestResult();
|
||||
}
|
||||
|
||||
if(parsedEvent.Type == "customer.subscription.deleted")
|
||||
{
|
||||
var subscription = Mapper<StripeSubscription>.MapFromJson(parsedEvent.Data.Object.ToString())
|
||||
as StripeSubscription;
|
||||
if(subscription?.Status == "canceled" && (subscription.Metadata?.ContainsKey("organizationId") ?? false))
|
||||
{
|
||||
var orgIdGuid = new Guid(subscription.Metadata["organizationId"]);
|
||||
await _organizationService.DisableAsync(orgIdGuid);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// not handling this event type
|
||||
}
|
||||
|
||||
return new OkResult();
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,10 @@ namespace Bit.Core.Models.Table
|
||||
|
||||
public void SetNewId()
|
||||
{
|
||||
Id = CoreHelpers.GenerateComb();
|
||||
if(Id == default(Guid))
|
||||
{
|
||||
Id = CoreHelpers.GenerateComb();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,8 @@ namespace Bit.Core.Services
|
||||
Task AdjustSeatsAsync(Guid organizationId, int seatAdjustment);
|
||||
Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup organizationSignup);
|
||||
Task DeleteAsync(Organization organization);
|
||||
Task DisableAsync(Guid organizationId);
|
||||
Task EnableAsync(Guid organizationId);
|
||||
Task UpdateAsync(Organization organization, bool updateBilling = false);
|
||||
Task<OrganizationUser> InviteUserAsync(Guid organizationId, Guid invitingUserId, string email,
|
||||
Enums.OrganizationUserType type, bool accessAllSubvaults, IEnumerable<SubvaultUser> subvaults);
|
||||
|
@ -294,6 +294,9 @@ namespace Bit.Core.Services
|
||||
PlanId = newPlan.StripePlanId,
|
||||
Quantity = 1
|
||||
}
|
||||
},
|
||||
Metadata = new Dictionary<string, string> {
|
||||
{ "organizationId", organization.Id.ToString() }
|
||||
}
|
||||
};
|
||||
|
||||
@ -478,6 +481,9 @@ namespace Bit.Core.Services
|
||||
$"{plan.MaxAdditionalSeats.GetValueOrDefault(0)} additional users.");
|
||||
}
|
||||
|
||||
// Pre-generate the org id so that we can save it with the Stripe subscription..
|
||||
Guid newOrgId = CoreHelpers.GenerateComb();
|
||||
|
||||
if(plan.Type == PlanType.Free)
|
||||
{
|
||||
var ownerExistingOrgCount =
|
||||
@ -505,6 +511,9 @@ namespace Bit.Core.Services
|
||||
PlanId = plan.StripePlanId,
|
||||
Quantity = 1
|
||||
}
|
||||
},
|
||||
Metadata = new Dictionary<string, string> {
|
||||
{ "organizationId", newOrgId.ToString() }
|
||||
}
|
||||
};
|
||||
|
||||
@ -534,6 +543,7 @@ namespace Bit.Core.Services
|
||||
|
||||
var organization = new Organization
|
||||
{
|
||||
Id = newOrgId,
|
||||
Name = signup.Name,
|
||||
BillingEmail = signup.BillingEmail,
|
||||
BusinessName = signup.BusinessName,
|
||||
@ -617,6 +627,28 @@ namespace Bit.Core.Services
|
||||
await _organizationRepository.DeleteAsync(organization);
|
||||
}
|
||||
|
||||
public async Task DisableAsync(Guid organizationId)
|
||||
{
|
||||
var org = await _organizationRepository.GetByIdAsync(organizationId);
|
||||
if(org != null && org.Enabled)
|
||||
{
|
||||
org.Enabled = false;
|
||||
await _organizationRepository.ReplaceAsync(org);
|
||||
|
||||
// TODO: send email to owners?
|
||||
}
|
||||
}
|
||||
|
||||
public async Task EnableAsync(Guid organizationId)
|
||||
{
|
||||
var org = await _organizationRepository.GetByIdAsync(organizationId);
|
||||
if(org != null && !org.Enabled)
|
||||
{
|
||||
org.Enabled = true;
|
||||
await _organizationRepository.ReplaceAsync(org);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(Organization organization, bool updateBilling = false)
|
||||
{
|
||||
if(organization.Id == default(Guid))
|
||||
|
Loading…
x
Reference in New Issue
Block a user