1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 09:32:48 -05:00

disable organization when subscription is canceled

This commit is contained in:
Kyle Spearrin
2017-04-26 16:14:15 -04:00
parent 3a5f667683
commit 7907d839c9
4 changed files with 80 additions and 5 deletions

View File

@ -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();
}
}