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.
|
|
|
|
// Copyright (c) 2019 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/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
class ProductManager: NSObject {
|
|
|
|
private static let lastFullVersionNumber = "1.8.1"
|
|
|
|
|
|
|
|
private static let lastFullVersionBuild = "2016"
|
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-26 23:31:30 +00:00
|
|
|
private var purchasedAppVersion: String?
|
|
|
|
|
2019-10-30 10:53:49 +00:00
|
|
|
private(set) var purchasedFeatures: Set<Product>
|
|
|
|
|
|
|
|
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-26 23:31:30 +00:00
|
|
|
purchasedAppVersion = nil
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func listProducts(completionHandler: (([SKProduct]) -> Void)?) {
|
|
|
|
guard inApp.products.isEmpty else {
|
|
|
|
completionHandler?(inApp.products)
|
|
|
|
return
|
|
|
|
}
|
2019-10-26 23:24:32 +00:00
|
|
|
inApp.requestProducts(withIdentifiers: Product.all) { _ in
|
2019-10-11 08:56:23 +00:00
|
|
|
completionHandler?(self.inApp.products)
|
|
|
|
}
|
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-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
|
|
|
|
|
|
|
|
private func reloadReceipt() {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
purchasedAppVersion = receipt.originalAppVersion
|
|
|
|
purchasedFeatures.removeAll()
|
|
|
|
|
|
|
|
if let version = purchasedAppVersion {
|
|
|
|
log.debug("Original purchased version: \(version)")
|
|
|
|
|
|
|
|
// treat former purchases as full versions
|
|
|
|
if version <= ProductManager.lastFullVersionNumber {
|
|
|
|
purchasedFeatures.insert(.fullVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let iapReceipts = receipt.inAppPurchaseReceipts {
|
|
|
|
log.debug("In-app receipts:")
|
|
|
|
iapReceipts.forEach {
|
|
|
|
guard let pid = $0.productIdentifier, let date = $0.originalPurchaseDate else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.debug("\t\(pid) [\(date)]")
|
|
|
|
}
|
|
|
|
for r in iapReceipts {
|
|
|
|
guard let pid = r.productIdentifier, let product = Product(rawValue: pid) else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
purchasedFeatures.insert(product)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("Purchased features: \(purchasedFeatures)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFullVersion() -> Bool {
|
|
|
|
guard !AppConstants.Flags.isBeta else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return purchasedFeatures.contains(.fullVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEligible(forFeature feature: Product) -> Bool {
|
|
|
|
guard !isFullVersion() else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return purchasedFeatures.contains(feature)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEligible(forProvider name: Infrastructure.Name) -> Bool {
|
|
|
|
guard !isFullVersion() else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return purchasedFeatures.contains {
|
|
|
|
return $0.rawValue.hasSuffix("providers.\(name.rawValue)")
|
|
|
|
}
|
2019-04-06 21:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|