1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-21 12:05:42 +01:00
This commit is contained in:
Jonas Hendrickx 2024-11-20 11:33:27 +01:00
parent efd4dab069
commit c367db0dbe

View File

@ -1927,14 +1927,20 @@ public class StripePaymentService : IPaymentService
}, },
}; };
if (parameters.TaxInformation.TaxId != null) if (!string.IsNullOrEmpty(parameters.TaxInformation.TaxId))
{ {
var taxIdType = _taxService.GetStripeTaxCode( var taxIdType = _taxService.GetStripeTaxCode(
options.CustomerDetails.Address.Country, options.CustomerDetails.Address.Country,
parameters.TaxInformation.TaxId); parameters.TaxInformation.TaxId);
if (taxIdType != null) if (taxIdType == null)
{ {
_logger.LogWarning("Invalid tax ID '{TaxID}' for country '{Country}'.",
parameters.TaxInformation.TaxId,
parameters.TaxInformation.Country);
throw new BadRequestException("billingPreviewInvalidTaxIdError");
}
options.CustomerDetails.TaxIds = [ options.CustomerDetails.TaxIds = [
new InvoiceCustomerDetailsTaxIdOptions new InvoiceCustomerDetailsTaxIdOptions
{ {
@ -1943,16 +1949,14 @@ public class StripePaymentService : IPaymentService
} }
]; ];
} }
}
try
{
var invoice = await _stripeAdapter.InvoiceCreatePreviewAsync(options); var invoice = await _stripeAdapter.InvoiceCreatePreviewAsync(options);
decimal effectiveTaxRate = 0; var effectiveTaxRate = invoice.Tax != null && invoice.TotalExcludingTax != null
? invoice.Tax.Value.ToMajor() / invoice.TotalExcludingTax.Value.ToMajor()
if (invoice.Tax != null && invoice.TotalExcludingTax != null) : 0M;
{
effectiveTaxRate = invoice.Tax.Value.ToMajor() / invoice.TotalExcludingTax.Value.ToMajor();
}
var result = new PreviewInvoiceResponseModel( var result = new PreviewInvoiceResponseModel(
effectiveTaxRate, effectiveTaxRate,
@ -1961,6 +1965,23 @@ public class StripePaymentService : IPaymentService
invoice.Total.ToMajor()); invoice.Total.ToMajor());
return result; return result;
} }
catch (StripeException e)
{
switch (e.StripeError.Code)
{
case "tax_id_invalid":
_logger.LogWarning("Invalid tax ID '{TaxID}' for country '{Country}'.",
parameters.TaxInformation.TaxId,
parameters.TaxInformation.Country);
throw new BadRequestException("billingPreviewInvalidTaxIdError");
default:
_logger.LogError(e, "Unexpected error previewing invoice with tax ID '{TaxId}' in country '{Country}'.",
parameters.TaxInformation.TaxId,
parameters.TaxInformation.Country);
throw new BadRequestException("billingPreviewInvoiceError");
}
}
}
private PaymentMethod GetLatestCardPaymentMethod(string customerId) private PaymentMethod GetLatestCardPaymentMethod(string customerId)
{ {