diff --git a/subscriptions/subscriptions-impl/src/main/java/com/duckduckgo/subscriptions/impl/billing/BillingClientAdapter.kt b/subscriptions/subscriptions-impl/src/main/java/com/duckduckgo/subscriptions/impl/billing/BillingClientAdapter.kt index 1d6f7e823398..2d86fb960da5 100644 --- a/subscriptions/subscriptions-impl/src/main/java/com/duckduckgo/subscriptions/impl/billing/BillingClientAdapter.kt +++ b/subscriptions/subscriptions-impl/src/main/java/com/duckduckgo/subscriptions/impl/billing/BillingClientAdapter.kt @@ -88,6 +88,7 @@ enum class BillingError { ITEM_ALREADY_OWNED, ITEM_NOT_OWNED, NETWORK_ERROR, - UNKNOWN_ERROR, + UNKNOWN_ERROR, // for when billing returns something we don't understand + BILLING_CRASH_ERROR, // This is our own error ; } diff --git a/subscriptions/subscriptions-impl/src/main/java/com/duckduckgo/subscriptions/impl/billing/RealBillingClientAdapter.kt b/subscriptions/subscriptions-impl/src/main/java/com/duckduckgo/subscriptions/impl/billing/RealBillingClientAdapter.kt index 3b01e28d0e47..1febcd31b551 100644 --- a/subscriptions/subscriptions-impl/src/main/java/com/duckduckgo/subscriptions/impl/billing/RealBillingClientAdapter.kt +++ b/subscriptions/subscriptions-impl/src/main/java/com/duckduckgo/subscriptions/impl/billing/RealBillingClientAdapter.kt @@ -34,6 +34,7 @@ import com.android.billingclient.api.queryProductDetails import com.android.billingclient.api.queryPurchaseHistory import com.duckduckgo.common.utils.DispatcherProvider import com.duckduckgo.di.scopes.AppScope +import com.duckduckgo.subscriptions.impl.billing.BillingError.BILLING_CRASH_ERROR import com.duckduckgo.subscriptions.impl.billing.BillingInitResult.Failure import com.duckduckgo.subscriptions.impl.billing.BillingInitResult.Success import com.squareup.anvil.annotations.ContributesBinding @@ -44,6 +45,7 @@ import kotlin.coroutines.resume import kotlin.coroutines.suspendCoroutine import kotlinx.coroutines.withContext import logcat.LogPriority.WARN +import logcat.asLog import logcat.logcat @ContributesBinding(AppScope::class) @@ -97,25 +99,30 @@ class RealBillingClientAdapter @Inject constructor( } override suspend fun getSubscriptions(productIds: List): SubscriptionsResult { - val client = billingClient - if (client == null || !client.isReady) return SubscriptionsResult.Failure() - - val queryParams = QueryProductDetailsParams.newBuilder() - .setProductList( - productIds.map { productId -> - Product.newBuilder() - .setProductId(productId) - .setProductType(ProductType.SUBS) - .build() - }, - ) - .build() - - val (billingResult, productDetails) = client.queryProductDetails(queryParams) - - return when (billingResult.responseCode) { - BillingResponseCode.OK -> SubscriptionsResult.Success(productDetails.orEmpty()) - else -> SubscriptionsResult.Failure(billingResult.responseCode.toBillingError(), billingResult.debugMessage) + try { + val client = billingClient + if (client == null || !client.isReady) return SubscriptionsResult.Failure() + + val queryParams = QueryProductDetailsParams.newBuilder() + .setProductList( + productIds.map { productId -> + Product.newBuilder() + .setProductId(productId) + .setProductType(ProductType.SUBS) + .build() + }, + ) + .build() + + val (billingResult, productDetails) = client.queryProductDetails(queryParams) + + return when (billingResult.responseCode) { + BillingResponseCode.OK -> SubscriptionsResult.Success(productDetails.orEmpty()) + else -> SubscriptionsResult.Failure(billingResult.responseCode.toBillingError(), billingResult.debugMessage) + } + } catch (t: Throwable) { + logcat { "Error getting subscriptions: ${t.asLog()}" } + return SubscriptionsResult.Failure(billingError = BILLING_CRASH_ERROR) } }