mirror of
https://github.com/bitwarden/server.git
synced 2025-05-23 04:21:05 -05:00
apis for org settings & billing
This commit is contained in:
parent
8ab363cc73
commit
2d7cb1321b
@ -54,7 +54,7 @@ namespace Bit.Api.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{id}/billing")]
|
[HttpGet("{id}/billing")]
|
||||||
public async Task<OrganizationResponseModel> GetBilling(string id)
|
public async Task<OrganizationBillingResponseModel> GetBilling(string id)
|
||||||
{
|
{
|
||||||
var orgIdGuid = new Guid(id);
|
var orgIdGuid = new Guid(id);
|
||||||
if(!_currentContext.OrganizationOwner(orgIdGuid))
|
if(!_currentContext.OrganizationOwner(orgIdGuid))
|
||||||
@ -68,9 +68,13 @@ namespace Bit.Api.Controllers
|
|||||||
throw new NotFoundException();
|
throw new NotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: billing stuff
|
var billingInfo = await _organizationService.GetBillingAsync(organization);
|
||||||
|
if(billingInfo == null)
|
||||||
|
{
|
||||||
|
throw new NotFoundException();
|
||||||
|
}
|
||||||
|
|
||||||
return new OrganizationResponseModel(organization);
|
return new OrganizationBillingResponseModel(organization, billingInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("")]
|
[HttpGet("")]
|
||||||
|
@ -1,14 +1,25 @@
|
|||||||
using Bit.Core.Models.Table;
|
using Bit.Core.Models.Table;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
namespace Bit.Core.Models.Api
|
namespace Bit.Core.Models.Api
|
||||||
{
|
{
|
||||||
public class OrganizationUpdateRequestModel
|
public class OrganizationUpdateRequestModel
|
||||||
{
|
{
|
||||||
|
[Required]
|
||||||
|
[StringLength(50)]
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
[StringLength(50)]
|
||||||
|
public string BusinessName { get; set; }
|
||||||
|
[EmailAddress]
|
||||||
|
[Required]
|
||||||
|
[StringLength(50)]
|
||||||
|
public string BillingEmail { get; set; }
|
||||||
|
|
||||||
public virtual Organization ToOrganization(Organization existingOrganization)
|
public virtual Organization ToOrganization(Organization existingOrganization)
|
||||||
{
|
{
|
||||||
existingOrganization.Name = Name;
|
existingOrganization.Name = Name;
|
||||||
|
existingOrganization.BusinessName = BusinessName;
|
||||||
|
existingOrganization.BillingEmail = BillingEmail;
|
||||||
return existingOrganization;
|
return existingOrganization;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using Bit.Core.Models.Table;
|
using Bit.Core.Models.Table;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Bit.Core.Models.Business;
|
||||||
|
using Stripe;
|
||||||
|
|
||||||
namespace Bit.Core.Models.Api
|
namespace Bit.Core.Models.Api
|
||||||
{
|
{
|
||||||
@ -15,6 +19,8 @@ namespace Bit.Core.Models.Api
|
|||||||
|
|
||||||
Id = organization.Id.ToString();
|
Id = organization.Id.ToString();
|
||||||
Name = organization.Name;
|
Name = organization.Name;
|
||||||
|
BusinessName = organization.BusinessName;
|
||||||
|
BillingEmail = organization.BillingEmail;
|
||||||
Plan = organization.Plan;
|
Plan = organization.Plan;
|
||||||
PlanType = organization.PlanType;
|
PlanType = organization.PlanType;
|
||||||
PlanTrial = organization.PlanTrial;
|
PlanTrial = organization.PlanTrial;
|
||||||
@ -23,9 +29,120 @@ namespace Bit.Core.Models.Api
|
|||||||
|
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
public string BusinessName { get; set; }
|
||||||
|
public string BillingEmail { get; set; }
|
||||||
public string Plan { get; set; }
|
public string Plan { get; set; }
|
||||||
public Enums.PlanType PlanType { get; set; }
|
public Enums.PlanType PlanType { get; set; }
|
||||||
public bool PlanTrial { get; set; }
|
public bool PlanTrial { get; set; }
|
||||||
public short MaxUsers { get; set; }
|
public short MaxUsers { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class OrganizationBillingResponseModel : OrganizationResponseModel
|
||||||
|
{
|
||||||
|
public OrganizationBillingResponseModel(Organization organization, OrganizationBilling billing)
|
||||||
|
: base(organization, "organizationBilling")
|
||||||
|
{
|
||||||
|
PaymentSource = billing.PaymentSource != null ? new BillingSource(billing.PaymentSource) : null;
|
||||||
|
Subscription = billing.Subscription != null ? new BillingSubscription(billing.Subscription) : null;
|
||||||
|
Charges = billing.Charges.Select(c => new BillingCharge(c));
|
||||||
|
}
|
||||||
|
|
||||||
|
public BillingSource PaymentSource { get; set; }
|
||||||
|
public BillingSubscription Subscription { get; set; }
|
||||||
|
public IEnumerable<BillingCharge> Charges { get; set; }
|
||||||
|
|
||||||
|
public class BillingSource
|
||||||
|
{
|
||||||
|
public BillingSource(Source source)
|
||||||
|
{
|
||||||
|
Type = source.Type;
|
||||||
|
|
||||||
|
switch(source.Type)
|
||||||
|
{
|
||||||
|
case SourceType.Card:
|
||||||
|
Description = $"{source.Card.Brand}, *{source.Card.Last4}";
|
||||||
|
CardBrand = source.Card.Brand;
|
||||||
|
break;
|
||||||
|
case SourceType.BankAccount:
|
||||||
|
Description = $"{source.BankAccount.BankName}, *{source.BankAccount.Last4}";
|
||||||
|
break;
|
||||||
|
// bitcoin/alipay?
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SourceType Type { get; set; }
|
||||||
|
public string CardBrand { get; set; }
|
||||||
|
public string Description { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BillingSubscription
|
||||||
|
{
|
||||||
|
public BillingSubscription(StripeSubscription sub)
|
||||||
|
{
|
||||||
|
Status = sub.Status;
|
||||||
|
TrialStartDate = sub.TrialStart;
|
||||||
|
TrialEndDate = sub.TrialEnd;
|
||||||
|
NextBillDate = sub.CurrentPeriodEnd;
|
||||||
|
CancelledDate = sub.CanceledAt;
|
||||||
|
CancelAtNextBillDate = sub.CancelAtPeriodEnd;
|
||||||
|
if(sub.Items?.Data != null)
|
||||||
|
{
|
||||||
|
Items = sub.Items.Data.Select(i => new BillingSubscriptionItem(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTime? TrialStartDate { get; set; }
|
||||||
|
public DateTime? TrialEndDate { get; set; }
|
||||||
|
public DateTime? NextBillDate { get; set; }
|
||||||
|
public DateTime? CancelledDate { get; set; }
|
||||||
|
public bool CancelAtNextBillDate { get; set; }
|
||||||
|
public string Status { get; set; }
|
||||||
|
public IEnumerable<BillingSubscriptionItem> Items { get; set; } = new List<BillingSubscriptionItem>();
|
||||||
|
|
||||||
|
public class BillingSubscriptionItem
|
||||||
|
{
|
||||||
|
public BillingSubscriptionItem(StripeSubscriptionItem item)
|
||||||
|
{
|
||||||
|
if(item.Plan != null)
|
||||||
|
{
|
||||||
|
Name = item.Plan.Name;
|
||||||
|
Amount = item.Plan.Amount / 100;
|
||||||
|
Interval = item.Plan.Interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
Quantity = item.Quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name { get; set; }
|
||||||
|
public decimal Amount { get; set; }
|
||||||
|
public int Quantity { get; set; }
|
||||||
|
public string Interval { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BillingCharge
|
||||||
|
{
|
||||||
|
public BillingCharge(StripeCharge charge)
|
||||||
|
{
|
||||||
|
Amount = charge.Amount / 100;
|
||||||
|
RefundedAmount = charge.AmountRefunded / 100;
|
||||||
|
PaymentSource = charge.Source != null ? new BillingSource(charge.Source) : null;
|
||||||
|
CreatedDate = charge.Created;
|
||||||
|
FailureMessage = charge.FailureMessage;
|
||||||
|
Refunded = charge.Refunded;
|
||||||
|
Status = charge.Status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTime CreatedDate { get; set; }
|
||||||
|
public decimal Amount { get; set; }
|
||||||
|
public BillingSource PaymentSource { get; set; }
|
||||||
|
public string Status { get; set; }
|
||||||
|
public string FailureMessage { get; set; }
|
||||||
|
public bool Refunded { get; set; }
|
||||||
|
public bool PartiallyRefunded => !Refunded && RefundedAmount > 0;
|
||||||
|
public decimal RefundedAmount { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
12
src/Core/Models/Business/OrganizationBilling.cs
Normal file
12
src/Core/Models/Business/OrganizationBilling.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using Stripe;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models.Business
|
||||||
|
{
|
||||||
|
public class OrganizationBilling
|
||||||
|
{
|
||||||
|
public Source PaymentSource { get; set; }
|
||||||
|
public StripeSubscription Subscription { get; set; }
|
||||||
|
public IEnumerable<StripeCharge> Charges { get; set; } = new List<StripeCharge>();
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ namespace Bit.Core.Services
|
|||||||
{
|
{
|
||||||
public interface IOrganizationService
|
public interface IOrganizationService
|
||||||
{
|
{
|
||||||
|
Task<OrganizationBilling> GetBillingAsync(Organization organization);
|
||||||
Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup organizationSignup);
|
Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup organizationSignup);
|
||||||
Task<OrganizationUser> InviteUserAsync(Guid organizationId, Guid invitingUserId, string email,
|
Task<OrganizationUser> InviteUserAsync(Guid organizationId, Guid invitingUserId, string email,
|
||||||
Enums.OrganizationUserType type, IEnumerable<SubvaultUser> subvaults);
|
Enums.OrganizationUserType type, IEnumerable<SubvaultUser> subvaults);
|
||||||
|
@ -39,6 +39,40 @@ namespace Bit.Core.Services
|
|||||||
_dataProtector = dataProtectionProvider.CreateProtector("OrganizationServiceDataProtector");
|
_dataProtector = dataProtectionProvider.CreateProtector("OrganizationServiceDataProtector");
|
||||||
_mailService = mailService;
|
_mailService = mailService;
|
||||||
}
|
}
|
||||||
|
public async Task<OrganizationBilling> GetBillingAsync(Organization organization)
|
||||||
|
{
|
||||||
|
var orgBilling = new OrganizationBilling();
|
||||||
|
var customerService = new StripeCustomerService();
|
||||||
|
var subscriptionService = new StripeSubscriptionService();
|
||||||
|
var chargeService = new StripeChargeService();
|
||||||
|
|
||||||
|
if(!string.IsNullOrWhiteSpace(organization.StripeCustomerId))
|
||||||
|
{
|
||||||
|
var customer = await customerService.GetAsync(organization.StripeCustomerId);
|
||||||
|
if(customer != null)
|
||||||
|
{
|
||||||
|
orgBilling.PaymentSource = customer.DefaultSource;
|
||||||
|
|
||||||
|
var charges = await chargeService.ListAsync(new StripeChargeListOptions
|
||||||
|
{
|
||||||
|
CustomerId = customer.Id,
|
||||||
|
Limit = 20
|
||||||
|
});
|
||||||
|
orgBilling.Charges = charges.OrderByDescending(c => c.Created);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!string.IsNullOrWhiteSpace(organization.StripeSubscriptionId))
|
||||||
|
{
|
||||||
|
var sub = await subscriptionService.GetAsync(organization.StripeSubscriptionId);
|
||||||
|
if(sub != null)
|
||||||
|
{
|
||||||
|
orgBilling.Subscription = sub;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return orgBilling;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup signup)
|
public async Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup signup)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user