Skip to content

Commit

Permalink
Add support for other platforms subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
vegaro committed Jan 17, 2025
1 parent d856d55 commit 6b0ae7f
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ data class CustomerCenterConfigData(
@SerialName("purchases_not_recovered")
PURCHASES_NOT_RECOVERED,

@SerialName("manage_subscription")
MANAGE_SUBSCRIPTION,

@SerialName("you_have_promo")
YOU_HAVE_PROMO,

Expand Down Expand Up @@ -199,6 +202,7 @@ data class CustomerCenterConfigData(
PURCHASES_NOT_RECOVERED ->
"We couldn't find any additional purchases under this account. " +
"Contact support for assistance if you think this is an error."
MANAGE_SUBSCRIPTION -> "Manage your subscription"
YOU_HAVE_PROMO -> "You've been granted a subscription that doesn’t renew"
YOU_HAVE_LIFETIME -> "Your active lifetime subscription"
WEB_SUBSCRIPTION_MANAGE ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ internal fun InternalCustomerCenter(

is CustomerCenterAction.DismissRestoreDialog -> viewModel.dismissRestoreDialog()
is CustomerCenterAction.ContactSupport -> viewModel.contactSupport(context, action.email)
is CustomerCenterAction.OpenURL -> viewModel.openURL(context, action.url)
is CustomerCenterAction.NavigationButtonPressed -> {
val buttonType = state.navigationButtonType
viewModel.onNavigationButtonPressed()
Expand Down Expand Up @@ -227,9 +228,8 @@ private fun MainScreen(
screen = managementScreen,
localization = configuration.localization,
purchaseInformation = state.purchaseInformation,
onPathButtonPress = { path ->
onAction(CustomerCenterAction.PathButtonPressed(path))
},
support = configuration.support,
onAction = onAction,
)
} ?: run {
// Handle missing management screen
Expand All @@ -240,9 +240,8 @@ private fun MainScreen(
ManageSubscriptionsView(
screen = noActiveScreen,
localization = configuration.localization,
onPathButtonPress = { path ->
onAction(CustomerCenterAction.PathButtonPressed(path))
},
support = configuration.support,
onAction = onAction,
)
} ?: run {
// Fallback with a restore button
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.revenuecat.purchases.ui.revenuecatui.customercenter

import android.net.Uri
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand Down Expand Up @@ -220,6 +221,7 @@ private class SubscriptionInformationProvider : PreviewParameterProvider<Purchas
ExpirationOrRenewal.Date.DateString("June 1st, 2024"),
),
store = Store.PLAY_STORE,
managementURL = Uri.parse("https://play.google.com/store/account/subscriptions"),
),
PurchaseInformation(
title = "Basic",
Expand All @@ -233,6 +235,7 @@ private class SubscriptionInformationProvider : PreviewParameterProvider<Purchas
ExpirationOrRenewal.Date.DateString("June 1st, 2024"),
),
store = Store.PLAY_STORE,
managementURL = Uri.parse("https://play.google.com/store/account/subscriptions"),
),
PurchaseInformation(
title = "Basic",
Expand All @@ -246,6 +249,7 @@ private class SubscriptionInformationProvider : PreviewParameterProvider<Purchas
ExpirationOrRenewal.Date.DateString("June 1st, 2024"),
),
store = Store.PLAY_STORE,
managementURL = Uri.parse("https://play.google.com/store/account/subscriptions"),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.revenuecat.purchases.ui.revenuecatui.customercenter.actions

import android.net.Uri
import com.revenuecat.purchases.ExperimentalPreviewRevenueCatPurchasesAPI
import com.revenuecat.purchases.customercenter.CustomerCenterConfigData

Expand All @@ -9,5 +10,6 @@ internal sealed class CustomerCenterAction {
object PerformRestore : CustomerCenterAction()
object DismissRestoreDialog : CustomerCenterAction()
data class ContactSupport(val email: String) : CustomerCenterAction()
data class OpenURL(val url: Uri) : CustomerCenterAction()
object NavigationButtonPressed : CustomerCenterAction()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.revenuecat.purchases.ui.revenuecatui.customercenter.data

import android.net.Uri
import com.revenuecat.purchases.ExperimentalPreviewRevenueCatPurchasesAPI
import com.revenuecat.purchases.Store
import com.revenuecat.purchases.customercenter.CustomerCenterConfigData
Expand Down Expand Up @@ -122,6 +123,7 @@ internal object CustomerCenterConfigTestData {
),
productIdentifier = "monthly_product_id",
store = Store.PLAY_STORE,
managementURL = Uri.parse("https://play.google.com/store/account/subscriptions"),
)

val purchaseInformationYearlyExpiring = PurchaseInformation(
Expand All @@ -136,5 +138,6 @@ internal object CustomerCenterConfigTestData {
),
productIdentifier = "yearly_product_id",
store = Store.PLAY_STORE,
managementURL = Uri.parse("https://play.google.com/store/account/subscriptions"),
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.revenuecat.purchases.ui.revenuecatui.customercenter.data

import android.net.Uri
import com.revenuecat.purchases.EntitlementInfo
import com.revenuecat.purchases.Store
import com.revenuecat.purchases.models.StoreProduct
Expand All @@ -18,12 +19,14 @@ internal class PurchaseInformation(
val expirationOrRenewal: ExpirationOrRenewal?,
val productIdentifier: String,
val store: Store,
val managementURL: Uri?,
) {

constructor(
entitlementInfo: EntitlementInfo? = null,
subscribedProduct: StoreProduct? = null,
transaction: TransactionDetails,
managementURL: Uri?,
dateFormatter: DateFormatter = DefaultDateFormatter(),
locale: Locale,
) : this(
Expand Down Expand Up @@ -71,6 +74,7 @@ internal class PurchaseInformation(
} else {
subscribedProduct?.let { PriceDetails.Paid(it.price.formatted) } ?: PriceDetails.Unknown
},
managementURL = managementURL,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal interface CustomerCenterViewModel {
fun contactSupport(context: Context, supportEmail: String)
fun onNavigationButtonPressed()
suspend fun loadCustomerCenter()
fun openURL(context: Context, url: Uri)
}

internal sealed class TransactionDetails(
Expand Down Expand Up @@ -203,7 +204,13 @@ internal class CustomerCenterViewModelImpl(
val entitlement = customerInfo.entitlements.all.values
.firstOrNull { it.productIdentifier == activeTransactionDetails.productIdentifier }

return createPurchaseInformation(activeTransactionDetails, entitlement, dateFormatter, locale)
return createPurchaseInformation(
activeTransactionDetails,
entitlement,
customerInfo.managementURL,
dateFormatter,
locale,
)
} else {
Logger.w("Could not find subscription information")
}
Expand Down Expand Up @@ -248,6 +255,7 @@ internal class CustomerCenterViewModelImpl(
private suspend fun createPurchaseInformation(
transaction: TransactionDetails,
entitlement: EntitlementInfo?,
managementURL: Uri?,
dateFormatter: DateFormatter,
locale: Locale,
): PurchaseInformation {
Expand All @@ -268,6 +276,7 @@ internal class CustomerCenterViewModelImpl(
entitlementInfo = entitlement,
subscribedProduct = product,
transaction = transaction,
managementURL = managementURL,
dateFormatter = dateFormatter,
locale = locale,
)
Expand All @@ -282,6 +291,16 @@ internal class CustomerCenterViewModelImpl(
context.startActivity(Intent.createChooser(intent, "Contact Support"))
}

@SuppressWarnings("ForbiddenComment")
override fun openURL(context: Context, url: Uri) {
// TODO: Handle In-App Browser
try {
context.startActivity(Intent(Intent.ACTION_VIEW, url))
} catch (e: ActivityNotFoundException) {
Logger.e("Error opening URL", e)
}
}

override fun onNavigationButtonPressed() {
_state.update { currentState ->
if (currentState is CustomerCenterState.Success &&
Expand Down
Loading

0 comments on commit 6b0ae7f

Please sign in to comment.