summaryrefslogtreecommitdiffhomepage
path: root/android/lib
diff options
context:
space:
mode:
authorJonatan Rhodin <jonatan.rhodin@mullvad.net>2024-06-17 16:06:50 +0200
committerJonatan Rhodin <jonatan.rhodin@mullvad.net>2024-06-19 10:50:21 +0200
commitf31afd8ee3861d50d02a099afbbf181338072276 (patch)
tree75ca652b057235d8af85e59a47f606f4799bcdb3 /android/lib
parent88c5d622d797faf99528f79a0907b101fa8f4cae (diff)
downloadmullvadvpn-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')
-rw-r--r--android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/BillingPaymentRepository.kt35
-rw-r--r--android/lib/payment/build.gradle.kts1
-rw-r--r--android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/PaymentRepository.kt4
-rw-r--r--android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/model/VerificationError.kt7
-rw-r--r--android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/model/VerificationResult.kt14
5 files changed, 27 insertions, 34 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()
diff --git a/android/lib/payment/build.gradle.kts b/android/lib/payment/build.gradle.kts
index 23f945b4f9..892ce21c75 100644
--- a/android/lib/payment/build.gradle.kts
+++ b/android/lib/payment/build.gradle.kts
@@ -39,6 +39,7 @@ android {
}
dependencies {
+ implementation(Dependencies.Arrow.core)
implementation(Dependencies.Kotlin.stdlib)
implementation(Dependencies.KotlinX.coroutinesAndroid)
}
diff --git a/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/PaymentRepository.kt b/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/PaymentRepository.kt
index 73fd0c061d..0f076eab74 100644
--- a/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/PaymentRepository.kt
+++ b/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/PaymentRepository.kt
@@ -1,10 +1,12 @@
package net.mullvad.mullvadvpn.lib.payment
import android.app.Activity
+import arrow.core.Either
import kotlinx.coroutines.flow.Flow
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
interface PaymentRepository {
@@ -14,7 +16,7 @@ interface PaymentRepository {
activityProvider: () -> Activity
): Flow<PurchaseResult>
- fun verifyPurchases(): Flow<VerificationResult>
+ suspend fun verifyPurchases(): Either<VerificationError, VerificationResult>
fun queryPaymentAvailability(): Flow<PaymentAvailability>
}
diff --git a/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/model/VerificationError.kt b/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/model/VerificationError.kt
new file mode 100644
index 0000000000..51cf8d1d28
--- /dev/null
+++ b/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/model/VerificationError.kt
@@ -0,0 +1,7 @@
+package net.mullvad.mullvadvpn.lib.payment.model
+
+sealed interface VerificationError {
+ data class BillingError(val exception: Throwable) : VerificationError
+
+ data object PlayVerificationError : VerificationError
+}
diff --git a/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/model/VerificationResult.kt b/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/model/VerificationResult.kt
index 725ea0af68..8cf971194d 100644
--- a/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/model/VerificationResult.kt
+++ b/android/lib/payment/src/main/kotlin/net/mullvad/mullvadvpn/lib/payment/model/VerificationResult.kt
@@ -1,19 +1,7 @@
package net.mullvad.mullvadvpn.lib.payment.model
-sealed interface VerificationResult {
- data object FetchingUnfinishedPurchases : VerificationResult
-
- data object VerificationStarted : VerificationResult
-
- // No verification was needed as there is no purchases to verify
+interface VerificationResult {
data object NothingToVerify : VerificationResult
data object Success : VerificationResult
-
- // Generic error, add more cases as needed
- sealed interface Error : VerificationResult {
- data class BillingError(val exception: Throwable?) : Error
-
- data class VerificationError(val exception: Throwable?) : Error
- }
}