From f8baf3abb3b4e5c71666153423dcd7f14048fd32 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 10 Apr 2017 11:30:36 -0400 Subject: [PATCH] upcoming invoice info --- .../Api/Response/OrganizationResponseModel.cs | 22 +++++++++++++++---- .../Models/Business/OrganizationBilling.cs | 1 + .../Implementations/OrganizationService.cs | 14 ++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Core/Models/Api/Response/OrganizationResponseModel.cs b/src/Core/Models/Api/Response/OrganizationResponseModel.cs index 34f0a3d1e3..1050aa2119 100644 --- a/src/Core/Models/Api/Response/OrganizationResponseModel.cs +++ b/src/Core/Models/Api/Response/OrganizationResponseModel.cs @@ -43,10 +43,12 @@ namespace Bit.Core.Models.Api 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 Charges { get; set; } public class BillingSource @@ -86,9 +88,9 @@ namespace Bit.Core.Models.Api Status = sub.Status; TrialStartDate = sub.TrialStart; TrialEndDate = sub.TrialEnd; - NextBillDate = sub.CurrentPeriodEnd; + EndDate = sub.CurrentPeriodEnd; CancelledDate = sub.CanceledAt; - CancelAtNextBillDate = sub.CancelAtPeriodEnd; + CancelAtEndDate = sub.CancelAtPeriodEnd; if(sub.Items?.Data != null) { Items = sub.Items.Data.Select(i => new BillingSubscriptionItem(i)); @@ -97,9 +99,9 @@ namespace Bit.Core.Models.Api public DateTime? TrialStartDate { get; set; } public DateTime? TrialEndDate { get; set; } - public DateTime? NextBillDate { get; set; } + public DateTime? EndDate { get; set; } public DateTime? CancelledDate { get; set; } - public bool CancelAtNextBillDate { get; set; } + public bool CancelAtEndDate { get; set; } public string Status { get; set; } public IEnumerable Items { get; set; } = new List(); @@ -124,6 +126,18 @@ namespace Bit.Core.Models.Api } } + public class BillingInvoice + { + public BillingInvoice(StripeInvoice inv) + { + Amount = inv.AmountDue / 100; + Date = inv.Date.Value; + } + + public decimal Amount { get; set; } + public DateTime? Date { get; set; } + } + public class BillingCharge { public BillingCharge(StripeCharge charge) diff --git a/src/Core/Models/Business/OrganizationBilling.cs b/src/Core/Models/Business/OrganizationBilling.cs index 649162c393..6d1e922233 100644 --- a/src/Core/Models/Business/OrganizationBilling.cs +++ b/src/Core/Models/Business/OrganizationBilling.cs @@ -7,6 +7,7 @@ namespace Bit.Core.Models.Business { public Source PaymentSource { get; set; } public StripeSubscription Subscription { get; set; } + public StripeInvoice UpcomingInvoice { get; set; } public IEnumerable Charges { get; set; } = new List(); } } diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 30204bcafc..de3a5a7321 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -46,6 +46,7 @@ namespace Bit.Core.Services var customerService = new StripeCustomerService(); var subscriptionService = new StripeSubscriptionService(); var chargeService = new StripeChargeService(); + var invoiceService = new StripeInvoiceService(); if(!string.IsNullOrWhiteSpace(organization.StripeCustomerId)) { @@ -72,6 +73,19 @@ namespace Bit.Core.Services Limit = 20 }); orgBilling.Charges = charges.OrderByDescending(c => c.Created); + + if(!string.IsNullOrWhiteSpace(organization.StripeSubscriptionId)) + { + try + { + var upcomingInvoice = await invoiceService.UpcomingAsync(organization.StripeCustomerId); + if(upcomingInvoice != null) + { + orgBilling.UpcomingInvoice = upcomingInvoice; + } + } + catch(StripeException) { } + } } }