1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-21 12:05:42 +01:00

discounts!

This commit is contained in:
Jonas Hendrickx 2024-11-20 14:50:13 +01:00
parent 9b5c696e1f
commit 4c349ddce5
3 changed files with 49 additions and 25 deletions

View File

@ -79,10 +79,16 @@ public class AccountsBillingController(
return TypedResults.Ok(transactions);
}
[HttpPost("preview-invoice"), AllowAnonymous]
[HttpPost("preview-invoice")]
public async Task<IResult> 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);
}

View File

@ -61,5 +61,5 @@ public interface IPaymentService
Task<bool> RisksSubscriptionFailure(Organization organization);
Task<bool> HasSecretsManagerStandalone(Organization organization);
Task<(DateTime?, DateTime?)> GetSuspensionDateAsync(Stripe.Subscription subscription);
Task<PreviewInvoiceResponseModel> PreviewInvoiceAsync(PreviewInvoiceRequestBody parameters);
Task<PreviewInvoiceResponseModel> PreviewInvoiceAsync(PreviewInvoiceRequestBody parameters, string gatewayCustomerId, string gatewaySubscriptionId);
}

View File

@ -1882,11 +1882,11 @@ public class StripePaymentService : IPaymentService
}
}
public async Task<PreviewInvoiceResponseModel> PreviewInvoiceAsync(PreviewInvoiceRequestBody parameters)
public async Task<PreviewInvoiceResponseModel> 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<InvoiceUpcomingInvoiceItemOptions>
Discounts = new List<InvoiceDiscountOptions>(),
SubscriptionDetails = new InvoiceSubscriptionDetailsOptions
{
Items =
[
new()
{
Quantity = 1,
PriceData = new InvoiceItemPriceDataOptions
{
Currency = "usd",
UnitAmount = pmStripePlan.Amount,
Product = pmStripePlan.ProductId
}
Plan = "premium-annually"
},
new()
{
Quantity = parameters.PasswordManager.AdditionalStorage,
PriceData = new InvoiceItemPriceDataOptions
{
Currency = "usd",
UnitAmount = storageStripePlan.Amount,
Product = storageStripePlan.ProductId
}
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);