mirror of
https://github.com/bitwarden/server.git
synced 2025-04-06 21:48:12 -05:00
Early return zero or negative amount invoices (#1595)
Stripe handles these by immediately finalizing as paid and crediting their account the appropriate amount.
This commit is contained in:
parent
dac3b3e893
commit
3d74f514ad
@ -747,6 +747,19 @@ namespace Bit.Core.Services
|
|||||||
|
|
||||||
var subResponse = await subscriptionService.UpdateAsync(sub.Id, subUpdateOptions);
|
var subResponse = await subscriptionService.UpdateAsync(sub.Id, subUpdateOptions);
|
||||||
|
|
||||||
|
var invoiceService = new InvoiceService();
|
||||||
|
var invoice = await invoiceService.GetAsync(subResponse?.LatestInvoiceId, new InvoiceGetOptions());
|
||||||
|
if (invoice == null)
|
||||||
|
{
|
||||||
|
throw new BadRequestException("Unable to locate draft invoice for subscription update.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no amount due, invoice is autofinalized, we're done
|
||||||
|
if (invoice.AmountDue <= 0)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
string paymentIntentClientSecret = null;
|
string paymentIntentClientSecret = null;
|
||||||
if (updatedItemOptions.Quantity > 0)
|
if (updatedItemOptions.Quantity > 0)
|
||||||
{
|
{
|
||||||
@ -755,12 +768,12 @@ namespace Bit.Core.Services
|
|||||||
if (chargeNow)
|
if (chargeNow)
|
||||||
{
|
{
|
||||||
paymentIntentClientSecret = await PayInvoiceAfterSubscriptionChangeAsync(
|
paymentIntentClientSecret = await PayInvoiceAfterSubscriptionChangeAsync(
|
||||||
storableSubscriber, subResponse?.LatestInvoiceId);
|
storableSubscriber, invoice);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var invoiceService = new InvoiceService();
|
|
||||||
var invoice = await invoiceService.FinalizeInvoiceAsync(subResponse.LatestInvoiceId, new InvoiceFinalizeOptions
|
invoice = await invoiceService.FinalizeInvoiceAsync(subResponse.LatestInvoiceId, new InvoiceFinalizeOptions
|
||||||
{
|
{
|
||||||
AutoAdvance = false,
|
AutoAdvance = false,
|
||||||
});
|
});
|
||||||
@ -867,7 +880,7 @@ namespace Bit.Core.Services
|
|||||||
await customerService.DeleteAsync(subscriber.GatewayCustomerId);
|
await customerService.DeleteAsync(subscriber.GatewayCustomerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> PayInvoiceAfterSubscriptionChangeAsync(ISubscriber subscriber, string invoiceId)
|
public async Task<string> PayInvoiceAfterSubscriptionChangeAsync(ISubscriber subscriber, Invoice invoice)
|
||||||
{
|
{
|
||||||
var customerService = new CustomerService();
|
var customerService = new CustomerService();
|
||||||
var customerOptions = new CustomerGetOptions();
|
var customerOptions = new CustomerGetOptions();
|
||||||
@ -884,16 +897,10 @@ namespace Bit.Core.Services
|
|||||||
var invoiceService = new InvoiceService();
|
var invoiceService = new InvoiceService();
|
||||||
string paymentIntentClientSecret = null;
|
string paymentIntentClientSecret = null;
|
||||||
|
|
||||||
var invoice = await invoiceService.GetAsync(invoiceId, new InvoiceGetOptions());
|
|
||||||
if (invoice == null)
|
|
||||||
{
|
|
||||||
throw new BadRequestException("Unable to locate draft invoice for subscription update.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Invoice them and pay now instead of waiting until Stripe does this automatically.
|
// Invoice them and pay now instead of waiting until Stripe does this automatically.
|
||||||
|
|
||||||
string cardPaymentMethodId = null;
|
string cardPaymentMethodId = null;
|
||||||
if (invoice?.AmountDue > 0 && !customer.Metadata.ContainsKey("btCustomerId"))
|
if (!customer.Metadata.ContainsKey("btCustomerId"))
|
||||||
{
|
{
|
||||||
var hasDefaultCardPaymentMethod = customer.InvoiceSettings?.DefaultPaymentMethod?.Type == "card";
|
var hasDefaultCardPaymentMethod = customer.InvoiceSettings?.DefaultPaymentMethod?.Type == "card";
|
||||||
var hasDefaultValidSource = customer.DefaultSource != null &&
|
var hasDefaultValidSource = customer.DefaultSource != null &&
|
||||||
@ -934,8 +941,6 @@ namespace Bit.Core.Services
|
|||||||
{
|
{
|
||||||
PaymentMethod = cardPaymentMethodId,
|
PaymentMethod = cardPaymentMethodId,
|
||||||
};
|
};
|
||||||
if (invoice.AmountDue > 0)
|
|
||||||
{
|
|
||||||
if (customer?.Metadata?.ContainsKey("btCustomerId") ?? false)
|
if (customer?.Metadata?.ContainsKey("btCustomerId") ?? false)
|
||||||
{
|
{
|
||||||
invoicePayOptions.PaidOutOfBand = true;
|
invoicePayOptions.PaidOutOfBand = true;
|
||||||
@ -976,7 +981,6 @@ namespace Bit.Core.Services
|
|||||||
});
|
});
|
||||||
invoicePayOptions.PaidOutOfBand = true;
|
invoicePayOptions.PaidOutOfBand = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user