Skip to content

Commit

Permalink
feat: update PurchaseManager.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajhilje committed Mar 21, 2024
1 parent 53985b1 commit 3ef3f26
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions IVPNClient/Managers/PurchaseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ 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 @@ -80,7 +78,6 @@ class PurchaseManager: NSObject {
}

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

switch result {
Expand All @@ -94,20 +91,17 @@ class PurchaseManager: NSObject {
// 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 @@ -118,23 +112,15 @@ class PurchaseManager: NSObject {

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

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

if transaction.revocationDate == nil {
log(.info, message: "[Store] Completing unfinished purchase \(transaction.productID)")
complete(transaction)
}
if transaction.revocationDate == nil {
log(.info, message: "[Store] Completing unfinished purchase \(transaction.productID)")
complete(transaction)
}

break
}
}
}
Expand Down Expand Up @@ -182,12 +168,10 @@ class PurchaseManager: NSObject {
Task {
await transaction.finish()
self.delegate?.purchaseSuccess(service: sessionStatus.serviceStatus)
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 Down

0 comments on commit 3ef3f26

Please sign in to comment.