2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// IAPManager.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/10/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
import Combine
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-09-23 13:02:26 +00:00
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
public final class IAPManager: ObservableObject {
|
|
|
|
private let customUserLevel: AppUserLevel?
|
|
|
|
|
2024-11-05 17:55:57 +00:00
|
|
|
private let inAppHelper: any AppProductHelper
|
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
private let receiptReader: AppReceiptReader
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-10-02 14:05:40 +00:00
|
|
|
private let unrestrictedFeatures: Set<AppFeature>
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
private let productsAtBuild: BuildProducts<AppProduct>?
|
|
|
|
|
|
|
|
private(set) var userLevel: AppUserLevel
|
|
|
|
|
|
|
|
private var purchasedAppBuild: Int?
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
public private(set) var purchasedProducts: Set<AppProduct>
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
@Published
|
|
|
|
public private(set) var eligibleFeatures: Set<AppFeature>
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-07 10:25:40 +00:00
|
|
|
private var pendingReceiptTask: Task<Void, Never>?
|
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
private var subscriptions: Set<AnyCancellable>
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
public init(
|
|
|
|
customUserLevel: AppUserLevel? = nil,
|
2024-11-05 17:55:57 +00:00
|
|
|
inAppHelper: any AppProductHelper,
|
2024-11-06 12:20:12 +00:00
|
|
|
receiptReader: AppReceiptReader,
|
2024-10-02 14:05:40 +00:00
|
|
|
unrestrictedFeatures: Set<AppFeature> = [],
|
2024-09-23 13:02:26 +00:00
|
|
|
productsAtBuild: BuildProducts<AppProduct>? = nil
|
|
|
|
) {
|
|
|
|
self.customUserLevel = customUserLevel
|
2024-11-05 17:55:57 +00:00
|
|
|
self.inAppHelper = inAppHelper
|
2024-09-23 13:02:26 +00:00
|
|
|
self.receiptReader = receiptReader
|
2024-10-02 14:05:40 +00:00
|
|
|
self.unrestrictedFeatures = unrestrictedFeatures
|
2024-09-23 13:02:26 +00:00
|
|
|
self.productsAtBuild = productsAtBuild
|
|
|
|
userLevel = .undefined
|
|
|
|
purchasedProducts = []
|
|
|
|
eligibleFeatures = []
|
2024-11-06 12:20:12 +00:00
|
|
|
subscriptions = []
|
2024-11-05 17:55:57 +00:00
|
|
|
}
|
2024-11-06 12:20:12 +00:00
|
|
|
}
|
2024-11-05 17:55:57 +00:00
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
// MARK: - Actions
|
2024-11-05 17:55:57 +00:00
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
extension IAPManager {
|
2024-11-07 22:02:10 +00:00
|
|
|
public func purchasableProducts(for products: [AppProduct]) async throws -> [InAppProduct] {
|
2024-11-06 12:20:12 +00:00
|
|
|
do {
|
|
|
|
let inAppProducts = try await inAppHelper.fetchProducts()
|
|
|
|
return products.compactMap {
|
|
|
|
inAppProducts[$0]
|
|
|
|
}
|
|
|
|
} catch {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .error, "Unable to fetch in-app products: \(error)")
|
2024-11-07 22:02:10 +00:00
|
|
|
throw error
|
2024-11-05 17:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
public func purchase(_ purchasableProduct: InAppProduct) async throws -> InAppPurchaseResult {
|
|
|
|
let result = try await inAppHelper.purchase(purchasableProduct)
|
2024-11-05 17:55:57 +00:00
|
|
|
if result == .done {
|
|
|
|
await reloadReceipt()
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
public func restorePurchases() async throws {
|
|
|
|
try await inAppHelper.restorePurchases()
|
|
|
|
await reloadReceipt()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func reloadReceipt() async {
|
2024-11-07 10:25:40 +00:00
|
|
|
if let pendingReceiptTask {
|
|
|
|
await pendingReceiptTask.value
|
|
|
|
}
|
|
|
|
pendingReceiptTask = Task {
|
2024-11-09 14:20:59 +00:00
|
|
|
await fetchLevelIfNeeded()
|
2024-11-07 10:25:40 +00:00
|
|
|
await asyncReloadReceipt()
|
|
|
|
}
|
|
|
|
await pendingReceiptTask?.value
|
|
|
|
pendingReceiptTask = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Eligibility
|
|
|
|
|
|
|
|
extension IAPManager {
|
|
|
|
public var isRestricted: Bool {
|
|
|
|
userLevel.isRestricted
|
|
|
|
}
|
|
|
|
|
|
|
|
public func isEligible(for feature: AppFeature) -> Bool {
|
|
|
|
eligibleFeatures.contains(feature)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func isEligible(for features: [AppFeature]) -> Bool {
|
|
|
|
features.allSatisfy(eligibleFeatures.contains)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func isEligible(forProvider providerId: ProviderID) -> Bool {
|
|
|
|
if providerId == .oeck {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return isEligible(for: .providers)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func isEligibleForFeedback() -> Bool {
|
|
|
|
#if os(tvOS)
|
|
|
|
false
|
|
|
|
#else
|
|
|
|
userLevel == .beta || isPayingUser()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
public func paywallReason(forFeature feature: AppFeature, suggesting product: AppProduct?) -> PaywallReason? {
|
|
|
|
if isEligible(for: feature) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return isRestricted ? .restricted : .purchase(feature, product)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func isPayingUser() -> Bool {
|
|
|
|
!purchasedProducts.isEmpty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Receipt
|
|
|
|
|
|
|
|
private extension IAPManager {
|
|
|
|
func asyncReloadReceipt() async {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .notice, "Start reloading in-app receipt...")
|
2024-11-07 10:25:40 +00:00
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
var purchasedAppBuild: Int?
|
|
|
|
var purchasedProducts: Set<AppProduct> = []
|
|
|
|
var eligibleFeatures: Set<AppFeature> = []
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
if let receipt = await receiptReader.receipt(at: userLevel) {
|
2024-09-23 13:02:26 +00:00
|
|
|
if let originalBuildNumber = receipt.originalBuildNumber {
|
|
|
|
purchasedAppBuild = originalBuildNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
if let purchasedAppBuild {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .info, "Original purchased build: \(purchasedAppBuild)")
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
// assume some purchases by build number
|
|
|
|
let entitled = productsAtBuild?(purchasedAppBuild) ?? []
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .notice, "Entitled features: \(entitled.map(\.rawValue))")
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
entitled.forEach {
|
|
|
|
purchasedProducts.insert($0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let iapReceipts = receipt.purchaseReceipts {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .info, "Process in-app purchase receipts...")
|
2024-11-07 10:25:40 +00:00
|
|
|
|
|
|
|
let products: [AppProduct] = iapReceipts.compactMap {
|
|
|
|
guard let pid = $0.productIdentifier else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let product = AppProduct(rawValue: pid) else {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .debug, "\tDiscard unknown product identifier: \(pid)")
|
2024-11-07 10:25:40 +00:00
|
|
|
return nil
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-11-06 12:20:12 +00:00
|
|
|
if let expirationDate = $0.expirationDate {
|
|
|
|
let now = Date()
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .debug, "\t\(pid) [expiration date: \(expirationDate), now: \(now)]")
|
2024-11-06 12:20:12 +00:00
|
|
|
if now >= expirationDate {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .info, "\t\(pid) [expired on: \(expirationDate)]")
|
2024-11-07 10:25:40 +00:00
|
|
|
return nil
|
2024-11-06 12:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
if let cancellationDate = $0.cancellationDate {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .info, "\t\(pid) [cancelled on: \(cancellationDate)]")
|
2024-11-07 10:25:40 +00:00
|
|
|
return nil
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
if let purchaseDate = $0.originalPurchaseDate {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .info, "\t\(pid) [purchased on: \(purchaseDate)]")
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-11-07 10:25:40 +00:00
|
|
|
return product
|
|
|
|
}
|
|
|
|
|
|
|
|
products.forEach {
|
|
|
|
purchasedProducts.insert($0)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eligibleFeatures = purchasedProducts.reduce(into: []) { eligible, product in
|
|
|
|
product.features.forEach {
|
|
|
|
eligible.insert($0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .error, "Could not parse App Store receipt!")
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
userLevel.features.forEach {
|
|
|
|
eligibleFeatures.insert($0)
|
|
|
|
}
|
2024-10-02 14:05:40 +00:00
|
|
|
unrestrictedFeatures.forEach {
|
|
|
|
eligibleFeatures.insert($0)
|
|
|
|
}
|
|
|
|
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .notice, "Finished reloading in-app receipt for user level \(userLevel)")
|
|
|
|
pp_log(.App.iap, .notice, "\tPurchased build number: \(purchasedAppBuild?.description ?? "unknown")")
|
|
|
|
pp_log(.App.iap, .notice, "\tPurchased products: \(purchasedProducts.map(\.rawValue))")
|
|
|
|
pp_log(.App.iap, .notice, "\tEligible features: \(eligibleFeatures)")
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
self.purchasedAppBuild = purchasedAppBuild
|
|
|
|
self.purchasedProducts = purchasedProducts
|
|
|
|
self.eligibleFeatures = eligibleFeatures // @Published -> objectWillChange.send()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
// MARK: - Observation
|
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
extension IAPManager {
|
|
|
|
public func observeObjects() {
|
2024-11-06 12:20:12 +00:00
|
|
|
Task {
|
|
|
|
await fetchLevelIfNeeded()
|
|
|
|
do {
|
|
|
|
let products = try await inAppHelper.fetchProducts()
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .info, "Available in-app products: \(products.map(\.key))")
|
2024-11-06 12:20:12 +00:00
|
|
|
|
|
|
|
inAppHelper
|
|
|
|
.didUpdate
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] in
|
|
|
|
Task {
|
|
|
|
await self?.reloadReceipt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
|
|
|
} catch {
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .error, "Unable to fetch in-app products: \(error)")
|
2024-11-06 12:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-09 14:20:59 +00:00
|
|
|
}
|
2024-11-06 12:20:12 +00:00
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
private extension IAPManager {
|
2024-11-06 12:20:12 +00:00
|
|
|
func fetchLevelIfNeeded() async {
|
|
|
|
guard userLevel == .undefined else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if let customUserLevel {
|
|
|
|
userLevel = customUserLevel
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .info, "App level (custom): \(userLevel)")
|
2024-11-06 12:20:12 +00:00
|
|
|
} else {
|
|
|
|
let isBeta = await SandboxChecker().isBeta
|
|
|
|
userLevel = isBeta ? .beta : .freemium
|
2024-11-07 10:33:20 +00:00
|
|
|
pp_log(.App.iap, .info, "App level: \(userLevel)")
|
2024-11-06 12:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|