2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// ProductManager.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 4/6/19.
|
2024-01-14 13:34:21 +00:00
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
//
|
|
|
|
|
2023-04-04 07:50:45 +00:00
|
|
|
import Combine
|
2021-01-01 10:13:41 +00:00
|
|
|
import Foundation
|
2023-12-20 19:43:39 +00:00
|
|
|
import PassepartoutCore
|
|
|
|
import PassepartoutProviders
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
@MainActor
|
|
|
|
public final class ProductManager: NSObject, ObservableObject {
|
|
|
|
private let inApp: any LocalInApp
|
2023-09-09 22:52:39 +00:00
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
private let receiptReader: ReceiptReader
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-09-09 22:52:39 +00:00
|
|
|
private let overriddenAppType: AppType?
|
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
public let buildProducts: BuildProducts
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
@Published public private(set) var appType: AppType
|
2023-09-09 22:52:39 +00:00
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
@Published public private(set) var isRefreshingProducts = false
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
@Published public private(set) var products: [InAppProduct]
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
//
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2024-01-07 11:11:16 +00:00
|
|
|
public var purchasedProductIdentifiers: Set<String> {
|
|
|
|
Set(purchasedFeatures.map(\.rawValue))
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
private var purchasedAppBuild: Int?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2021-09-27 15:59:01 +00:00
|
|
|
private var purchasedFeatures: Set<LocalProduct>
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2021-09-27 15:59:01 +00:00
|
|
|
private var purchaseDates: [LocalProduct: Date]
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
public init(inApp: any LocalInApp,
|
|
|
|
receiptReader: ReceiptReader,
|
|
|
|
overriddenAppType: AppType? = nil,
|
2024-01-20 08:50:23 +00:00
|
|
|
buildProducts: BuildProducts? = nil) {
|
2023-09-09 22:52:39 +00:00
|
|
|
self.overriddenAppType = overriddenAppType
|
2023-12-20 19:43:39 +00:00
|
|
|
self.receiptReader = receiptReader
|
2024-01-20 08:50:23 +00:00
|
|
|
self.buildProducts = buildProducts ?? BuildProducts { _ in [] }
|
|
|
|
appType = overriddenAppType ?? .undefined
|
2023-09-09 22:52:39 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
products = []
|
2023-12-20 19:43:39 +00:00
|
|
|
self.inApp = inApp
|
2021-01-01 10:13:41 +00:00
|
|
|
purchasedAppBuild = nil
|
|
|
|
purchasedFeatures = []
|
|
|
|
purchaseDates = [:]
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
super.init()
|
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
inApp.setTransactionsObserver { [weak self] in
|
|
|
|
self?.reloadReceipt()
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
reloadReceipt()
|
2023-09-09 22:52:39 +00:00
|
|
|
|
2023-09-10 17:08:31 +00:00
|
|
|
Task {
|
2023-12-21 07:09:52 +00:00
|
|
|
await refreshProducts()
|
|
|
|
|
2023-09-10 17:08:31 +00:00
|
|
|
let isBeta = await SandboxChecker().isBeta
|
|
|
|
appType = overriddenAppType ?? (isBeta ? .beta : .freemium)
|
|
|
|
pp_log.info("App type: \(appType)")
|
|
|
|
reloadReceipt()
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-21 07:54:00 +00:00
|
|
|
// MARK: Main interface
|
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
public func canMakePayments() -> Bool {
|
|
|
|
inApp.canMakePurchases()
|
2022-04-18 10:01:42 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-21 07:09:52 +00:00
|
|
|
public func refreshProducts() async {
|
2022-04-12 13:09:14 +00:00
|
|
|
let ids = LocalProduct.all
|
|
|
|
guard !ids.isEmpty else {
|
2021-01-01 10:13:41 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-20 17:53:18 +00:00
|
|
|
guard products.isEmpty else {
|
|
|
|
pp_log.debug("In-app products already available, not refreshing")
|
|
|
|
return
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
isRefreshingProducts = true
|
2023-12-21 07:09:52 +00:00
|
|
|
do {
|
|
|
|
let productsMap = try await inApp.requestProducts(withIdentifiers: ids)
|
|
|
|
pp_log.debug("In-app products: \(productsMap.keys.map(\.rawValue))")
|
|
|
|
|
|
|
|
products = Array(productsMap.values)
|
|
|
|
isRefreshingProducts = false
|
|
|
|
} catch {
|
|
|
|
pp_log.warning("Unable to list products: \(error)")
|
|
|
|
isRefreshingProducts = false
|
2023-12-20 19:43:39 +00:00
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
public func product(withIdentifier identifier: LocalProduct) -> InAppProduct? {
|
2022-09-04 18:09:31 +00:00
|
|
|
inApp.product(withIdentifier: identifier)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-23 11:10:34 +00:00
|
|
|
public func featureProducts(including: (LocalProduct) -> Bool) -> [InAppProduct] {
|
2023-12-20 19:43:39 +00:00
|
|
|
inApp.products().filter {
|
2021-09-27 15:59:01 +00:00
|
|
|
guard let p = LocalProduct(rawValue: $0.productIdentifier) else {
|
2021-02-07 11:50:39 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-12-23 11:10:34 +00:00
|
|
|
guard including(p) else {
|
2021-02-07 11:50:39 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
guard p.isFeature else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-23 11:10:34 +00:00
|
|
|
public func featureProducts(excluding: (LocalProduct) -> Bool) -> [InAppProduct] {
|
2023-12-20 19:43:39 +00:00
|
|
|
inApp.products().filter {
|
2021-09-27 15:59:01 +00:00
|
|
|
guard let p = LocalProduct(rawValue: $0.productIdentifier) else {
|
2021-01-01 10:13:41 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-12-23 11:10:34 +00:00
|
|
|
guard !excluding(p) else {
|
2021-01-01 10:13:41 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
guard p.isFeature else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-21 07:09:52 +00:00
|
|
|
public func purchase(_ product: InAppProduct) async throws -> InAppPurchaseResult {
|
2023-12-20 19:43:39 +00:00
|
|
|
guard let pid = LocalProduct(rawValue: product.productIdentifier) else {
|
2023-12-21 07:09:52 +00:00
|
|
|
assertionFailure("Unrecognized product: \(product)")
|
2023-12-20 19:43:39 +00:00
|
|
|
pp_log.warning("Unrecognized product: \(product)")
|
2023-12-21 07:09:52 +00:00
|
|
|
return .cancelled
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2023-12-21 07:09:52 +00:00
|
|
|
let result = try await inApp.purchase(productWithIdentifier: pid)
|
|
|
|
reloadReceipt()
|
|
|
|
return result
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-21 07:09:52 +00:00
|
|
|
public func restorePurchases() async throws {
|
|
|
|
try await inApp.restorePurchases()
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-21 07:54:00 +00:00
|
|
|
public func hasPurchased(_ product: LocalProduct) -> Bool {
|
|
|
|
isActivePurchase(product)
|
2021-02-07 09:43:54 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 07:54:00 +00:00
|
|
|
public func purchaseDate(forProduct product: LocalProduct) -> Date? {
|
|
|
|
purchaseDates[product]
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2023-12-21 07:54:00 +00:00
|
|
|
}
|
2021-02-02 16:51:09 +00:00
|
|
|
|
2023-12-21 07:54:00 +00:00
|
|
|
// MARK: In-app eligibility
|
|
|
|
|
|
|
|
extension ProductManager {
|
2023-12-20 19:43:39 +00:00
|
|
|
public func isEligible(forFeature feature: LocalProduct) -> Bool {
|
2024-01-10 17:48:38 +00:00
|
|
|
if let purchasedAppBuild {
|
2022-04-26 18:40:51 +00:00
|
|
|
if feature == .networkSettings && buildProducts.hasProduct(.networkSettings, atBuild: purchasedAppBuild) {
|
2022-04-13 18:12:25 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2024-01-10 17:48:38 +00:00
|
|
|
pp_log.verbose("Eligibility: purchasedFeatures = \(purchasedFeatures)")
|
|
|
|
pp_log.verbose("Eligibility: purchaseDates = \(purchaseDates)")
|
|
|
|
pp_log.verbose("Eligibility: isIncludedInFullVersion(\(feature)) = \(isIncludedInFullVersion(feature))")
|
2023-12-16 19:58:54 +00:00
|
|
|
if isIncludedInFullVersion(feature) {
|
2024-01-10 17:48:38 +00:00
|
|
|
let isFullVersion = isFullVersion()
|
|
|
|
let isActive = isActivePurchase(feature)
|
|
|
|
pp_log.verbose("Eligibility: isFullVersion() = \(isFullVersion)")
|
|
|
|
pp_log.verbose("Eligibility: isActivePurchase(\(feature)) = \(isActive)")
|
|
|
|
return isFullVersion || isActive
|
2023-12-20 19:43:39 +00:00
|
|
|
}
|
2024-01-10 17:48:38 +00:00
|
|
|
let isActive = isActivePurchase(feature)
|
|
|
|
pp_log.verbose("Eligibility: isActivePurchase(\(feature)) = \(isActive)")
|
|
|
|
return isActive
|
2021-02-02 16:51:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
public func isEligible(forProvider providerName: ProviderName) -> Bool {
|
2022-10-04 20:24:06 +00:00
|
|
|
guard providerName != .oeck else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return isEligible(forFeature: providerName.product)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
public func isEligibleForFeedback() -> Bool {
|
2023-12-16 19:58:54 +00:00
|
|
|
appType == .beta || isPayingUser()
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2023-12-21 07:54:00 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-21 07:54:00 +00:00
|
|
|
extension ProductManager {
|
|
|
|
func isActivePurchase(_ feature: LocalProduct) -> Bool {
|
2024-01-10 17:48:38 +00:00
|
|
|
purchasedFeatures.contains(feature)
|
2021-02-07 14:28:30 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 07:54:00 +00:00
|
|
|
func isActivePurchase(where predicate: (LocalProduct) -> Bool) -> Bool {
|
2024-01-10 17:48:38 +00:00
|
|
|
purchasedFeatures.contains(where: predicate)
|
2023-12-21 07:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isCurrentPlatformVersion() -> Bool {
|
|
|
|
isActivePurchase(isMac ? .fullVersion_macOS : .fullVersion_iOS)
|
|
|
|
}
|
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
func isIncludedInFullVersion(_ feature: LocalProduct) -> Bool {
|
2024-01-20 08:50:23 +00:00
|
|
|
switch appType {
|
|
|
|
case .fullVersionPlusTV:
|
|
|
|
return !feature.isLegacyPlatformVersion
|
|
|
|
|
|
|
|
default:
|
|
|
|
return !feature.isLegacyPlatformVersion && feature != .appleTV
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func isFullVersion() -> Bool {
|
2024-01-20 08:50:23 +00:00
|
|
|
if appType == .fullVersion || appType == .fullVersionPlusTV {
|
2024-01-10 17:48:38 +00:00
|
|
|
pp_log.verbose("Eligibility: appType = .fullVersion")
|
2023-12-21 07:54:00 +00:00
|
|
|
return true
|
|
|
|
}
|
2024-01-10 17:48:38 +00:00
|
|
|
pp_log.verbose("Eligibility: isCurrentPlatformVersion() = \(isCurrentPlatformVersion())")
|
2023-12-21 07:54:00 +00:00
|
|
|
if isCurrentPlatformVersion() {
|
|
|
|
return true
|
|
|
|
}
|
2024-01-10 17:48:38 +00:00
|
|
|
pp_log.verbose("Eligibility: isActivePurchase(.fullVersion) = \(isActivePurchase(.fullVersion))")
|
2023-12-21 07:54:00 +00:00
|
|
|
return isActivePurchase(.fullVersion)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
|
|
|
|
public func isPayingUser() -> Bool {
|
2024-01-10 17:48:38 +00:00
|
|
|
!purchasedFeatures.isEmpty
|
2023-12-16 19:58:54 +00:00
|
|
|
}
|
2023-12-20 19:43:39 +00:00
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2023-12-21 07:54:00 +00:00
|
|
|
// MARK: Receipt
|
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
extension ProductManager {
|
|
|
|
public func reloadReceipt(andNotify: Bool = true) {
|
|
|
|
guard let receipt = receiptReader.receipt(for: appType) else {
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.error("Could not parse App Store receipt!")
|
2021-01-01 10:13:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
if let originalBuildNumber = receipt.originalBuildNumber {
|
|
|
|
purchasedAppBuild = originalBuildNumber
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
purchasedFeatures.removeAll()
|
|
|
|
|
|
|
|
if let buildNumber = purchasedAppBuild {
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.debug("Original purchased build: \(buildNumber)")
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2022-04-26 18:40:51 +00:00
|
|
|
// assume some purchases by build number
|
|
|
|
buildProducts.products(atBuild: buildNumber).forEach {
|
|
|
|
purchasedFeatures.insert($0)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 19:43:39 +00:00
|
|
|
if let iapReceipts = receipt.purchaseReceipts {
|
2021-01-01 10:13:41 +00:00
|
|
|
purchaseDates.removeAll()
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.debug("In-app receipts:")
|
2021-01-01 10:13:41 +00:00
|
|
|
iapReceipts.forEach {
|
2021-09-27 15:59:01 +00:00
|
|
|
guard let pid = $0.productIdentifier, let product = LocalProduct(rawValue: pid) else {
|
2021-01-01 10:13:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if let cancellationDate = $0.cancellationDate {
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.debug("\t\(pid) [cancelled on: \(cancellationDate)]")
|
2021-01-01 10:13:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if let purchaseDate = $0.originalPurchaseDate {
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.debug("\t\(pid) [purchased on: \(purchaseDate)]")
|
2021-01-01 10:13:41 +00:00
|
|
|
purchaseDates[product] = purchaseDate
|
|
|
|
}
|
|
|
|
purchasedFeatures.insert(product)
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.info("Purchased features: \(purchasedFeatures)")
|
2021-01-01 10:13:41 +00:00
|
|
|
if andNotify {
|
2022-04-13 18:51:55 +00:00
|
|
|
objectWillChange.send()
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 15:46:52 +00:00
|
|
|
// MARK: Purchasable products
|
|
|
|
|
|
|
|
extension ProductManager {
|
|
|
|
|
|
|
|
// no purchase -> full version or platform version
|
|
|
|
// purchased platform -> may only purchase other platform
|
|
|
|
|
|
|
|
public func purchasableProducts(withFeature feature: LocalProduct?) -> [LocalProduct] {
|
2024-01-11 16:52:05 +00:00
|
|
|
|
|
|
|
// separate purchase
|
|
|
|
guard feature != .appleTV else {
|
|
|
|
if hasPurchased(.appleTV) {
|
2024-01-11 15:46:52 +00:00
|
|
|
return []
|
|
|
|
}
|
2024-01-11 16:52:05 +00:00
|
|
|
return [.appleTV]
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasPurchased(.fullVersion) {
|
|
|
|
return []
|
|
|
|
}
|
2024-01-11 15:46:52 +00:00
|
|
|
#if targetEnvironment(macCatalyst)
|
2024-01-11 16:52:05 +00:00
|
|
|
if hasPurchased(.fullVersion_macOS) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
if hasPurchased(.fullVersion_iOS) {
|
|
|
|
return [.fullVersion_macOS]
|
|
|
|
}
|
|
|
|
return [.fullVersion, .fullVersion_macOS]
|
2024-01-11 15:46:52 +00:00
|
|
|
#else
|
2024-01-11 16:52:05 +00:00
|
|
|
if hasPurchased(.fullVersion_iOS) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
if hasPurchased(.fullVersion_macOS) {
|
|
|
|
return [.fullVersion_iOS]
|
2024-01-11 15:46:52 +00:00
|
|
|
}
|
2024-01-11 16:52:05 +00:00
|
|
|
return [.fullVersion, .fullVersion_iOS]
|
|
|
|
#endif
|
2024-01-11 15:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-21 07:54:00 +00:00
|
|
|
// MARK: Helpers
|
|
|
|
|
2023-07-24 21:32:05 +00:00
|
|
|
private extension ProductManager {
|
|
|
|
var isMac: Bool {
|
|
|
|
#if targetEnvironment(macCatalyst)
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
}
|
2021-02-02 16:51:09 +00:00
|
|
|
}
|