1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05:00

APIs for premium. Billing helpers.

This commit is contained in:
Kyle Spearrin
2017-07-06 14:55:58 -04:00
parent 2afef85f85
commit d346ee5169
22 changed files with 789 additions and 313 deletions

View File

@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;
namespace Bit.Core.Models.Api
{
public class PremiumRequestModel : PaymentRequestModel
{
[Range(0, 99)]
public short? AdditionalStorageGb { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Bit.Core.Models.Api
{
public class StorageRequestModel : IValidatableObject
{
[Required]
public short? StroageGbAdjustment { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if(StroageGbAdjustment == 0)
{
yield return new ValidationResult("Storage adjustment cannot be 0.",
new string[] { nameof(StroageGbAdjustment) });
}
}
}
}

View File

@ -2,7 +2,7 @@
namespace Bit.Core.Models.Api
{
public class OrganizationPaymentRequestModel
public class PaymentRequestModel
{
[Required]
public string PaymentToken { get; set; }

View File

@ -0,0 +1,137 @@
using System;
using System.Linq;
using System.Collections.Generic;
using Bit.Core.Models.Business;
using Stripe;
namespace Bit.Core.Models.Api
{
public class BillingResponseModel : ResponseModel
{
public BillingResponseModel(BillingInfo billing)
: base("billing")
{
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));
UpcomingInvoice = billing.UpcomingInvoice != null ? new BillingInvoice(billing.UpcomingInvoice) : null;
}
public BillingSource PaymentSource { get; set; }
public BillingSubscription Subscription { get; set; }
public BillingInvoice UpcomingInvoice { 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}, " +
string.Format("{0}/{1}",
string.Concat(source.Card.ExpirationMonth.Length == 1 ?
"0" : string.Empty, source.Card.ExpirationMonth),
source.Card.ExpirationYear);
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;
EndDate = sub.CurrentPeriodEnd;
CancelledDate = sub.CanceledAt;
CancelAtEndDate = 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? EndDate { get; set; }
public DateTime? CancelledDate { get; set; }
public bool CancelAtEndDate { 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 / 100M;
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 BillingInvoice
{
public BillingInvoice(StripeInvoice inv)
{
Amount = inv.AmountDue / 100M;
Date = inv.Date.Value;
}
public decimal Amount { get; set; }
public DateTime? Date { get; set; }
}
public class BillingCharge
{
public BillingCharge(StripeCharge charge)
{
Amount = charge.Amount / 100M;
RefundedAmount = charge.AmountRefunded / 100M;
PaymentSource = charge.Source != null ? new BillingSource(charge.Source) : null;
CreatedDate = charge.Created;
FailureMessage = charge.FailureMessage;
Refunded = charge.Refunded;
Status = charge.Status;
InvoiceId = charge.InvoiceId;
}
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; }
public string InvoiceId { get; set; }
}
}

View File

@ -3,7 +3,6 @@ using System.Linq;
using Bit.Core.Models.Table;
using System.Collections.Generic;
using Bit.Core.Models.Business;
using Stripe;
namespace Bit.Core.Models.Api
{
@ -43,7 +42,7 @@ namespace Bit.Core.Models.Api
public class OrganizationBillingResponseModel : OrganizationResponseModel
{
public OrganizationBillingResponseModel(Organization organization, OrganizationBilling billing)
public OrganizationBillingResponseModel(Organization organization, BillingInfo billing)
: base(organization, "organizationBilling")
{
PaymentSource = billing.PaymentSource != null ? new BillingSource(billing.PaymentSource) : null;
@ -56,117 +55,5 @@ namespace Bit.Core.Models.Api
public BillingSubscription Subscription { get; set; }
public BillingInvoice UpcomingInvoice { 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}, " +
string.Format("{0}/{1}",
string.Concat(source.Card.ExpirationMonth.Length == 1 ?
"0" : string.Empty, source.Card.ExpirationMonth),
source.Card.ExpirationYear);
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;
EndDate = sub.CurrentPeriodEnd;
CancelledDate = sub.CanceledAt;
CancelAtEndDate = 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? EndDate { get; set; }
public DateTime? CancelledDate { get; set; }
public bool CancelAtEndDate { 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 / 100M;
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 BillingInvoice
{
public BillingInvoice(StripeInvoice inv)
{
Amount = inv.AmountDue / 100M;
Date = inv.Date.Value;
}
public decimal Amount { get; set; }
public DateTime? Date { get; set; }
}
public class BillingCharge
{
public BillingCharge(StripeCharge charge)
{
Amount = charge.Amount / 100M;
RefundedAmount = charge.AmountRefunded / 100M;
PaymentSource = charge.Source != null ? new BillingSource(charge.Source) : null;
CreatedDate = charge.Created;
FailureMessage = charge.FailureMessage;
Refunded = charge.Refunded;
Status = charge.Status;
InvoiceId = charge.InvoiceId;
}
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; }
public string InvoiceId { get; set; }
}
}
}

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace Bit.Core.Models.Business
{
public class OrganizationBilling
public class BillingInfo
{
public Source PaymentSource { get; set; }
public StripeSubscription Subscription { get; set; }

View File

@ -0,0 +1,10 @@
using System;
namespace Bit.Core.Models.Table
{
public interface IRevisable
{
DateTime CreationDate { get; }
DateTime RevisionDate { get; }
}
}

View File

@ -0,0 +1,10 @@
namespace Bit.Core.Models.Table
{
public interface IStorable
{
long? Storage { get; set; }
short? MaxStorageGb { get; set; }
long StorageBytesRemaining();
long StorageBytesRemaining(short maxStorageGb);
}
}

View File

@ -0,0 +1,5 @@
namespace Bit.Core.Models.Table
{
public interface IStorableSubscriber : IStorable, ISubscriber
{ }
}

View File

@ -0,0 +1,10 @@
namespace Bit.Core.Models.Table
{
public interface ISubscriber
{
string StripeCustomerId { get; set; }
string StripeSubscriptionId { get; set; }
string BillingEmailAddress();
string BillingName();
}
}

View File

@ -4,7 +4,7 @@ using Bit.Core.Enums;
namespace Bit.Core.Models.Table
{
public class Organization : IDataObject<Guid>
public class Organization : IDataObject<Guid>, ISubscriber, IStorable, IStorableSubscriber, IRevisable
{
public Guid Id { get; set; }
public string Name { get; set; }
@ -32,6 +32,16 @@ namespace Bit.Core.Models.Table
}
}
public string BillingEmailAddress()
{
return BillingEmail;
}
public string BillingName()
{
return BusinessName;
}
public long StorageBytesRemaining()
{
if(!MaxStorageGb.HasValue)
@ -39,7 +49,12 @@ namespace Bit.Core.Models.Table
return 0;
}
var maxStorageBytes = MaxStorageGb.Value * 1073741824L;
return StorageBytesRemaining(MaxStorageGb.Value);
}
public long StorageBytesRemaining(short maxStorageGb)
{
var maxStorageBytes = maxStorageGb * 1073741824L;
if(!Storage.HasValue)
{
return maxStorageBytes;

View File

@ -7,7 +7,7 @@ using System.Linq;
namespace Bit.Core.Models.Table
{
public class User : IDataObject<Guid>
public class User : IDataObject<Guid>, ISubscriber, IStorable, IStorableSubscriber, IRevisable
{
private Dictionary<TwoFactorProviderType, TwoFactorProvider> _twoFactorProviders;
@ -30,6 +30,8 @@ namespace Bit.Core.Models.Table
public bool Premium { get; set; }
public long? Storage { get; set; }
public short? MaxStorageGb { get; set; }
public string StripeCustomerId { get; set; }
public string StripeSubscriptionId { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
@ -38,6 +40,16 @@ namespace Bit.Core.Models.Table
Id = CoreHelpers.GenerateComb();
}
public string BillingEmailAddress()
{
return Email;
}
public string BillingName()
{
return Name;
}
public Dictionary<TwoFactorProviderType, TwoFactorProvider> GetTwoFactorProviders()
{
if(string.IsNullOrWhiteSpace(TwoFactorProviders))
@ -110,7 +122,12 @@ namespace Bit.Core.Models.Table
return 0;
}
var maxStorageBytes = MaxStorageGb.Value * 1073741824L;
return StorageBytesRemaining(MaxStorageGb.Value);
}
public long StorageBytesRemaining(short maxStorageGb)
{
var maxStorageBytes = maxStorageGb * 1073741824L;
if(!Storage.HasValue)
{
return maxStorageBytes;