Skip to content

Commit

Permalink
Replace StripePaymentService with PremiumUserBillingService in Replac…
Browse files Browse the repository at this point in the history
…ePaymentMethodAsync call (#5350)
  • Loading branch information
amorask-bitwarden authored Jan 30, 2025
1 parent 23dce58 commit 443a147
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/Core/Billing/Services/IPremiumUserBillingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Core.Billing.Models.Sales;
using Bit.Core.Billing.Models;
using Bit.Core.Billing.Models.Sales;
using Bit.Core.Entities;

namespace Bit.Core.Billing.Services;
Expand Down Expand Up @@ -27,4 +28,9 @@ public interface IPremiumUserBillingService
/// </code>
/// </example>
Task Finalize(PremiumUserSale sale);

Task UpdatePaymentMethod(
User user,
TokenizedPaymentSource tokenizedPaymentSource,
TaxInformation taxInformation);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Bit.Core.Billing.Caches;
using Bit.Core.Billing.Constants;
using Bit.Core.Billing.Models;
using Bit.Core.Billing.Models.Sales;
using Bit.Core.Entities;
using Bit.Core.Enums;
Expand Down Expand Up @@ -58,6 +59,28 @@ public async Task Finalize(PremiumUserSale sale)
await userRepository.ReplaceAsync(user);
}

public async Task UpdatePaymentMethod(
User user,
TokenizedPaymentSource tokenizedPaymentSource,
TaxInformation taxInformation)
{
if (string.IsNullOrEmpty(user.GatewayCustomerId))
{
var customer = await CreateCustomerAsync(user,
new CustomerSetup { TokenizedPaymentSource = tokenizedPaymentSource, TaxInformation = taxInformation });

user.Gateway = GatewayType.Stripe;
user.GatewayCustomerId = customer.Id;

await userRepository.ReplaceAsync(user);
}
else
{
await subscriberService.UpdatePaymentSource(user, tokenizedPaymentSource);
await subscriberService.UpdateTaxInformation(user, taxInformation);
}
}

private async Task<Customer> CreateCustomerAsync(
User user,
CustomerSetup customerSetup)
Expand Down
11 changes: 6 additions & 5 deletions src/Core/Services/Implementations/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Bit.Core.Auth.Enums;
using Bit.Core.Auth.Models;
using Bit.Core.Auth.Models.Business.Tokenables;
using Bit.Core.Billing.Models;
using Bit.Core.Billing.Models.Sales;
using Bit.Core.Billing.Services;
using Bit.Core.Context;
Expand Down Expand Up @@ -1044,11 +1045,11 @@ public async Task ReplacePaymentMethodAsync(User user, string paymentToken, Paym
throw new BadRequestException("Invalid token.");
}

var updated = await _paymentService.UpdatePaymentMethodAsync(user, paymentMethodType, paymentToken, taxInfo: taxInfo);
if (updated)
{
await SaveUserAsync(user);
}
var tokenizedPaymentSource = new TokenizedPaymentSource(paymentMethodType, paymentToken);
var taxInformation = TaxInformation.From(taxInfo);

await _premiumUserBillingService.UpdatePaymentMethod(user, tokenizedPaymentSource, taxInformation);
await SaveUserAsync(user);
}

public async Task CancelPremiumAsync(User user, bool? endOfPeriod = null)
Expand Down

0 comments on commit 443a147

Please sign in to comment.