diff options
| author | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2024-06-17 16:06:50 +0200 |
|---|---|---|
| committer | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2024-06-19 10:50:21 +0200 |
| commit | f31afd8ee3861d50d02a099afbbf181338072276 (patch) | |
| tree | 75ca652b057235d8af85e59a47f606f4799bcdb3 /android/lib/billing/src | |
| parent | 88c5d622d797faf99528f79a0907b101fa8f4cae (diff) | |
| download | mullvadvpn-f31afd8ee3861d50d02a099afbbf181338072276.tar.xz mullvadvpn-f31afd8ee3861d50d02a099afbbf181338072276.zip | |
Replace retry with exponential backoff with arrow schedule
Also clean up the code related to play purchase verification
Diffstat (limited to 'android/lib/billing/src')
| -rw-r--r-- | android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingPaymentRepository.kt | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingPaymentRepository.kt b/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingPaymentRepository.kt index 8b3ad66171..b526a10032 100644 --- a/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingPaymentRepository.kt +++ b/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingPaymentRepository.kt @@ -1,6 +1,9 @@ package net.mullvad.mullvadvpn.lib.billing import android.app.Activity +import arrow.core.Either +import arrow.core.raise.either +import arrow.core.raise.ensure import com.android.billingclient.api.BillingClient.BillingResponseCode import com.android.billingclient.api.Purchase import kotlinx.coroutines.flow.Flow @@ -22,6 +25,7 @@ import net.mullvad.mullvadvpn.lib.payment.ProductIds import net.mullvad.mullvadvpn.lib.payment.model.PaymentAvailability import net.mullvad.mullvadvpn.lib.payment.model.ProductId import net.mullvad.mullvadvpn.lib.payment.model.PurchaseResult +import net.mullvad.mullvadvpn.lib.payment.model.VerificationError import net.mullvad.mullvadvpn.lib.payment.model.VerificationResult class BillingPaymentRepository( @@ -129,28 +133,19 @@ class BillingPaymentRepository( } } - override fun verifyPurchases(): Flow<VerificationResult> = flow { - emit(VerificationResult.FetchingUnfinishedPurchases) + override suspend fun verifyPurchases(): Either<VerificationError, VerificationResult> = either { val purchasesResult = billingRepository.queryPurchases() - when (purchasesResult.responseCode()) { - BillingResponseCode.OK -> { - val purchases = purchasesResult.nonPendingPurchases() - if (purchases.isNotEmpty()) { - emit(VerificationResult.VerificationStarted) - emit( - verifyPurchase(purchases.first()) - .fold( - { VerificationResult.Error.VerificationError(null) }, - { VerificationResult.Success } - ) - ) - } else { - emit(VerificationResult.NothingToVerify) - } - } - else -> - emit(VerificationResult.Error.BillingError(purchasesResult.toBillingException())) + ensure(purchasesResult.responseCode() == BillingResponseCode.OK) { + VerificationError.BillingError(purchasesResult.toBillingException()) + } + val purchases = purchasesResult.nonPendingPurchases() + if (purchases.isEmpty()) { + return@either VerificationResult.NothingToVerify } + verifyPurchase(purchases.first()) + .mapLeft { VerificationError.PlayVerificationError } + .map { VerificationResult.Success } + .bind() } private suspend fun initialisePurchase() = playPurchaseRepository.initializePlayPurchase() |
