-
Notifications
You must be signed in to change notification settings - Fork 948
[PM-33508] feat: Add AuthenticatedBillingApi and BillingService network layer #6668
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
SaintPatrck
merged 5 commits into
main
from
premium-upgrade/PM-33508-billing-api-service
Mar 18, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c9326c4
[PM-33508] feat: Add AuthenticatedBillingApi and BillingService netwoβ¦
SaintPatrck 8887374
Add returnUrl request body to portal session endpoint
SaintPatrck cd1756e
Rename checkoutUrl to checkoutSessionUrl in CheckoutSessionResponseJson
SaintPatrck 8bce796
Remove returnUrl from portal session endpoint
SaintPatrck b1a6ab0
Move PLATFORM constant definition
SaintPatrck 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
Some comments aren't visible on the classic Files Changed page.
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
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
28 changes: 28 additions & 0 deletions
28
network/src/main/kotlin/com/bitwarden/network/api/AuthenticatedBillingApi.kt
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,28 @@ | ||
| package com.bitwarden.network.api | ||
|
|
||
| import com.bitwarden.network.model.CheckoutSessionRequestJson | ||
| import com.bitwarden.network.model.CheckoutSessionResponseJson | ||
| import com.bitwarden.network.model.NetworkResult | ||
| import com.bitwarden.network.model.PortalUrlResponseJson | ||
| import retrofit2.http.Body | ||
| import retrofit2.http.POST | ||
|
|
||
| /** | ||
| * Defines raw calls under the /account/billing API with authentication applied. | ||
| */ | ||
| internal interface AuthenticatedBillingApi { | ||
|
|
||
| /** | ||
| * Creates a Stripe checkout session for premium upgrade. | ||
| */ | ||
| @POST("/account/billing/vnext/premium/checkout") | ||
| suspend fun createCheckoutSession( | ||
| @Body body: CheckoutSessionRequestJson, | ||
| ): NetworkResult<CheckoutSessionResponseJson> | ||
|
|
||
| /** | ||
| * Creates a Stripe customer portal session for managing the premium subscription. | ||
| */ | ||
| @POST("/account/billing/vnext/portal-session") | ||
| suspend fun getPortalUrl(): NetworkResult<PortalUrlResponseJson> | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
network/src/main/kotlin/com/bitwarden/network/model/CheckoutSessionRequestJson.kt
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,15 @@ | ||
| package com.bitwarden.network.model | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| /** | ||
| * Request object for creating a Stripe checkout session for premium upgrade. | ||
| * | ||
| * @property platform The platform identifier (e.g., "android" or "ios"). | ||
| */ | ||
| @Serializable | ||
| data class CheckoutSessionRequestJson( | ||
| @SerialName("platform") | ||
| val platform: String, | ||
| ) |
15 changes: 15 additions & 0 deletions
15
network/src/main/kotlin/com/bitwarden/network/model/CheckoutSessionResponseJson.kt
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,15 @@ | ||
| package com.bitwarden.network.model | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| /** | ||
| * Response object returned when creating a premium checkout session. | ||
| * | ||
| * @property checkoutSessionUrl The Stripe checkout URL for premium upgrade. | ||
| */ | ||
| @Serializable | ||
| data class CheckoutSessionResponseJson( | ||
| @SerialName("checkoutSessionUrl") | ||
| val checkoutSessionUrl: String, | ||
| ) |
15 changes: 15 additions & 0 deletions
15
network/src/main/kotlin/com/bitwarden/network/model/PortalUrlResponseJson.kt
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,15 @@ | ||
| package com.bitwarden.network.model | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| /** | ||
| * Response object returned when requesting a Stripe customer portal session. | ||
| * | ||
| * @property url The Stripe customer portal URL. | ||
| */ | ||
| @Serializable | ||
| data class PortalUrlResponseJson( | ||
| @SerialName("url") | ||
| val url: String, | ||
| ) |
20 changes: 20 additions & 0 deletions
20
network/src/main/kotlin/com/bitwarden/network/service/BillingService.kt
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,20 @@ | ||
| package com.bitwarden.network.service | ||
|
|
||
| import com.bitwarden.network.model.CheckoutSessionResponseJson | ||
| import com.bitwarden.network.model.PortalUrlResponseJson | ||
|
|
||
| /** | ||
| * Provides an API for interacting with the billing endpoints. | ||
| */ | ||
| interface BillingService { | ||
|
|
||
| /** | ||
| * Creates a Stripe checkout session for premium upgrade. | ||
| */ | ||
| suspend fun createCheckoutSession(): Result<CheckoutSessionResponseJson> | ||
|
|
||
| /** | ||
| * Creates a Stripe customer portal session for managing the premium subscription. | ||
| */ | ||
| suspend fun getPortalUrl(): Result<PortalUrlResponseJson> | ||
| } |
29 changes: 29 additions & 0 deletions
29
network/src/main/kotlin/com/bitwarden/network/service/BillingServiceImpl.kt
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,29 @@ | ||
| package com.bitwarden.network.service | ||
|
|
||
| import com.bitwarden.network.api.AuthenticatedBillingApi | ||
| import com.bitwarden.network.model.CheckoutSessionRequestJson | ||
| import com.bitwarden.network.model.CheckoutSessionResponseJson | ||
| import com.bitwarden.network.model.PortalUrlResponseJson | ||
| import com.bitwarden.network.util.toResult | ||
|
|
||
| private const val PLATFORM = "android" | ||
|
|
||
| /** | ||
| * The default implementation of the [BillingService]. | ||
| */ | ||
| internal class BillingServiceImpl( | ||
| private val authenticatedBillingApi: AuthenticatedBillingApi, | ||
| ) : BillingService { | ||
|
|
||
| override suspend fun createCheckoutSession(): Result<CheckoutSessionResponseJson> = | ||
| authenticatedBillingApi | ||
| .createCheckoutSession( | ||
| body = CheckoutSessionRequestJson(platform = PLATFORM), | ||
| ) | ||
| .toResult() | ||
|
|
||
| override suspend fun getPortalUrl(): Result<PortalUrlResponseJson> = | ||
| authenticatedBillingApi | ||
| .getPortalUrl() | ||
| .toResult() | ||
| } |
79 changes: 79 additions & 0 deletions
79
network/src/test/kotlin/com/bitwarden/network/service/BillingServiceTest.kt
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,79 @@ | ||
| package com.bitwarden.network.service | ||
|
|
||
| import com.bitwarden.core.data.util.asSuccess | ||
| import com.bitwarden.network.api.AuthenticatedBillingApi | ||
| import com.bitwarden.network.base.BaseServiceTest | ||
| import com.bitwarden.network.model.CheckoutSessionResponseJson | ||
| import com.bitwarden.network.model.PortalUrlResponseJson | ||
| import kotlinx.coroutines.test.runTest | ||
| import okhttp3.mockwebserver.MockResponse | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.Test | ||
| import retrofit2.create | ||
|
|
||
| class BillingServiceTest : BaseServiceTest() { | ||
|
|
||
| private val billingApi: AuthenticatedBillingApi = retrofit.create() | ||
| private val service = BillingServiceImpl( | ||
| authenticatedBillingApi = billingApi, | ||
| ) | ||
|
|
||
| @Test | ||
| fun `createCheckoutSession when response is Failure should return Failure`() = | ||
| runTest { | ||
| val response = MockResponse().setResponseCode(400) | ||
| server.enqueue(response) | ||
| val actual = service.createCheckoutSession() | ||
| assertTrue(actual.isFailure) | ||
| } | ||
|
|
||
| @Test | ||
| fun `createCheckoutSession when response is Success should return Success`() = | ||
| runTest { | ||
| val response = MockResponse() | ||
| .setBody(CHECKOUT_SESSION_RESPONSE_JSON) | ||
| .setResponseCode(200) | ||
| server.enqueue(response) | ||
| val actual = service.createCheckoutSession() | ||
| assertEquals(CHECKOUT_SESSION_RESPONSE.asSuccess(), actual) | ||
| } | ||
|
|
||
| @Test | ||
| fun `getPortalUrl when response is Failure should return Failure`() = runTest { | ||
| val response = MockResponse().setResponseCode(400) | ||
| server.enqueue(response) | ||
| val actual = service.getPortalUrl() | ||
| assertTrue(actual.isFailure) | ||
| } | ||
|
|
||
| @Test | ||
| fun `getPortalUrl when response is Success should return Success`() = runTest { | ||
| val response = MockResponse() | ||
| .setBody(PORTAL_URL_RESPONSE_JSON) | ||
| .setResponseCode(200) | ||
| server.enqueue(response) | ||
| val actual = service.getPortalUrl() | ||
| assertEquals(PORTAL_URL_RESPONSE.asSuccess(), actual) | ||
| } | ||
| } | ||
|
|
||
| private const val CHECKOUT_SESSION_RESPONSE_JSON = """ | ||
| { | ||
| "checkoutSessionUrl": "https://checkout.stripe.com/c/pay/test_session_123" | ||
| } | ||
| """ | ||
|
|
||
| private val CHECKOUT_SESSION_RESPONSE = CheckoutSessionResponseJson( | ||
| checkoutSessionUrl = "https://checkout.stripe.com/c/pay/test_session_123", | ||
| ) | ||
|
|
||
| private const val PORTAL_URL_RESPONSE_JSON = """ | ||
| { | ||
| "url": "https://billing.stripe.com/p/session/test_portal_456" | ||
| } | ||
| """ | ||
|
|
||
| private val PORTAL_URL_RESPONSE = PortalUrlResponseJson( | ||
| url = "https://billing.stripe.com/p/session/test_portal_456", | ||
| ) |
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.