mirror of
https://github.com/bitwarden/server.git
synced 2024-11-21 12:05:42 +01:00
discounts!
This commit is contained in:
parent
9b5c696e1f
commit
4c349ddce5
@ -79,10 +79,16 @@ public class AccountsBillingController(
|
|||||||
return TypedResults.Ok(transactions);
|
return TypedResults.Ok(transactions);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("preview-invoice"), AllowAnonymous]
|
[HttpPost("preview-invoice")]
|
||||||
public async Task<IResult> PreviewInvoiceAsync([FromBody] PreviewInvoiceRequestBody model)
|
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);
|
return TypedResults.Ok(invoice);
|
||||||
}
|
}
|
||||||
|
@ -61,5 +61,5 @@ public interface IPaymentService
|
|||||||
Task<bool> RisksSubscriptionFailure(Organization organization);
|
Task<bool> RisksSubscriptionFailure(Organization organization);
|
||||||
Task<bool> HasSecretsManagerStandalone(Organization organization);
|
Task<bool> HasSecretsManagerStandalone(Organization organization);
|
||||||
Task<(DateTime?, DateTime?)> GetSuspensionDateAsync(Stripe.Subscription subscription);
|
Task<(DateTime?, DateTime?)> GetSuspensionDateAsync(Stripe.Subscription subscription);
|
||||||
Task<PreviewInvoiceResponseModel> PreviewInvoiceAsync(PreviewInvoiceRequestBody parameters);
|
Task<PreviewInvoiceResponseModel> PreviewInvoiceAsync(PreviewInvoiceRequestBody parameters, string gatewayCustomerId, string gatewaySubscriptionId);
|
||||||
}
|
}
|
||||||
|
@ -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
|
var options = new InvoiceCreatePreviewOptions
|
||||||
{
|
{
|
||||||
AutomaticTax = new InvoiceAutomaticTaxOptions
|
AutomaticTax = new InvoiceAutomaticTaxOptions
|
||||||
@ -1894,28 +1894,23 @@ public class StripePaymentService : IPaymentService
|
|||||||
Enabled = true,
|
Enabled = true,
|
||||||
},
|
},
|
||||||
Currency = "usd",
|
Currency = "usd",
|
||||||
InvoiceItems = new List<InvoiceUpcomingInvoiceItemOptions>
|
Discounts = new List<InvoiceDiscountOptions>(),
|
||||||
|
SubscriptionDetails = new InvoiceSubscriptionDetailsOptions
|
||||||
{
|
{
|
||||||
|
Items =
|
||||||
|
[
|
||||||
new()
|
new()
|
||||||
{
|
{
|
||||||
Quantity = 1,
|
Quantity = 1,
|
||||||
PriceData = new InvoiceItemPriceDataOptions
|
Plan = "premium-annually"
|
||||||
{
|
|
||||||
Currency = "usd",
|
|
||||||
UnitAmount = pmStripePlan.Amount,
|
|
||||||
Product = pmStripePlan.ProductId
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
new()
|
new()
|
||||||
{
|
{
|
||||||
Quantity = parameters.PasswordManager.AdditionalStorage,
|
Quantity = parameters.PasswordManager.AdditionalStorage,
|
||||||
PriceData = new InvoiceItemPriceDataOptions
|
Plan = "storage-gb-annually"
|
||||||
{
|
|
||||||
Currency = "usd",
|
|
||||||
UnitAmount = storageStripePlan.Amount,
|
|
||||||
Product = storageStripePlan.ProductId
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
CustomerDetails = new InvoiceCustomerDetailsOptions
|
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
|
try
|
||||||
{
|
{
|
||||||
var invoice = await _stripeAdapter.InvoiceCreatePreviewAsync(options);
|
var invoice = await _stripeAdapter.InvoiceCreatePreviewAsync(options);
|
||||||
|
Loading…
Reference in New Issue
Block a user