Skip to content

Commit

Permalink
Add purchase billing product function
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Sep 12, 2023
1 parent 475153b commit cf799d8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package net.mullvad.mullvadvpn.repository.payment

import kotlinx.coroutines.flow.singleOrNull
import net.mullvad.mullvadvpn.lib.billing.BillingRepository
import net.mullvad.mullvadvpn.lib.billing.model.BillingProduct
import net.mullvad.mullvadvpn.lib.billing.model.PurchaseEvent
import net.mullvad.mullvadvpn.lib.billing.model.PurchaseFlowResult
import net.mullvad.mullvadvpn.lib.billing.model.QueryProductResult

class PaymentRepository(
Expand All @@ -13,6 +17,46 @@ class PaymentRepository(
billingPaymentAvailability = getBillingProducts()
)

suspend fun purchaseBillingProduct(product: BillingProduct): PurchaseResult {
// Get transaction id
val transactionId = fetchTransactionId()

val result =
billingRepository.startPurchaseFlow(
productId = product.productId,
transactionId = transactionId
)

if (result is PurchaseFlowResult.Ok) {
// Wait for events
return when (val purchaseEvent = billingRepository.purchaseEvents.singleOrNull()) {
is PurchaseEvent.Error -> {
// Return error
PurchaseResult.PurchaseError
}
is PurchaseEvent.PurchaseCompleted -> {
// Verify towards api
if (verifyPurchase()) {
PurchaseResult.PurchaseCompleted
} else {
PurchaseResult.VerificationError
}
}
PurchaseEvent.UserCanceled -> {
// Purchase aborted
PurchaseResult.PurchaseCancelled
}
null -> {
// Return error
PurchaseResult.PurchaseError
}
}
} else {
// Return error
return PurchaseResult.PurchaseError
}
}

private suspend fun getBillingProducts(): BillingPaymentAvailability =
when (val result = billingRepository.queryProducts()) {
is QueryProductResult.Ok ->
Expand All @@ -30,4 +74,9 @@ class PaymentRepository(
// Placeholder function
return "BOOPITOBOP"
}

private fun verifyPurchase(): Boolean {
// Placeholder function
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.mullvad.mullvadvpn.repository.payment

sealed interface PurchaseResult {
data object PurchaseCompleted: PurchaseResult

data object VerificationError: PurchaseResult

data object PurchaseCancelled: PurchaseResult

data object PurchaseError: PurchaseResult
}

0 comments on commit cf799d8

Please sign in to comment.