2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// ProductManager.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 4/6/19.
|
2022-02-04 11:44:30 +00:00
|
|
|
// Copyright (c) 2022 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/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2022-04-12 13:09:14 +00:00
|
|
|
import PassepartoutCore
|
2021-01-01 10:13:41 +00:00
|
|
|
import StoreKit
|
|
|
|
import Kvitto
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
enum ProductError: Error {
|
2021-02-02 16:51:09 +00:00
|
|
|
case uneligible
|
|
|
|
|
2021-01-07 22:37:32 +00:00
|
|
|
case beta
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@MainActor
|
|
|
|
class ProductManager: NSObject, ObservableObject {
|
|
|
|
enum AppType: Int {
|
|
|
|
case freemium = 0
|
|
|
|
|
|
|
|
case beta = 1
|
2021-02-05 16:10:19 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
case fullVersion = 2
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
struct Configuration {
|
|
|
|
let appType: AppType
|
|
|
|
|
|
|
|
let lastFullVersionBuild: (Int, LocalProduct)
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2022-04-13 18:12:25 +00:00
|
|
|
let lastNetworkSettingsBuild: Int
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
init(
|
|
|
|
appType: AppType,
|
2022-04-13 18:12:25 +00:00
|
|
|
lastFullVersionBuild: (Int, LocalProduct),
|
|
|
|
lastNetworkSettingsBuild: Int
|
2021-01-01 10:13:41 +00:00
|
|
|
) {
|
2022-04-12 13:09:14 +00:00
|
|
|
self.appType = appType
|
2021-01-01 10:13:41 +00:00
|
|
|
self.lastFullVersionBuild = lastFullVersionBuild
|
2022-04-13 18:12:25 +00:00
|
|
|
self.lastNetworkSettingsBuild = lastNetworkSettingsBuild
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
let cfg: Configuration
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@Published private(set) var isRefreshingProducts = false
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@Published private(set) var products: [SKProduct]
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2021-09-27 15:59:01 +00:00
|
|
|
private let inApp: InApp<LocalProduct>
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
private var purchasedAppBuild: Int?
|
|
|
|
|
2021-09-27 15:59:01 +00:00
|
|
|
private var purchasedFeatures: Set<LocalProduct>
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2021-09-27 15:59:01 +00:00
|
|
|
private var purchaseDates: [LocalProduct: Date]
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2021-09-27 15:59:01 +00:00
|
|
|
private var cancelledPurchases: Set<LocalProduct>
|
2021-01-29 18:06:39 +00:00
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
private var refreshRequest: SKReceiptRefreshRequest?
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
init(_ cfg: Configuration) {
|
2021-01-01 10:13:41 +00:00
|
|
|
self.cfg = cfg
|
2022-04-12 13:09:14 +00:00
|
|
|
products = []
|
2021-01-01 10:13:41 +00:00
|
|
|
inApp = InApp()
|
|
|
|
purchasedAppBuild = nil
|
|
|
|
purchasedFeatures = []
|
|
|
|
purchaseDates = [:]
|
2021-01-29 18:06:39 +00:00
|
|
|
cancelledPurchases = []
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
reloadReceipt()
|
|
|
|
SKPaymentQueue.default().add(self)
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
refreshProducts()
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
SKPaymentQueue.default().remove(self)
|
|
|
|
}
|
|
|
|
|
2022-04-18 10:01:42 +00:00
|
|
|
func canMakePayments() -> Bool {
|
|
|
|
SKPaymentQueue.canMakePayments()
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func refreshProducts() {
|
|
|
|
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
|
|
|
|
inApp.requestProducts(withIdentifiers: ids, completionHandler: { _ in
|
|
|
|
pp_log.debug("In-app products: \(self.inApp.products.map { $0.productIdentifier })")
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
self.products = self.inApp.products
|
|
|
|
self.isRefreshingProducts = false
|
2021-01-01 10:13:41 +00:00
|
|
|
}, failureHandler: {
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.warning("Unable to list products: \($0)")
|
|
|
|
self.isRefreshingProducts = false
|
2021-01-01 10:13:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func product(withIdentifier identifier: LocalProduct) -> SKProduct? {
|
2021-01-01 10:13:41 +00:00
|
|
|
return inApp.product(withIdentifier: identifier)
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func featureProducts(including: [LocalProduct]) -> [SKProduct] {
|
2021-02-07 11:50:39 +00:00
|
|
|
return 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
|
|
|
|
}
|
|
|
|
guard including.contains(p) else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
guard p.isFeature else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func featureProducts(excluding: [LocalProduct]) -> [SKProduct] {
|
2021-01-01 10:13:41 +00:00
|
|
|
return 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
|
|
|
|
}
|
2021-02-04 12:34:42 +00:00
|
|
|
guard !excluding.contains(p) else {
|
2021-01-01 10:13:41 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
guard p.isFeature else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func purchase(_ product: SKProduct, completionHandler: @escaping (Result<InAppPurchaseResult, Error>) -> Void) {
|
2022-04-13 18:51:55 +00:00
|
|
|
inApp.purchase(product: product) { result in
|
|
|
|
if case .success = result {
|
2021-01-01 10:13:41 +00:00
|
|
|
self.reloadReceipt()
|
|
|
|
}
|
2022-04-13 18:51:55 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(result)
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func restorePurchases(completionHandler: @escaping (Error?) -> Void) {
|
2022-04-13 18:51:55 +00:00
|
|
|
inApp.restorePurchases { (finished, _, error) in
|
|
|
|
guard finished else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(error)
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: In-app eligibility
|
|
|
|
|
2021-02-07 09:43:54 +00:00
|
|
|
private func isCurrentPlatformVersion() -> Bool {
|
2022-04-19 12:32:15 +00:00
|
|
|
#if targetEnvironment(macCatalyst)
|
2021-02-07 09:43:54 +00:00
|
|
|
return purchasedFeatures.contains(.fullVersion_macOS)
|
2022-04-19 12:32:15 +00:00
|
|
|
#else
|
|
|
|
return purchasedFeatures.contains(.fullVersion_iOS)
|
2021-02-07 09:43:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private func isFullVersion() -> Bool {
|
2022-04-12 13:09:14 +00:00
|
|
|
if cfg.appType == .fullVersion {
|
2021-01-01 10:13:41 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-02-07 09:43:54 +00:00
|
|
|
if isCurrentPlatformVersion() {
|
2021-01-01 10:13:41 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return purchasedFeatures.contains(.fullVersion)
|
|
|
|
}
|
2021-02-02 16:51:09 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func isEligible(forFeature feature: LocalProduct) -> Bool {
|
2022-04-13 18:12:25 +00:00
|
|
|
if let purchasedAppBuild = purchasedAppBuild {
|
|
|
|
if feature == .networkSettings && purchasedAppBuild <= cfg.lastNetworkSettingsBuild {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 12:32:15 +00:00
|
|
|
#if targetEnvironment(macCatalyst)
|
2021-02-07 14:18:55 +00:00
|
|
|
return isFullVersion()
|
2022-04-19 12:32:15 +00:00
|
|
|
#else
|
|
|
|
return isFullVersion() || purchasedFeatures.contains(feature)
|
2021-02-07 14:18:55 +00:00
|
|
|
#endif
|
2021-02-02 16:51:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func isEligible(forProvider providerName: ProviderName) -> Bool {
|
|
|
|
isEligible(forFeature: providerName.product)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func isEligibleForFeedback() -> Bool {
|
|
|
|
return cfg.appType == .beta || !purchasedFeatures.isEmpty
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
func hasPurchased(_ product: LocalProduct) -> Bool {
|
2021-02-07 14:28:30 +00:00
|
|
|
return purchasedFeatures.contains(product)
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func isCancelledPurchase(_ product: LocalProduct) -> Bool {
|
2021-01-29 18:06:39 +00:00
|
|
|
return cancelledPurchases.contains(product)
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func purchaseDate(forProduct product: LocalProduct) -> Date? {
|
2021-01-01 10:13:41 +00:00
|
|
|
return purchaseDates[product]
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func reloadReceipt(andNotify: Bool = true) {
|
2021-01-01 10:13:41 +00:00
|
|
|
guard let url = Bundle.main.appStoreReceiptURL else {
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.warning("No App Store receipt found!")
|
2021-01-01 10:13:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let receipt = Receipt(contentsOfURL: url) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
if let originalAppVersion = receipt.originalAppVersion, let buildNumber = Int(originalAppVersion) {
|
|
|
|
purchasedAppBuild = buildNumber
|
|
|
|
}
|
|
|
|
purchasedFeatures.removeAll()
|
2021-01-29 18:06:39 +00:00
|
|
|
cancelledPurchases.removeAll()
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// treat former purchases as full versions
|
2021-02-05 17:47:32 +00:00
|
|
|
if buildNumber <= cfg.lastFullVersionBuild.0 {
|
|
|
|
purchasedFeatures.insert(cfg.lastFullVersionBuild.1)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let iapReceipts = receipt.inAppPurchaseReceipts {
|
|
|
|
purchaseDates.removeAll()
|
|
|
|
|
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-29 18:06:39 +00:00
|
|
|
cancelledPurchases.insert(product)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProductManager: SKPaymentTransactionObserver {
|
2022-04-12 13:09:14 +00:00
|
|
|
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
|
2021-07-19 22:21:14 +00:00
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
self?.reloadReceipt()
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 16:51:09 +00:00
|
|
|
extension ProductManager {
|
2022-04-12 13:09:14 +00:00
|
|
|
func hasRefunded() -> Bool {
|
2021-02-02 16:51:09 +00:00
|
|
|
reloadReceipt(andNotify: false)
|
|
|
|
let isEligibleForFullVersion = isFullVersion()
|
|
|
|
let hasCancelledFullVersion: Bool
|
|
|
|
let hasCancelledTrustedNetworks: Bool
|
|
|
|
|
2022-04-19 12:32:15 +00:00
|
|
|
#if targetEnvironment(macCatalyst)
|
2021-02-02 16:51:09 +00:00
|
|
|
hasCancelledFullVersion = !isEligibleForFullVersion && (isCancelledPurchase(.fullVersion) || isCancelledPurchase(.fullVersion_macOS))
|
|
|
|
hasCancelledTrustedNetworks = false
|
2022-04-19 12:32:15 +00:00
|
|
|
#else
|
|
|
|
hasCancelledFullVersion = !isEligibleForFullVersion && (isCancelledPurchase(.fullVersion) || isCancelledPurchase(.fullVersion_iOS))
|
|
|
|
hasCancelledTrustedNetworks = !isEligibleForFullVersion && isCancelledPurchase(.trustedNetworks)
|
2021-02-02 16:51:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// review features and potentially revert them if they were used (Siri is handled in AppDelegate)
|
2022-04-12 13:09:14 +00:00
|
|
|
return hasCancelledFullVersion || hasCancelledTrustedNetworks
|
2021-02-02 16:51:09 +00:00
|
|
|
}
|
|
|
|
}
|