From 4c349ddce5fcedc368b3b03d93dbfad5623844a7 Mon Sep 17 00:00:00 2001 From: Jonas Hendrickx Date: Wed, 20 Nov 2024 14:50:13 +0100 Subject: [PATCH] discounts! --- .../Controllers/AccountsBillingController.cs | 10 ++- src/Core/Services/IPaymentService.cs | 2 +- .../Implementations/StripePaymentService.cs | 62 ++++++++++++------- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/Api/Billing/Controllers/AccountsBillingController.cs b/src/Api/Billing/Controllers/AccountsBillingController.cs index 53027c304..6427acc2d 100644 --- a/src/Api/Billing/Controllers/AccountsBillingController.cs +++ b/src/Api/Billing/Controllers/AccountsBillingController.cs @@ -79,10 +79,16 @@ public class AccountsBillingController( return TypedResults.Ok(transactions); } - [HttpPost("preview-invoice"), AllowAnonymous] + [HttpPost("preview-invoice")] public async Task PreviewInvoiceAsync([FromBody] PreviewInvoiceRequestBody model) { - var invoice = await paymentService.PreviewInvoiceAsync(model); + var user = await userService.GetUserByPrincipalAsync(User); + if (user == null) + { + throw new UnauthorizedAccessException(); + } + + var invoice = await paymentService.PreviewInvoiceAsync(model, user.GatewayCustomerId, user.GatewaySubscriptionId); return TypedResults.Ok(invoice); } diff --git a/src/Core/Services/IPaymentService.cs b/src/Core/Services/IPaymentService.cs index 9fe4b8355..c9d38ff66 100644 --- a/src/Core/Services/IPaymentService.cs +++ b/src/Core/Services/IPaymentService.cs @@ -61,5 +61,5 @@ public interface IPaymentService Task RisksSubscriptionFailure(Organization organization); Task HasSecretsManagerStandalone(Organization organization); Task<(DateTime?, DateTime?)> GetSuspensionDateAsync(Stripe.Subscription subscription); - Task PreviewInvoiceAsync(PreviewInvoiceRequestBody parameters); + Task PreviewInvoiceAsync(PreviewInvoiceRequestBody parameters, string gatewayCustomerId, string gatewaySubscriptionId); } diff --git a/src/Core/Services/Implementations/StripePaymentService.cs b/src/Core/Services/Implementations/StripePaymentService.cs index a7a0c7ea8..08e474ccc 100644 --- a/src/Core/Services/Implementations/StripePaymentService.cs +++ b/src/Core/Services/Implementations/StripePaymentService.cs @@ -1882,11 +1882,11 @@ public class StripePaymentService : IPaymentService } } - public async Task PreviewInvoiceAsync(PreviewInvoiceRequestBody parameters) + public async Task PreviewInvoiceAsync( + PreviewInvoiceRequestBody parameters, + string gatewayCustomerId, + string gatewaySubscriptionId) { - var pmStripePlan = await _stripeAdapter.PlanGetAsync("premium-annually"); - var storageStripePlan = await _stripeAdapter.PlanGetAsync("storage-gb-annually"); - var options = new InvoiceCreatePreviewOptions { AutomaticTax = new InvoiceAutomaticTaxOptions @@ -1894,28 +1894,23 @@ public class StripePaymentService : IPaymentService Enabled = true, }, Currency = "usd", - InvoiceItems = new List + Discounts = new List(), + SubscriptionDetails = new InvoiceSubscriptionDetailsOptions { - new() - { - Quantity = 1, - PriceData = new InvoiceItemPriceDataOptions + Items = + [ + new() { - Currency = "usd", - UnitAmount = pmStripePlan.Amount, - Product = pmStripePlan.ProductId - } - }, - new() - { - Quantity = parameters.PasswordManager.AdditionalStorage, - PriceData = new InvoiceItemPriceDataOptions + Quantity = 1, + Plan = "premium-annually" + }, + + new() { - Currency = "usd", - UnitAmount = storageStripePlan.Amount, - Product = storageStripePlan.ProductId + Quantity = parameters.PasswordManager.AdditionalStorage, + Plan = "storage-gb-annually" } - } + ] }, CustomerDetails = new InvoiceCustomerDetailsOptions { @@ -1950,6 +1945,29 @@ public class StripePaymentService : IPaymentService ]; } + if (gatewayCustomerId != null) + { + var gatewayCustomer = await _stripeAdapter.CustomerGetAsync(gatewayCustomerId); + + if (gatewayCustomer.Discount != null) + { + options.Discounts.Add(new InvoiceDiscountOptions + { + Discount = gatewayCustomer.Discount.Id + }); + } + + var gatewaySubscription = await _stripeAdapter.SubscriptionGetAsync(gatewaySubscriptionId); + + if (gatewaySubscription?.Discount != null) + { + options.Discounts.Add(new InvoiceDiscountOptions + { + Discount = gatewaySubscription.Discount.Id + }); + } + } + try { var invoice = await _stripeAdapter.InvoiceCreatePreviewAsync(options);