English topic guide inspired by the original iOS Developer Notebook, expanded for both iOS and Android teams.
import StoreKit
@MainActor
func purchase(_ product: Product) async throws -> Transaction {
let result = try await product.purchase()
switch result {
case .success(let verification):
let transaction = try verification.payloadValue
await transaction.finish()
return transaction
default:
throw NSError(domain: "IAP", code: 1)
}
}
fun acknowledgePurchase(purchase: Purchase) {
if (!purchase.isAcknowledged) {
val params = AcknowledgePurchaseParams
.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
billingClient.acknowledgePurchase(params) { result ->
check(result.responseCode == BillingClient.BillingResponseCode.OK)
}
}
}