-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into billing/PM-16684/integrate-pricing-service
- Loading branch information
Showing
48 changed files
with
1,153 additions
and
437 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using Bit.Billing.Services; | ||
using Bit.Core.Repositories; | ||
using Quartz; | ||
using Stripe; | ||
|
||
namespace Bit.Billing.Jobs; | ||
|
||
public class SubscriptionCancellationJob( | ||
IStripeFacade stripeFacade, | ||
IOrganizationRepository organizationRepository) | ||
: IJob | ||
{ | ||
public async Task Execute(IJobExecutionContext context) | ||
{ | ||
var subscriptionId = context.MergedJobDataMap.GetString("subscriptionId"); | ||
var organizationId = new Guid(context.MergedJobDataMap.GetString("organizationId") ?? string.Empty); | ||
|
||
var organization = await organizationRepository.GetByIdAsync(organizationId); | ||
if (organization == null || organization.Enabled) | ||
{ | ||
// Organization was deleted or re-enabled by CS, skip cancellation | ||
return; | ||
} | ||
|
||
var subscription = await stripeFacade.GetSubscription(subscriptionId); | ||
if (subscription?.Status != "unpaid") | ||
{ | ||
// Subscription is no longer unpaid, skip cancellation | ||
return; | ||
} | ||
|
||
// Cancel the subscription | ||
await stripeFacade.CancelSubscription(subscriptionId, new SubscriptionCancelOptions()); | ||
|
||
// Void any open invoices | ||
var options = new InvoiceListOptions | ||
{ | ||
Status = "open", | ||
Subscription = subscriptionId, | ||
Limit = 100 | ||
}; | ||
var invoices = await stripeFacade.ListInvoices(options); | ||
foreach (var invoice in invoices) | ||
{ | ||
await stripeFacade.VoidInvoice(invoice.Id); | ||
} | ||
|
||
while (invoices.HasMore) | ||
{ | ||
options.StartingAfter = invoices.Data.Last().Id; | ||
invoices = await stripeFacade.ListInvoices(options); | ||
foreach (var invoice in invoices) | ||
{ | ||
await stripeFacade.VoidInvoice(invoice.Id); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.