2019-04-06 21:50:07 +00:00
|
|
|
//
|
2019-10-11 08:56:23 +00:00
|
|
|
// ProductManager.swift
|
2019-04-06 21:50:07 +00:00
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 4/6/19.
|
2020-01-11 08:27:22 +00:00
|
|
|
// Copyright (c) 2020 Davide De Rosa. All rights reserved.
|
2019-04-06 21:50:07 +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
|
2019-04-06 22:36:44 +00:00
|
|
|
import StoreKit
|
2019-10-11 08:56:23 +00:00
|
|
|
import Convenience
|
2019-10-26 23:31:30 +00:00
|
|
|
import SwiftyBeaver
|
|
|
|
import Kvitto
|
|
|
|
import PassepartoutCore
|
2020-06-12 21:30:57 +00:00
|
|
|
import TunnelKit
|
2019-10-26 23:31:30 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
class ProductManager: NSObject {
|
2019-11-02 10:28:35 +00:00
|
|
|
static let didReloadReceipt = Notification.Name("ProductManagerDidReloadReceipt")
|
2019-11-09 12:06:13 +00:00
|
|
|
|
|
|
|
static let didReviewPurchases = Notification.Name("ProductManagerDidReviewPurchases")
|
2019-11-02 10:28:35 +00:00
|
|
|
|
2019-10-31 19:53:47 +00:00
|
|
|
private static let lastFullVersionBuild = 2016 // 1.8.1
|
2019-10-11 08:56:23 +00:00
|
|
|
|
|
|
|
static let shared = ProductManager()
|
|
|
|
|
2019-10-26 23:24:32 +00:00
|
|
|
private let inApp: InApp<Product>
|
2019-10-11 08:56:23 +00:00
|
|
|
|
2019-10-31 19:53:47 +00:00
|
|
|
private var purchasedAppBuild: Int?
|
2019-10-26 23:31:30 +00:00
|
|
|
|
2019-11-02 10:16:14 +00:00
|
|
|
private var purchasedFeatures: Set<Product>
|
2019-10-30 10:53:49 +00:00
|
|
|
|
|
|
|
private var refreshRequest: SKReceiptRefreshRequest?
|
|
|
|
|
|
|
|
private var restoreCompletionHandler: ((Error?) -> Void)?
|
2019-10-26 23:31:30 +00:00
|
|
|
|
|
|
|
private override init() {
|
2019-10-11 08:56:23 +00:00
|
|
|
inApp = InApp()
|
2019-10-31 19:53:47 +00:00
|
|
|
purchasedAppBuild = nil
|
2019-10-26 23:31:30 +00:00
|
|
|
purchasedFeatures = []
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
reloadReceipt()
|
2019-10-30 10:53:49 +00:00
|
|
|
SKPaymentQueue.default().add(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
SKPaymentQueue.default().remove(self)
|
2019-10-11 08:56:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 10:42:21 +00:00
|
|
|
func listProducts(completionHandler: (([SKProduct]?, Error?) -> Void)?) {
|
2020-05-13 12:23:21 +00:00
|
|
|
let products = Product.all
|
|
|
|
guard !products.isEmpty else {
|
|
|
|
completionHandler?(nil, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
inApp.requestProducts(withIdentifiers: products, completionHandler: { _ in
|
2019-11-28 10:22:10 +00:00
|
|
|
log.debug("In-app products: \(self.inApp.products.map { $0.productIdentifier })")
|
2020-05-13 12:23:21 +00:00
|
|
|
|
2019-11-30 10:42:21 +00:00
|
|
|
completionHandler?(self.inApp.products, nil)
|
|
|
|
}, failureHandler: {
|
|
|
|
completionHandler?(nil, $0)
|
|
|
|
})
|
2019-04-07 10:17:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 10:53:49 +00:00
|
|
|
func product(withIdentifier identifier: Product) -> SKProduct? {
|
|
|
|
return inApp.product(withIdentifier: identifier)
|
|
|
|
}
|
|
|
|
|
2019-11-09 16:32:00 +00:00
|
|
|
func featureProducts(includingFullVersion: Bool) -> [SKProduct] {
|
|
|
|
return inApp.products.filter {
|
|
|
|
guard let p = Product(rawValue: $0.productIdentifier) else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
guard includingFullVersion || p != .fullVersion else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
guard p.isFeature else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
func purchase(_ product: SKProduct, completionHandler: @escaping (InAppPurchaseResult, Error?) -> Void) {
|
2019-10-26 23:31:30 +00:00
|
|
|
inApp.purchase(product: product) {
|
|
|
|
if $0 == .success {
|
|
|
|
self.reloadReceipt()
|
|
|
|
}
|
|
|
|
completionHandler($0, $1)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 10:53:49 +00:00
|
|
|
|
|
|
|
func restorePurchases(completionHandler: @escaping (Error?) -> Void) {
|
|
|
|
restoreCompletionHandler = completionHandler
|
|
|
|
refreshRequest = SKReceiptRefreshRequest()
|
|
|
|
refreshRequest?.delegate = self
|
|
|
|
refreshRequest?.start()
|
|
|
|
}
|
2019-10-26 23:31:30 +00:00
|
|
|
|
|
|
|
// MARK: In-app eligibility
|
|
|
|
|
2019-11-09 11:40:48 +00:00
|
|
|
private func reloadReceipt(andNotify: Bool = true) {
|
2019-10-26 23:31:30 +00:00
|
|
|
guard let url = Bundle.main.appStoreReceiptURL else {
|
|
|
|
log.warning("No App Store receipt found!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let receipt = Receipt(contentsOfURL: url) else {
|
|
|
|
log.error("Could not parse App Store receipt!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:53:47 +00:00
|
|
|
if let originalAppVersion = receipt.originalAppVersion, let buildNumber = Int(originalAppVersion) {
|
|
|
|
purchasedAppBuild = buildNumber
|
|
|
|
}
|
2019-10-26 23:31:30 +00:00
|
|
|
purchasedFeatures.removeAll()
|
|
|
|
|
2019-10-31 19:53:47 +00:00
|
|
|
if let buildNumber = purchasedAppBuild {
|
|
|
|
log.debug("Original purchased build: \(buildNumber)")
|
2019-10-26 23:31:30 +00:00
|
|
|
|
|
|
|
// treat former purchases as full versions
|
2019-10-31 19:53:47 +00:00
|
|
|
if buildNumber <= ProductManager.lastFullVersionBuild {
|
2019-10-26 23:31:30 +00:00
|
|
|
purchasedFeatures.insert(.fullVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let iapReceipts = receipt.inAppPurchaseReceipts {
|
|
|
|
log.debug("In-app receipts:")
|
|
|
|
iapReceipts.forEach {
|
2019-11-09 17:29:23 +00:00
|
|
|
guard let pid = $0.productIdentifier, let product = Product(rawValue: pid) else {
|
2019-10-26 23:31:30 +00:00
|
|
|
return
|
|
|
|
}
|
2019-11-09 17:29:23 +00:00
|
|
|
if let cancellationDate = $0.cancellationDate {
|
2019-11-09 11:18:06 +00:00
|
|
|
log.debug("\t\(pid) [cancelled on: \(cancellationDate)]")
|
2019-11-09 17:29:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if let purchaseDate = $0.originalPurchaseDate {
|
|
|
|
log.debug("\t\(pid) [purchased on: \(purchaseDate)]")
|
2019-11-09 11:18:06 +00:00
|
|
|
}
|
2019-10-26 23:31:30 +00:00
|
|
|
purchasedFeatures.insert(product)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("Purchased features: \(purchasedFeatures)")
|
2019-11-02 10:28:35 +00:00
|
|
|
|
2019-11-09 11:40:48 +00:00
|
|
|
if andNotify {
|
|
|
|
NotificationCenter.default.post(name: ProductManager.didReloadReceipt, object: nil)
|
|
|
|
}
|
2019-10-26 23:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isFullVersion() -> Bool {
|
2019-11-02 10:45:00 +00:00
|
|
|
if AppConstants.Flags.isBeta && AppConstants.Flags.isBetaFullVersion {
|
2019-10-26 23:31:30 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return purchasedFeatures.contains(.fullVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEligible(forFeature feature: Product) -> Bool {
|
2019-12-22 11:07:48 +00:00
|
|
|
return isFullVersion() || purchasedFeatures.contains(feature)
|
2019-10-26 23:31:30 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 10:56:06 +00:00
|
|
|
func isEligible(forProvider metadata: Infrastructure.Metadata) -> Bool {
|
2019-12-22 11:07:48 +00:00
|
|
|
return isFullVersion() || purchasedFeatures.contains(metadata.product)
|
2019-04-06 21:50:07 +00:00
|
|
|
}
|
2019-11-02 10:16:14 +00:00
|
|
|
|
|
|
|
func isEligibleForFeedback() -> Bool {
|
|
|
|
return AppConstants.Flags.isBeta || !purchasedFeatures.isEmpty
|
|
|
|
}
|
2019-11-09 11:40:48 +00:00
|
|
|
|
|
|
|
// MARK: Review
|
|
|
|
|
|
|
|
func reviewPurchases() {
|
|
|
|
let service = TransientStore.shared.service
|
|
|
|
reloadReceipt(andNotify: false)
|
2019-11-21 08:28:28 +00:00
|
|
|
var anyRefund = false
|
2019-11-09 11:40:48 +00:00
|
|
|
|
|
|
|
// review features and potentially revert them if they were used (Siri is handled in AppDelegate)
|
|
|
|
|
|
|
|
log.debug("Checking 'Trusted networks'")
|
|
|
|
if !isEligible(forFeature: .trustedNetworks) {
|
2019-11-21 08:28:28 +00:00
|
|
|
|
|
|
|
// reset trusted networks for ALL profiles (must load first)
|
|
|
|
for key in service.allProfileKeys() {
|
|
|
|
guard let profile = service.profile(withKey: key) else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if profile.trustedNetworks.includesMobile || !profile.trustedNetworks.includedWiFis.isEmpty {
|
|
|
|
profile.trustedNetworks.includesMobile = false
|
|
|
|
profile.trustedNetworks.includedWiFis.removeAll()
|
|
|
|
anyRefund = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if anyRefund {
|
2019-11-09 11:40:48 +00:00
|
|
|
log.debug("\tRefunded")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.debug("Checking 'Unlimited hosts'")
|
|
|
|
if !isEligible(forFeature: .unlimitedHosts) {
|
2020-02-28 17:32:04 +00:00
|
|
|
let ids = service.hostIds()
|
2019-11-09 11:40:48 +00:00
|
|
|
if ids.count > AppConstants.InApp.limitedNumberOfHosts {
|
|
|
|
for id in ids {
|
|
|
|
service.removeProfile(ProfileKey(.host, id))
|
|
|
|
}
|
|
|
|
log.debug("\tRefunded")
|
2019-11-21 08:28:28 +00:00
|
|
|
anyRefund = true
|
2019-11-09 11:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.debug("Checking providers")
|
2020-02-28 17:32:04 +00:00
|
|
|
for name in service.providerNames() {
|
2019-12-22 10:56:06 +00:00
|
|
|
guard let metadata = InfrastructureFactory.shared.metadata(forName: name) else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !isEligible(forProvider: metadata) {
|
2019-11-09 11:40:48 +00:00
|
|
|
service.removeProfile(ProfileKey(name))
|
|
|
|
log.debug("\tRefunded provider: \(name)")
|
2019-11-21 08:28:28 +00:00
|
|
|
anyRefund = true
|
2019-11-09 11:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 08:28:28 +00:00
|
|
|
guard anyRefund else {
|
2019-11-09 11:40:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// save reverts and remove fraud VPN profile
|
|
|
|
TransientStore.shared.serialize(withProfiles: true)
|
|
|
|
VPN.shared.uninstall(completionHandler: nil)
|
|
|
|
|
2019-11-09 12:06:13 +00:00
|
|
|
NotificationCenter.default.post(name: ProductManager.didReviewPurchases, object: nil)
|
2019-11-09 11:40:48 +00:00
|
|
|
}
|
2019-04-06 21:50:07 +00:00
|
|
|
}
|
2019-10-30 10:53:49 +00:00
|
|
|
|
2019-10-28 09:14:03 +00:00
|
|
|
extension ConnectionService {
|
|
|
|
var hasReachedMaximumNumberOfHosts: Bool {
|
2020-02-28 17:32:04 +00:00
|
|
|
let numberOfHosts = hostIds().count
|
2019-10-28 09:14:03 +00:00
|
|
|
return numberOfHosts >= AppConstants.InApp.limitedNumberOfHosts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 10:53:49 +00:00
|
|
|
extension ProductManager: SKPaymentTransactionObserver {
|
|
|
|
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
|
|
|
|
reloadReceipt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProductManager: SKRequestDelegate {
|
|
|
|
func requestDidFinish(_ request: SKRequest) {
|
|
|
|
reloadReceipt()
|
|
|
|
inApp.restorePurchases { [weak self] (finished, _, error) in
|
|
|
|
guard finished else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self?.restoreCompletionHandler?(error)
|
|
|
|
self?.restoreCompletionHandler = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func request(_ request: SKRequest, didFailWithError error: Error) {
|
|
|
|
restoreCompletionHandler?(error)
|
|
|
|
restoreCompletionHandler = nil
|
|
|
|
}
|
|
|
|
}
|