Skip to content

Commit

Permalink
refactor(payments): update PurchaseManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajhilje committed Mar 5, 2024
1 parent a7ca5e8 commit 7f7ee14
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions IVPNClient/Managers/PurchaseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class PurchaseManager: NSObject {

private(set) var products: [Product] = []

private var purchasing = false

private var apiEndpoint: String {
guard let _ = KeyChain.sessionToken else {
return Config.apiPaymentInitial
Expand Down Expand Up @@ -78,30 +80,34 @@ class PurchaseManager: NSObject {
}

delegate?.purchaseStart()
purchasing = true
let result = try await product.purchase()

switch result {
case let .success(.verified(transaction)):
// Successful purchase
log(.info, message: "[Store] Purchase success \(productId), completing transaction")
log(.info, message: "[Store] Completing successful in-app purchase \(productId)")
self.complete(transaction)
break
case .success(.unverified(_, _)):
// Successful purchase but transaction/receipt can't be verified
// Could be a jailbroken phone
log(.info, message: "[Store] Purchase \(productId): success, unverified")
delegate?.purchaseError(error: ErrorResult(status: 500, message: "Purchase is unverified."))
purchasing = false
break
case .pending:
// Transaction waiting on SCA (Strong Customer Authentication) or
// approval from Ask to Buy
log(.info, message: "[Store] Purchase \(productId): pending")
delegate?.purchasePending()
purchasing = false
break
case .userCancelled:
// ^^^
log(.info, message: "[Store] Purchase \(productId): userCancelled")
delegate?.purchaseError(error: ErrorResult(status: 500, message: "User canelled the purchase."))
purchasing = false
break
@unknown default:
break
Expand All @@ -111,15 +117,19 @@ class PurchaseManager: NSObject {
}

func startObserver() {
observerTask = Task {
for await result in Transaction.updates {
observerTask = Task(priority: .background) {
for await _ in Transaction.updates {
guard !purchasing else {
break
}

for await result in Transaction.unfinished {
guard case .verified(let transaction) = result else {
continue
}

if transaction.revocationDate == nil {
log(.info, message: "[Store] Completing updated transaction \(transaction.productID)")
log(.info, message: "[Store] Completing unfinished purchase \(transaction.productID)")
complete(transaction)
}
}
Expand All @@ -142,15 +152,14 @@ class PurchaseManager: NSObject {

if transaction.revocationDate == nil {
self.getAccountFor(transaction: transaction) { account, error in
log(.info, message: "[Store] Purchase is restored.")
completion(account, error)
}
return
}
}

let error = ErrorResult(status: 500, message: "There are no purchases to restore.")
log(.error, message: "[Store] There are no purchases to restore.")
log(.error, message: "[Store] There are no purchases to restore")
completion(nil, error)
}
}
Expand All @@ -173,10 +182,12 @@ class PurchaseManager: NSObject {
Task {
await transaction.finish()
self.delegate?.purchaseSuccess(service: sessionStatus.serviceStatus)
log(.info, message: "[Store] Purchase was completed successfully.")
self.purchasing = false
log(.info, message: "[Store] Purchase \(transaction.productID) completed successfully")
}
case .failure(let error):
self.delegate?.purchaseError(error: error ?? defaultError)
self.purchasing = false
log(.error, message: "[Store] There was an error with purchase completion: \(error?.message ?? "")")
}
}
Expand All @@ -198,7 +209,7 @@ class PurchaseManager: NSObject {
case .success(let account):
KeyChain.username = account.accountId
completion(account, nil)
log(.info, message: "[Store] Purchase was successfully restored.")
log(.info, message: "[Store] Purchase restored successfully")
case .failure(let error):
completion(nil, error ?? defaultError)
log(.error, message: "[Store] There was an error with restoring purchase: \(error?.message ?? "")")
Expand Down

0 comments on commit 7f7ee14

Please sign in to comment.