-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[PM-32216] Create Stripe Checkout Session Endpoint #7246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sbrown-livefront
merged 33 commits into
main
from
billing/pm-32216/create-stripe-checkout-session-endpoint
Mar 27, 2026
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
e03233e
feat(stripe): add checkout session constants and settings
sbrown-livefront 6c409e8
feat(billing): integrate Stripe Checkout Session adapter
sbrown-livefront c5b7ee9
feat(billing): define premium checkout session DTOs
sbrown-livefront b18e242
feat(billing): implement CreatePremiumCheckoutSessionCommand
sbrown-livefront 2bbe6fc
feat(billing): add premium checkout session API endpoint
sbrown-livefront 0dd6fd6
test(billing): add premium checkout session tests
sbrown-livefront 0986f38
fix(billing): run dotnet format
sbrown-livefront 0692d6c
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront e5fb512
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront 67d7e3f
fix(billing): run dotnet format
sbrown-livefront 59e3ec5
refactor(billing): clarify Stripe session types in IStripeAdapter
sbrown-livefront c985187
refactor(billing): clarify Stripe session service and types in Stripe…
sbrown-livefront 6c6211d
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront 068f50d
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront b9eea98
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront 8fbcfaf
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront 0376871
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront 6b6b5db
refactor(StripeAdapter): remove duplicate billing portal session method
sbrown-livefront 824a455
style(premium): remove trailing comma from payment method types
sbrown-livefront 7a94cfb
refactor(billing): retrieve client version from context
sbrown-livefront 1be170a
refactor(premium): remove IUserService dependency from checkout command
sbrown-livefront 23561ce
refactor(premium): consolidate stripe customer creation logic
sbrown-livefront d45530f
fix(billing) run dotnet format
sbrown-livefront 17d39a0
feat(billing): add user ID to premium checkout session subscription
sbrown-livefront 87672a6
test(billing): verify user ID is set in premium checkout session meta…
sbrown-livefront 9bad6d5
test(billing): handle billing exception during stripe customer creation
sbrown-livefront 31cdaef
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront 08137a3
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront 580279c
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront d911eaf
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront 2291fcb
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront 0e58591
[PM-32218] Create Session Complete Handler (#7283)
sbrown-livefront 804c3b6
Merge branch 'main' into billing/pm-32216/create-stripe-checkout-sess…
sbrown-livefront File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
21 changes: 21 additions & 0 deletions
21
src/Api/Billing/Models/Requests/Premium/CreatePremiumCheckoutSessionRequest.cs
This file contains hidden or 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,21 @@ | ||
| using System.ComponentModel.DataAnnotations; | ||
| using Bit.Core.Billing.Constants; | ||
|
|
||
| namespace Bit.Api.Billing.Models.Requests.Premium; | ||
|
|
||
| public class CreatePremiumCheckoutSessionRequest : IValidatableObject | ||
| { | ||
| [Required] | ||
| public required string Platform { get; set; } | ||
|
|
||
| public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
| { | ||
| if (Platform is not (StripeConstants.CheckoutSession.Platforms.Ios | ||
| or StripeConstants.CheckoutSession.Platforms.Android)) | ||
| { | ||
| yield return new ValidationResult( | ||
| $"Platform must be '{StripeConstants.CheckoutSession.Platforms.Ios}' or '{StripeConstants.CheckoutSession.Platforms.Android}'.", | ||
| [nameof(Platform)]); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
95 changes: 95 additions & 0 deletions
95
src/Billing/Services/Implementations/CheckoutSessionCompletedHandler.cs
This file contains hidden or 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,95 @@ | ||
| using Bit.Core.Billing.Extensions; | ||
| using Bit.Core.Billing.Pricing; | ||
| using Bit.Core.Billing.Services; | ||
| using Bit.Core.Enums; | ||
| using Bit.Core.Repositories; | ||
| using Bit.Core.Utilities; | ||
| using Stripe; | ||
|
|
||
| namespace Bit.Billing.Services.Implementations; | ||
|
|
||
| public class CheckoutSessionCompletedHandler( | ||
| IStripeEventService stripeEventService, | ||
| IStripeEventUtilityService stripeEventUtilityService, | ||
| IStripeAdapter stripeAdapter, | ||
| IUserRepository userRepository, | ||
| IPricingClient pricingClient, | ||
| IPushNotificationAdapter pushNotificationAdapter, | ||
| ILogger<CheckoutSessionCompletedHandler> logger) | ||
| : ICheckoutSessionCompletedHandler | ||
| { | ||
| public async Task HandleAsync(Event parsedEvent) | ||
| { | ||
| var session = await stripeEventService.GetCheckoutSession(parsedEvent, true, ["subscription"]); | ||
| var subscription = session.Subscription; | ||
|
|
||
| if (subscription is null) | ||
| { | ||
| logger.LogError("Checkout Session {SessionId} has no subscription ID", session.Id); | ||
| return; | ||
| } | ||
|
|
||
| var (_, userId, _) = stripeEventUtilityService.GetIdsFromMetadata(subscription.Metadata); | ||
| if (!userId.HasValue) | ||
| { | ||
| logger.LogError("No userId found in metadata for subscription {SubscriptionId}", subscription.Id); | ||
| return; | ||
| } | ||
|
|
||
| var user = await userRepository.GetByIdAsync(userId.Value); | ||
| if (user is null) | ||
| { | ||
| logger.LogError("User {UserId} not found for subscription {SubscriptionId}", userId.Value, subscription.Id); | ||
| return; | ||
| } | ||
|
|
||
| if (user.Premium) | ||
| { | ||
| logger.LogError("User {UserId} is already premium for subscription {SubscriptionId}", user.Id, subscription.Id); | ||
| return; | ||
| } | ||
|
|
||
| var premiumPlan = await pricingClient.GetAvailablePremiumPlan(); | ||
|
|
||
| // if the subscription does not contain the premium seat, this is not a premium subscription upgrade | ||
| if (subscription.Items.All(i => i.Price.Id != premiumPlan.Seat.StripePriceId)) | ||
| { | ||
| logger.LogError("Subscription {SubscriptionId} does not contain premium seat", subscription.Id); | ||
| return; | ||
| } | ||
|
|
||
| user.Premium = true; | ||
| user.GatewaySubscriptionId = subscription.Id; | ||
| user.Gateway = GatewayType.Stripe; | ||
| user.PremiumExpirationDate = subscription.GetCurrentPeriodEnd(); | ||
| user.MaxStorageGb = (short)premiumPlan.Storage.Provided; | ||
| user.LicenseKey = string.IsNullOrWhiteSpace(user.LicenseKey) ? CoreHelpers.SecureRandomString(20) : user.LicenseKey; | ||
| user.RevisionDate = DateTime.UtcNow; | ||
|
|
||
| await userRepository.ReplaceAsync(user); | ||
| await pushNotificationAdapter.NotifyPremiumStatusChangedAsync(user); | ||
| await UpdateDefaultPaymentMethodAsync(subscription.DefaultPaymentMethodId, session.CustomerId, subscription.Id); | ||
| } | ||
|
|
||
| private async Task UpdateDefaultPaymentMethodAsync(string? defaultPaymentMethodId, string customerId, string subscriptionId) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(defaultPaymentMethodId)) | ||
| { | ||
| logger.LogWarning("No default payment method found for customer {CustomerId}", customerId); | ||
| return; | ||
| } | ||
|
|
||
| await stripeAdapter.UpdateCustomerAsync(customerId, new CustomerUpdateOptions | ||
| { | ||
| InvoiceSettings = new CustomerInvoiceSettingsOptions | ||
| { | ||
| DefaultPaymentMethod = defaultPaymentMethodId | ||
| } | ||
| }); | ||
|
|
||
| await stripeAdapter.UpdateSubscriptionAsync(subscriptionId, new SubscriptionUpdateOptions | ||
| { | ||
| DefaultPaymentMethod = string.Empty | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
4 changes: 4 additions & 0 deletions
4
src/Core/Billing/Models/Api/Response/Premium/PremiumCheckoutSessionResponseModel.cs
This file contains hidden or 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,4 @@ | ||
| | ||
| namespace Bit.Core.Billing.Models.Api.Response.Premium; | ||
|
|
||
| public record PremiumCheckoutSessionResponseModel(string CheckoutSessionUrl); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.