1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-19 03:58:13 -05:00

return invoices and transactions on billing api

This commit is contained in:
Kyle Spearrin 2019-02-08 23:24:48 -05:00
parent f837c1708e
commit a97a6216d7
5 changed files with 119 additions and 28 deletions

View File

@ -15,6 +15,6 @@ namespace Bit.Core.Enums
[Display(Name = "Coinbase")] [Display(Name = "Coinbase")]
Coinbase = 4, Coinbase = 4,
[Display(Name = "PayPal")] [Display(Name = "PayPal")]
PayPal = 1, PayPal = 5,
} }
} }

View File

@ -15,8 +15,9 @@ namespace Bit.Core.Models.Api
CreditAmount = billing.CreditAmount; CreditAmount = billing.CreditAmount;
PaymentSource = billing.PaymentSource != null ? new BillingSource(billing.PaymentSource) : null; PaymentSource = billing.PaymentSource != null ? new BillingSource(billing.PaymentSource) : null;
Subscription = billing.Subscription != null ? new BillingSubscription(billing.Subscription) : null; Subscription = billing.Subscription != null ? new BillingSubscription(billing.Subscription) : null;
Charges = billing.Charges.Select(c => new BillingCharge(c)); Transactions = billing.Transactions?.Select(t => new BillingTransaction(t));
UpcomingInvoice = billing.UpcomingInvoice != null ? new BillingInvoice(billing.UpcomingInvoice) : null; Invoices = billing.Invoices?.Select(i => new BillingInvoice(i));
UpcomingInvoice = billing.UpcomingInvoice != null ? new BillingInvoiceInfo(billing.UpcomingInvoice) : null;
StorageName = user.Storage.HasValue ? Utilities.CoreHelpers.ReadableBytesSize(user.Storage.Value) : null; StorageName = user.Storage.HasValue ? Utilities.CoreHelpers.ReadableBytesSize(user.Storage.Value) : null;
StorageGb = user.Storage.HasValue ? Math.Round(user.Storage.Value / 1073741824D, 2) : 0; // 1 GB StorageGb = user.Storage.HasValue ? Math.Round(user.Storage.Value / 1073741824D, 2) : 0; // 1 GB
MaxStorageGb = user.MaxStorageGb; MaxStorageGb = user.MaxStorageGb;
@ -44,8 +45,9 @@ namespace Bit.Core.Models.Api
public short? MaxStorageGb { get; set; } public short? MaxStorageGb { get; set; }
public BillingSource PaymentSource { get; set; } public BillingSource PaymentSource { get; set; }
public BillingSubscription Subscription { get; set; } public BillingSubscription Subscription { get; set; }
public BillingInvoice UpcomingInvoice { get; set; } public BillingInvoiceInfo UpcomingInvoice { get; set; }
public IEnumerable<BillingCharge> Charges { get; set; } public IEnumerable<BillingInvoice> Invoices { get; set; }
public IEnumerable<BillingTransaction> Transactions { get; set; }
public UserLicense License { get; set; } public UserLicense License { get; set; }
public DateTime? Expiration { get; set; } public DateTime? Expiration { get; set; }
} }
@ -111,9 +113,9 @@ namespace Bit.Core.Models.Api
} }
} }
public class BillingInvoice public class BillingInvoiceInfo
{ {
public BillingInvoice(BillingInfo.BillingInvoice inv) public BillingInvoiceInfo(BillingInfo.BillingInvoice inv)
{ {
Amount = inv.Amount; Amount = inv.Amount;
Date = inv.Date; Date = inv.Date;
@ -123,28 +125,44 @@ namespace Bit.Core.Models.Api
public DateTime? Date { get; set; } public DateTime? Date { get; set; }
} }
public class BillingCharge public class BillingInvoice : BillingInvoiceInfo
{ {
public BillingCharge(BillingInfo.BillingCharge charge) public BillingInvoice(BillingInfo.BillingInvoice2 inv)
: base(inv)
{ {
Amount = charge.Amount; Url = inv.Url;
RefundedAmount = charge.RefundedAmount; PdfUrl = inv.PdfUrl;
PaymentSource = charge.PaymentSource != null ? new BillingSource(charge.PaymentSource) : null; Number = inv.Number;
CreatedDate = charge.CreatedDate; Paid = inv.Paid;
FailureMessage = charge.FailureMessage; }
Refunded = charge.Refunded;
Status = charge.Status; public string Url { get; set; }
InvoiceId = charge.InvoiceId; public string PdfUrl { get; set; }
public string Number { get; set; }
public bool Paid { get; set; }
}
public class BillingTransaction
{
public BillingTransaction(BillingInfo.BillingTransaction transaction)
{
CreatedDate = transaction.CreatedDate;
Amount = transaction.Amount;
Refunded = transaction.Refunded;
RefundedAmount = transaction.RefundedAmount;
PartiallyRefunded = transaction.PartiallyRefunded;
Type = transaction.Type;
PaymentMethodType = transaction.PaymentMethodType;
Details = transaction.Details;
} }
public DateTime CreatedDate { get; set; } public DateTime CreatedDate { get; set; }
public decimal Amount { get; set; } public decimal Amount { get; set; }
public BillingSource PaymentSource { get; set; } public bool? Refunded { get; set; }
public string Status { get; set; } public bool? PartiallyRefunded { get; set; }
public string FailureMessage { get; set; } public decimal? RefundedAmount { get; set; }
public bool Refunded { get; set; } public TransactionType Type { get; set; }
public bool PartiallyRefunded => !Refunded && RefundedAmount > 0; public PaymentMethodType? PaymentMethodType { get; set; }
public decimal RefundedAmount { get; set; } public string Details { get; set; }
public string InvoiceId { get; set; }
} }
} }

View File

@ -67,8 +67,9 @@ namespace Bit.Core.Models.Api
{ {
PaymentSource = billing.PaymentSource != null ? new BillingSource(billing.PaymentSource) : null; PaymentSource = billing.PaymentSource != null ? new BillingSource(billing.PaymentSource) : null;
Subscription = billing.Subscription != null ? new BillingSubscription(billing.Subscription) : null; Subscription = billing.Subscription != null ? new BillingSubscription(billing.Subscription) : null;
Charges = billing.Charges.Select(c => new BillingCharge(c)); Transactions = billing.Transactions?.Select(t => new BillingTransaction(t));
UpcomingInvoice = billing.UpcomingInvoice != null ? new BillingInvoice(billing.UpcomingInvoice) : null; Invoices = billing.Invoices?.Select(i => new BillingInvoice(i));
UpcomingInvoice = billing.UpcomingInvoice != null ? new BillingInvoiceInfo(billing.UpcomingInvoice) : null;
StorageName = organization.Storage.HasValue ? StorageName = organization.Storage.HasValue ?
Utilities.CoreHelpers.ReadableBytesSize(organization.Storage.Value) : null; Utilities.CoreHelpers.ReadableBytesSize(organization.Storage.Value) : null;
StorageGb = organization.Storage.HasValue ? Math.Round(organization.Storage.Value / 1073741824D) : 0; // 1 GB StorageGb = organization.Storage.HasValue ? Math.Round(organization.Storage.Value / 1073741824D) : 0; // 1 GB
@ -88,8 +89,9 @@ namespace Bit.Core.Models.Api
public double? StorageGb { get; set; } public double? StorageGb { get; set; }
public BillingSource PaymentSource { get; set; } public BillingSource PaymentSource { get; set; }
public BillingSubscription Subscription { get; set; } public BillingSubscription Subscription { get; set; }
public BillingInvoice UpcomingInvoice { get; set; } public BillingInvoiceInfo UpcomingInvoice { get; set; }
public IEnumerable<BillingCharge> Charges { get; set; } public IEnumerable<BillingInvoice> Invoices { get; set; }
public IEnumerable<BillingTransaction> Transactions { get; set; }
public DateTime? Expiration { get; set; } public DateTime? Expiration { get; set; }
} }
} }

View File

@ -1,4 +1,5 @@
using Bit.Core.Enums; using Bit.Core.Enums;
using Bit.Core.Models.Table;
using Stripe; using Stripe;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -13,6 +14,8 @@ namespace Bit.Core.Models.Business
public BillingSubscription Subscription { get; set; } public BillingSubscription Subscription { get; set; }
public BillingInvoice UpcomingInvoice { get; set; } public BillingInvoice UpcomingInvoice { get; set; }
public IEnumerable<BillingCharge> Charges { get; set; } = new List<BillingCharge>(); public IEnumerable<BillingCharge> Charges { get; set; } = new List<BillingCharge>();
public IEnumerable<BillingInvoice2> Invoices { get; set; } = new List<BillingInvoice2>();
public IEnumerable<BillingTransaction> Transactions { get; set; } = new List<BillingTransaction>();
public class BillingSource public class BillingSource
{ {
@ -193,6 +196,8 @@ namespace Bit.Core.Models.Business
public class BillingInvoice public class BillingInvoice
{ {
public BillingInvoice() { }
public BillingInvoice(Invoice inv) public BillingInvoice(Invoice inv)
{ {
Amount = inv.AmountDue / 100M; Amount = inv.AmountDue / 100M;
@ -263,5 +268,63 @@ namespace Bit.Core.Models.Business
public decimal RefundedAmount { get; set; } public decimal RefundedAmount { get; set; }
public string InvoiceId { get; set; } public string InvoiceId { get; set; }
} }
public class BillingTransaction
{
public BillingTransaction(Transaction transaction)
{
CreatedDate = transaction.CreationDate;
Refunded = transaction.Refunded;
Type = transaction.Type;
PaymentMethodType = transaction.PaymentMethodType;
Details = transaction.Details;
if(transaction.RefundedAmount.HasValue)
{
RefundedAmount = Math.Abs(transaction.RefundedAmount.Value);
}
switch(transaction.Type)
{
case TransactionType.Charge:
case TransactionType.Credit:
case TransactionType.PromotionalCredit:
case TransactionType.ReferralCredit:
Amount = -1 * Math.Abs(transaction.Amount);
break;
case TransactionType.Refund:
Amount = Math.Abs(transaction.Amount);
break;
default:
break;
}
}
public DateTime CreatedDate { get; set; }
public decimal Amount { get; set; }
public bool? Refunded { get; set; }
public bool? PartiallyRefunded => !Refunded.GetValueOrDefault() && RefundedAmount.GetValueOrDefault() > 0;
public decimal? RefundedAmount { get; set; }
public TransactionType Type { get; set; }
public PaymentMethodType? PaymentMethodType { get; set; }
public string Details { get; set; }
}
public class BillingInvoice2 : BillingInvoice
{
public BillingInvoice2(Invoice inv)
{
Url = inv.HostedInvoiceUrl;
PdfUrl = inv.InvoicePdf;
Number = inv.Number;
Paid = inv.Paid;
Amount = inv.Total / 100M;
Date = inv.Date.Value;
}
public string Url { get; set; }
public string PdfUrl { get; set; }
public string Number { get; set; }
public bool Paid { get; set; }
}
} }
} }

View File

@ -958,6 +958,14 @@ namespace Bit.Core.Services
}); });
billingInfo.Charges = charges?.Data?.OrderByDescending(c => c.Created) billingInfo.Charges = charges?.Data?.OrderByDescending(c => c.Created)
.Select(c => new BillingInfo.BillingCharge(c)); .Select(c => new BillingInfo.BillingCharge(c));
var invoices = await invoiceService.ListAsync(new InvoiceListOptions
{
CustomerId = customer.Id,
Limit = 20
});
billingInfo.Invoices = invoices?.Data?.OrderByDescending(i => i.Date)
.Select(i => new BillingInfo.BillingInvoice2(i));
} }
} }