-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathProductManager.swift
337 lines (284 loc) · 10.4 KB
/
ProductManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// ProductManager.swift
// Passepartout
//
// Created by Davide De Rosa on 4/6/19.
// Copyright (c) 2023 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 Combine
import Foundation
import PassepartoutCore
import PassepartoutProviders
@MainActor
public final class ProductManager: NSObject, ObservableObject {
private let inApp: any LocalInApp
private let receiptReader: ReceiptReader
private let overriddenAppType: AppType?
public let buildProducts: BuildProducts
public let didRefundProducts = PassthroughSubject<Void, Never>()
@Published public private(set) var appType: AppType
@Published public private(set) var isRefreshingProducts = false
@Published public private(set) var products: [InAppProduct]
//
public var purchasedProductIdentifiers: Set<String> {
Set(purchasedFeatures.map(\.rawValue))
}
private var purchasedAppBuild: Int?
private var purchasedFeatures: Set<LocalProduct>
private var purchaseDates: [LocalProduct: Date]
private var cancelledPurchases: Set<LocalProduct>? {
willSet {
guard cancelledPurchases != nil else {
return
}
guard let newCancelledPurchases = newValue, newCancelledPurchases != cancelledPurchases else {
pp_log.debug("No purchase was refunded")
return
}
detectRefunds(newCancelledPurchases)
}
}
public init(inApp: any LocalInApp,
receiptReader: ReceiptReader,
overriddenAppType: AppType? = nil,
buildProducts: BuildProducts) {
self.overriddenAppType = overriddenAppType
self.receiptReader = receiptReader
self.buildProducts = buildProducts
appType = .undefined
products = []
self.inApp = inApp
purchasedAppBuild = nil
purchasedFeatures = []
purchaseDates = [:]
cancelledPurchases = nil
super.init()
inApp.setTransactionsObserver { [weak self] in
self?.reloadReceipt()
}
reloadReceipt()
Task {
await refreshProducts()
let isBeta = await SandboxChecker().isBeta
appType = overriddenAppType ?? (isBeta ? .beta : .freemium)
pp_log.info("App type: \(appType)")
reloadReceipt()
}
}
// MARK: Main interface
public func canMakePayments() -> Bool {
inApp.canMakePurchases()
}
public func refreshProducts() async {
let ids = LocalProduct.all
guard !ids.isEmpty else {
return
}
guard products.isEmpty else {
pp_log.debug("In-app products already available, not refreshing")
return
}
isRefreshingProducts = true
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
}
}
public func product(withIdentifier identifier: LocalProduct) -> InAppProduct? {
inApp.product(withIdentifier: identifier)
}
public func featureProducts(including: (LocalProduct) -> Bool) -> [InAppProduct] {
inApp.products().filter {
guard let p = LocalProduct(rawValue: $0.productIdentifier) else {
return false
}
guard including(p) else {
return false
}
guard p.isFeature else {
return false
}
return true
}
}
public func featureProducts(excluding: (LocalProduct) -> Bool) -> [InAppProduct] {
inApp.products().filter {
guard let p = LocalProduct(rawValue: $0.productIdentifier) else {
return false
}
guard !excluding(p) else {
return false
}
guard p.isFeature else {
return false
}
return true
}
}
public func purchase(_ product: InAppProduct) async throws -> InAppPurchaseResult {
guard let pid = LocalProduct(rawValue: product.productIdentifier) else {
assertionFailure("Unrecognized product: \(product)")
pp_log.warning("Unrecognized product: \(product)")
return .cancelled
}
let result = try await inApp.purchase(productWithIdentifier: pid)
reloadReceipt()
return result
}
public func restorePurchases() async throws {
try await inApp.restorePurchases()
}
public func hasPurchased(_ product: LocalProduct) -> Bool {
isActivePurchase(product)
}
public func purchaseDate(forProduct product: LocalProduct) -> Date? {
purchaseDates[product]
}
}
// MARK: In-app eligibility
extension ProductManager {
public func isEligible(forFeature feature: LocalProduct) -> Bool {
if let purchasedAppBuild = purchasedAppBuild {
if feature == .networkSettings && buildProducts.hasProduct(.networkSettings, atBuild: purchasedAppBuild) {
return true
}
}
if isIncludedInFullVersion(feature) {
return isFullVersion() || isActivePurchase(feature)
}
return isActivePurchase(feature)
}
public func isEligible(forProvider providerName: ProviderName) -> Bool {
guard providerName != .oeck else {
return true
}
return isEligible(forFeature: providerName.product)
}
public func isEligibleForFeedback() -> Bool {
appType == .beta || isPayingUser()
}
}
extension ProductManager {
func isActivePurchase(_ feature: LocalProduct) -> Bool {
purchasedFeatures.contains(feature) && cancelledPurchases?.contains(feature) != true
}
func isActivePurchase(where predicate: (LocalProduct) -> Bool) -> Bool {
purchasedFeatures.contains(where: predicate) && cancelledPurchases?.contains(where: predicate) != true
}
func isCurrentPlatformVersion() -> Bool {
isActivePurchase(isMac ? .fullVersion_macOS : .fullVersion_iOS)
}
func isIncludedInFullVersion(_ feature: LocalProduct) -> Bool {
!feature.isLegacyPlatformVersion && feature != .appleTV
}
public func isFullVersion() -> Bool {
if appType == .fullVersion {
return true
}
if isCurrentPlatformVersion() {
return true
}
return isActivePurchase(.fullVersion)
}
public func isPayingUser() -> Bool {
!purchasedFeatures.subtracting(cancelledPurchases ?? []).isEmpty
}
}
// MARK: Receipt
extension ProductManager {
public func reloadReceipt(andNotify: Bool = true) {
guard let receipt = receiptReader.receipt(for: appType) else {
pp_log.error("Could not parse App Store receipt!")
return
}
if let originalBuildNumber = receipt.originalBuildNumber {
purchasedAppBuild = originalBuildNumber
}
purchasedFeatures.removeAll()
var newCancelledPurchases: Set<LocalProduct> = []
if let buildNumber = purchasedAppBuild {
pp_log.debug("Original purchased build: \(buildNumber)")
// assume some purchases by build number
buildProducts.products(atBuild: buildNumber).forEach {
purchasedFeatures.insert($0)
}
}
if let iapReceipts = receipt.purchaseReceipts {
purchaseDates.removeAll()
pp_log.debug("In-app receipts:")
iapReceipts.forEach {
guard let pid = $0.productIdentifier, let product = LocalProduct(rawValue: pid) else {
return
}
if let cancellationDate = $0.cancellationDate {
pp_log.debug("\t\(pid) [cancelled on: \(cancellationDate)]")
newCancelledPurchases.insert(product)
return
}
if let purchaseDate = $0.originalPurchaseDate {
pp_log.debug("\t\(pid) [purchased on: \(purchaseDate)]")
purchaseDates[product] = purchaseDate
}
purchasedFeatures.insert(product)
}
}
pp_log.info("Purchased features: \(purchasedFeatures)")
if andNotify {
objectWillChange.send()
}
cancelledPurchases = newCancelledPurchases
}
}
// MARK: Helpers
private extension ProductManager {
var isMac: Bool {
#if targetEnvironment(macCatalyst)
true
#else
false
#endif
}
// FIXME: in-app, this is incomplete
func detectRefunds(_ refunds: Set<LocalProduct>) {
let isEligibleForFullVersion = isFullVersion()
let hasCancelledFullVersion: Bool
let hasCancelledTrustedNetworks: Bool
if isMac {
hasCancelledFullVersion = !isEligibleForFullVersion && (
refunds.contains(.fullVersion) || refunds.contains(.fullVersion_macOS)
)
hasCancelledTrustedNetworks = false
} else {
hasCancelledFullVersion = !isEligibleForFullVersion && (
refunds.contains(.fullVersion) || refunds.contains(.fullVersion_iOS)
)
hasCancelledTrustedNetworks = !isEligibleForFullVersion && refunds.contains(.trustedNetworks)
}
// review features and potentially revert them if they were used (Siri is handled in AppDelegate)
if hasCancelledFullVersion || hasCancelledTrustedNetworks {
didRefundProducts.send()
}
}
}