Skip to content

Commit

Permalink
Update InvoiceUpcoming handler to send multiple emails
Browse files Browse the repository at this point in the history
  • Loading branch information
amorask-bitwarden committed Oct 3, 2023
1 parent fdb5c4f commit c972048
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions src/Billing/Controllers/StripeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,49 +208,64 @@ await _userService.UpdatePremiumExpirationAsync(userId,
}
else if (parsedEvent.Type.Equals(HandledStripeWebhook.UpcomingInvoice))
{
var invoice = await _stripeEventService.GetInvoice(parsedEvent);
var subscriptionService = new SubscriptionService();
var subscription = await subscriptionService.GetAsync(invoice.SubscriptionId);
if (subscription == null)
var invoice = await _stripeEventService.GetInvoice(parsedEvent, true, new List<string> { "subscription" });

if (invoice.Subscription == null)
{
throw new Exception("Invoice subscription is null. " + invoice.Id);
throw new Exception(
$"Received null Subscription from Stripe for ID '{invoice.SubscriptionId}' while processing Event with ID '{parsedEvent.Id}'");
}

subscription = await VerifyCorrectTaxRateForCharge(invoice, subscription);
var updatedSubscription = await VerifyCorrectTaxRateForCharge(invoice, invoice.Subscription);

string email = null;
var ids = GetIdsFromMetaData(subscription.Metadata);
// org
if (ids.Item1.HasValue)
var (organizationId, userId) = GetIdsFromMetaData(updatedSubscription.Metadata);

var invoiceLineItemDescriptions = invoice.Lines.Select(i => i.Description).ToList();

async Task SendEmail(IEnumerable<string> emails)
{
var validEmails = emails.Where(e => !string.IsNullOrEmpty(e));

if (invoice.NextPaymentAttempt.HasValue)
{
await _mailService.SendInvoiceUpcoming(
validEmails,
invoice.AmountDue / 100M,
invoice.NextPaymentAttempt.Value,
invoiceLineItemDescriptions,
true);
}
}

if (organizationId.HasValue)
{
// sponsored org
if (IsSponsoredSubscription(subscription))
if (IsSponsoredSubscription(updatedSubscription))
{
await _validateSponsorshipCommand.ValidateSponsorshipAsync(ids.Item1.Value);
await _validateSponsorshipCommand.ValidateSponsorshipAsync(organizationId.Value);
}

var org = await _organizationRepository.GetByIdAsync(ids.Item1.Value);
if (org != null && OrgPlanForInvoiceNotifications(org))
var organization = await _organizationRepository.GetByIdAsync(organizationId.Value);

if (organization == null || !OrgPlanForInvoiceNotifications(organization))
{
email = org.BillingEmail;
return new OkResult();
}

await SendEmail(new List<string> { organization.BillingEmail });

var ownerEmails = await _organizationRepository.GetOwnerEmailAddressesById(organization.Id);

await SendEmail(ownerEmails);
}
// user
else if (ids.Item2.HasValue)
else if (userId.HasValue)
{
var user = await _userService.GetUserByIdAsync(ids.Item2.Value);
var user = await _userService.GetUserByIdAsync(userId.Value);

if (user.Premium)
{
email = user.Email;
await SendEmail(new List<string> { user.Email });
}
}

if (!string.IsNullOrWhiteSpace(email) && invoice.NextPaymentAttempt.HasValue)
{
var items = invoice.Lines.Select(i => i.Description).ToList();
await _mailService.SendInvoiceUpcomingAsync(email, invoice.AmountDue / 100M,
invoice.NextPaymentAttempt.Value, items, true);
}
}
else if (parsedEvent.Type.Equals(HandledStripeWebhook.ChargeSucceeded))
{
Expand Down

0 comments on commit c972048

Please sign in to comment.