From 3f67d6a4176c0fc809f5540a66401ac410eb7d37 Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Thu, 7 Sep 2023 23:09:47 +0200 Subject: [PATCH] Add payment repository with query payment types function --- android/app/build.gradle.kts | 3 ++ .../payment/BillingPaymentAvailability.kt | 18 ++++++++++ .../repository/payment/PaymentAvailability.kt | 6 ++++ .../repository/payment/PaymentRepository.kt | 33 +++++++++++++++++++ .../lib/billing/model/QueryProductResult.kt | 2 +- 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/BillingPaymentAvailability.kt create mode 100644 android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/PaymentAvailability.kt create mode 100644 android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/PaymentRepository.kt diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index b1b1b5616a39..31286cf98c86 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -261,6 +261,9 @@ dependencies { implementation(Dependencies.Kotlin.stdlib) implementation(Dependencies.KotlinX.coroutinesAndroid) + // Billing + implementation(project(Dependencies.Mullvad.billingLib)) + // UI tooling implementation(Dependencies.Compose.uiToolingPreview) debugImplementation(Dependencies.Compose.uiTooling) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/BillingPaymentAvailability.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/BillingPaymentAvailability.kt new file mode 100644 index 000000000000..8b9a629f8aa3 --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/BillingPaymentAvailability.kt @@ -0,0 +1,18 @@ +package net.mullvad.mullvadvpn.repository.payment + +import net.mullvad.mullvadvpn.lib.billing.model.BillingException +import net.mullvad.mullvadvpn.lib.billing.model.BillingProduct + +sealed interface BillingPaymentAvailability { + data class ProductsAvailable(val products: List) : BillingPaymentAvailability + + data object ProductsUnavailable : BillingPaymentAvailability + + sealed interface Error : BillingPaymentAvailability { + data object BillingUnavailable : BillingPaymentAvailability + + data object ServiceUnavailable : BillingPaymentAvailability + + data class Other(val exception: BillingException) : BillingPaymentAvailability + } +} diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/PaymentAvailability.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/PaymentAvailability.kt new file mode 100644 index 000000000000..0a201e84312b --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/PaymentAvailability.kt @@ -0,0 +1,6 @@ +package net.mullvad.mullvadvpn.repository.payment + +data class PaymentAvailability( + val webPaymentAvailable: Boolean, + val billingPaymentAvailability: BillingPaymentAvailability +) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/PaymentRepository.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/PaymentRepository.kt new file mode 100644 index 000000000000..8c2e91dd31ed --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/payment/PaymentRepository.kt @@ -0,0 +1,33 @@ +package net.mullvad.mullvadvpn.repository.payment + +import net.mullvad.mullvadvpn.lib.billing.BillingRepository +import net.mullvad.mullvadvpn.lib.billing.model.QueryProductResult + +class PaymentRepository( + private val billingRepository: BillingRepository, + private val showWebPayment: Boolean +) { + suspend fun queryAvailablePaymentTypes(): PaymentAvailability = + PaymentAvailability( + webPaymentAvailable = showWebPayment, + billingPaymentAvailability = getBillingProducts() + ) + + private suspend fun getBillingProducts(): BillingPaymentAvailability = + when (val result = billingRepository.queryProducts()) { + is QueryProductResult.Ok -> + BillingPaymentAvailability.ProductsAvailable(products = result.products) + QueryProductResult.ItemUnavailable -> BillingPaymentAvailability.ProductsUnavailable + QueryProductResult.BillingUnavailable -> + BillingPaymentAvailability.Error.BillingUnavailable + QueryProductResult.ServiceUnavailable -> + BillingPaymentAvailability.Error.ServiceUnavailable + is QueryProductResult.Error -> + BillingPaymentAvailability.Error.Other(exception = result.exception) + } + + private fun fetchTransactionId(): String { + // Placeholder function + return "BOOPITOBOP" + } +} diff --git a/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/model/QueryProductResult.kt b/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/model/QueryProductResult.kt index 010eb0bed477..41dec812ffce 100644 --- a/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/model/QueryProductResult.kt +++ b/android/lib/billing/src/main/kotlin/net/mullvad/mullvadvpn/lib/billing/model/QueryProductResult.kt @@ -1,6 +1,6 @@ package net.mullvad.mullvadvpn.lib.billing.model -interface QueryProductResult { +sealed interface QueryProductResult { data class Ok(val products: List) : QueryProductResult data object BillingUnavailable: QueryProductResult