1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-24 12:35:25 +01:00
This commit is contained in:
Jonas Hendrickx 2024-10-11 10:39:21 +02:00
parent 824049ea45
commit ae82074dcb
3 changed files with 43 additions and 54 deletions

View File

@ -1,4 +1,6 @@
using Bit.Core.Services;
using Bit.Api.Billing.Models.Requests;
using Bit.Api.Billing.Models.Responses;
using Bit.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
@ -46,4 +48,42 @@ public class StripeController(
return TypedResults.Ok(setupIntent.ClientSecret);
}
[HttpPost]
[Route("~/tax/calculate")]
public async Task<IResult> CalculateAsync([FromBody] CalculateTaxRequestModel requestBody)
{
var options = new Stripe.Tax.CalculationCreateOptions
{
Currency = "usd",
CustomerDetails = new()
{
Address = new()
{
PostalCode = requestBody.PostalCode,
Country = requestBody.Country
},
AddressSource = "billing"
},
LineItems = new()
{
new()
{
Amount = Convert.ToInt64(requestBody.Amount * 100),
Reference = "Subscription",
},
}
};
var taxCalculation = await stripeAdapter.CalculateTaxAsync(options);
var response = new CalculateTaxResponseModel
{
SalesTaxRate = taxCalculation.TaxBreakdown.Any()
? decimal.Parse(taxCalculation.TaxBreakdown.Single().TaxRateDetails.PercentageDecimal) / 100
: 0,
SalesTaxAmount = Convert.ToDecimal(taxCalculation.TaxAmountExclusive) / 100,
TaxableAmount = Convert.ToDecimal(requestBody.Amount),
TotalAmount = Convert.ToDecimal(taxCalculation.AmountTotal) / 100,
};
return TypedResults.Ok(response);
}
}

View File

@ -1,51 +0,0 @@
using Bit.Api.Billing.Models.Requests;
using Bit.Api.Billing.Models.Responses;
using Bit.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Bit.Api.Billing.Controllers;
[Route("tax")]
[Authorize("Application")]
public class TaxController(
IStripeAdapter stripeAdapter) : Controller
{
[HttpPost]
[Route("calculate")]
public async Task<IResult> CalculateAsync([FromBody] CalculateTaxRequestModel requestBody)
{
var options = new Stripe.Tax.CalculationCreateOptions
{
Currency = "usd",
CustomerDetails = new()
{
Address = new()
{
PostalCode = requestBody.BillingAddressPostalCode,
Country = requestBody.BillingAddressCountry
},
AddressSource = "billing"
},
LineItems = new()
{
new()
{
Amount = Convert.ToInt64(requestBody.Amount * 100),
Reference = "Subscription",
},
}
};
var taxCalculation = await stripeAdapter.CalculateTaxAsync(options);
var response = new CalculateTaxResponseModel
{
SalesTaxRate = taxCalculation.TaxBreakdown.Any()
? decimal.Parse(taxCalculation.TaxBreakdown.Single().TaxRateDetails.PercentageDecimal) / 100
: 0,
SalesTaxAmount = Convert.ToDecimal(taxCalculation.TaxAmountExclusive) / 100,
TaxableAmount = Convert.ToDecimal(requestBody.Amount),
TotalAmount = Convert.ToDecimal(taxCalculation.AmountTotal) / 100,
};
return TypedResults.Ok(response);
}
}

View File

@ -4,7 +4,7 @@ public class CalculateTaxRequestModel
{
public decimal Amount { get; set; }
public string BillingAddressPostalCode { get; set; }
public string PostalCode { get; set; }
public string BillingAddressCountry { get; set; }
public string Country { get; set; }
}