Skip to content

Commit

Permalink
Add purchase verification function to payment repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Sep 18, 2023
1 parent bca9759 commit 4dfdd6d
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package net.mullvad.mullvadvpn.lib.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.BillingPurchase
import net.mullvad.mullvadvpn.lib.billing.model.PurchaseEvent
import net.mullvad.mullvadvpn.lib.billing.model.PurchaseFlowResult
import net.mullvad.mullvadvpn.lib.billing.model.QueryProductResult
import net.mullvad.mullvadvpn.lib.billing.model.QueryPurchasesResult

class PaymentRepository(
private val billingRepository: BillingRepository,
Expand All @@ -17,13 +19,13 @@ class PaymentRepository(
billingPaymentAvailability = getBillingProducts()
)

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

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

Expand All @@ -36,7 +38,7 @@ class PaymentRepository(
}
is PurchaseEvent.PurchaseCompleted -> {
// Verify towards api
if (verifyPurchase()) {
if (verifyPurchase(purchaseEvent.purchases.first())) {
PurchaseResult.PurchaseCompleted
} else {
PurchaseResult.VerificationError
Expand All @@ -57,6 +59,13 @@ class PaymentRepository(
}
}

suspend fun verifyPurchases() {
val result = billingRepository.queryPurchases()
if (result is QueryPurchasesResult.PurchaseFound) {
verifyPurchase(result.purchase)
}
}

private suspend fun getBillingProducts(): BillingPaymentAvailability =
when (val result = billingRepository.queryProducts()) {
is QueryProductResult.Ok ->
Expand All @@ -77,7 +86,7 @@ class PaymentRepository(
return "BOOPITOBOP"
}

private fun verifyPurchase(): Boolean {
private fun verifyPurchase(purchase: BillingPurchase): Boolean {
// Placeholder function
return true
}
Expand Down

0 comments on commit 4dfdd6d

Please sign in to comment.