fix
杨锴
2025-06-16 3fa53409f5132333ce6d83fff796e108ddd62090
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
//
// SwiftyStoreKit.swift
// SwiftyStoreKit
//
// Copyright (c) 2015 Andrea Bizzotto (bizz84@gmail.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
import StoreKit
 
public class SwiftyStoreKit {
    
    private let productsInfoController: ProductsInfoController
    
    fileprivate let paymentQueueController: PaymentQueueController
    
    fileprivate let receiptVerificator: InAppReceiptVerificator
    
    init(productsInfoController: ProductsInfoController = ProductsInfoController(),
         paymentQueueController: PaymentQueueController = PaymentQueueController(paymentQueue: SKPaymentQueue.default()),
         receiptVerificator: InAppReceiptVerificator = InAppReceiptVerificator()) {
        
        self.productsInfoController = productsInfoController
        self.paymentQueueController = paymentQueueController
        self.receiptVerificator = receiptVerificator
    }
    
    // MARK: private methods
    fileprivate func retrieveProductsInfo(_ productIds: Set<String>, completion: @escaping (RetrieveResults) -> Void) -> InAppProductRequest {
        return productsInfoController.retrieveProductsInfo(productIds, completion: completion)
    }
    
    fileprivate func purchaseProduct(_ productId: String, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, completion: @escaping ( PurchaseResult) -> Void) -> InAppProductRequest {
        
        return retrieveProductsInfo(Set([productId])) { result -> Void in
            if let product = result.retrievedProducts.first {
                self.purchase(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox, completion: completion)
            } else if let error = result.error {
                completion(.error(error: SKError(_nsError: error as NSError)))
            } else if let invalidProductId = result.invalidProductIDs.first {
                let userInfo = [ NSLocalizedDescriptionKey: "Invalid product id: \(invalidProductId)" ]
                let error = NSError(domain: SKErrorDomain, code: SKError.paymentInvalid.rawValue, userInfo: userInfo)
                completion(.error(error: SKError(_nsError: error)))
            } else {
                let error = NSError(domain: SKErrorDomain, code: SKError.unknown.rawValue, userInfo: nil)
                completion(.error(error: SKError(_nsError: error)))
            }
        }
    }
    
    fileprivate func purchase(product: SKProduct, quantity: Int, atomically: Bool, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, paymentDiscount: PaymentDiscount? = nil, completion: @escaping (PurchaseResult) -> Void) {
        paymentQueueController.startPayment(Payment(product: product, paymentDiscount: paymentDiscount, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox) { result in
            
            completion(self.processPurchaseResult(result))
        })
    }
    
    fileprivate func restorePurchases(atomically: Bool = true, applicationUsername: String = "", completion: @escaping (RestoreResults) -> Void) {
        
        paymentQueueController.restorePurchases(RestorePurchases(atomically: atomically, applicationUsername: applicationUsername) { results in
            
            let results = self.processRestoreResults(results)
            completion(results)
        })
    }
    
    fileprivate func completeTransactions(atomically: Bool = true, completion: @escaping ([Purchase]) -> Void) {
        
        paymentQueueController.completeTransactions(CompleteTransactions(atomically: atomically, callback: completion))
    }
    
    fileprivate func finishTransaction(_ transaction: PaymentTransaction) {
        
        paymentQueueController.finishTransaction(transaction)
    }
    
    private func processPurchaseResult(_ result: TransactionResult) -> PurchaseResult {
        switch result {
        case .purchased(let purchase):
            return .success(purchase: purchase)
        case .failed(let error):
            return .error(error: error)
        case .restored(let purchase):
            return .error(error: storeInternalError(description: "Cannot restore product \(purchase.productId) from purchase path"))
        }
    }
    
    private func processRestoreResults(_ results: [TransactionResult]) -> RestoreResults {
        var restoredPurchases: [Purchase] = []
        var restoreFailedPurchases: [(SKError, String?)] = []
        for result in results {
            switch result {
            case .purchased(let purchase):
                let error = storeInternalError(description: "Cannot purchase product \(purchase.productId) from restore purchases path")
                restoreFailedPurchases.append((error, purchase.productId))
            case .failed(let error):
                restoreFailedPurchases.append((error, nil))
            case .restored(let purchase):
                restoredPurchases.append(purchase)
            }
        }
        return RestoreResults(restoredPurchases: restoredPurchases, restoreFailedPurchases: restoreFailedPurchases)
    }
    
    private func storeInternalError(code: SKError.Code = SKError.unknown, description: String = "") -> SKError {
        let error = NSError(domain: SKErrorDomain, code: code.rawValue, userInfo: [ NSLocalizedDescriptionKey: description ])
        return SKError(_nsError: error)
    }
}
 
extension SwiftyStoreKit {
    
    // MARK: Singleton
    fileprivate static let sharedInstance = SwiftyStoreKit()
    
    // MARK: Public methods - Purchases
    
    /// Check if the current device can make payments.
    /// - returns: `false` if this device is not able or allowed to make payments
    public class var canMakePayments: Bool {
        return SKPaymentQueue.canMakePayments()
    }
    
    /// Retrieve products information
    /// - Parameter productIds: The set of product identifiers to retrieve corresponding products for
    /// - Parameter completion: handler for result
    /// - returns: A cancellable `InAppRequest` object 
    @discardableResult
    public class func retrieveProductsInfo(_ productIds: Set<String>, completion: @escaping (RetrieveResults) -> Void) -> InAppRequest {
        return sharedInstance.retrieveProductsInfo(productIds, completion: completion)
    }
    
    /// Purchase a product
    ///  - Parameter productId: productId as specified in App Store Connect
    ///  - Parameter quantity: quantity of the product to be purchased
    ///  - Parameter atomically: whether the product is purchased atomically (e.g. `finishTransaction` is called immediately)
    ///  - Parameter applicationUsername: an opaque identifier for the user’s account on your system
    ///  - Parameter completion: handler for result
    ///  - returns: A cancellable `InAppRequest` object   
    @discardableResult
    public class func purchaseProduct(_ productId: String, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, completion: @escaping (PurchaseResult) -> Void) -> InAppRequest {
        
        return sharedInstance.purchaseProduct(productId, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox, completion: completion)
    }
    
    /// Purchase a product
    ///  - Parameter product: product to be purchased
    ///  - Parameter quantity: quantity of the product to be purchased
    ///  - Parameter atomically: whether the product is purchased atomically (e.g. `finishTransaction` is called immediately)
    ///  - Parameter applicationUsername: an opaque identifier for the user’s account on your system
    ///  - Parameter product: optional discount to be applied. Must be of `SKProductPayment` type
    ///  - Parameter completion: handler for result
    public class func purchaseProduct(_ product: SKProduct, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, paymentDiscount: PaymentDiscount? = nil, completion: @escaping ( PurchaseResult) -> Void) {
        
        sharedInstance.purchase(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox, paymentDiscount: paymentDiscount, completion: completion)
    }
    
    /// Restore purchases
    ///  - Parameter atomically: whether the product is purchased atomically (e.g. `finishTransaction` is called immediately)
    ///  - Parameter applicationUsername: an opaque identifier for the user’s account on your system
    ///  - Parameter completion: handler for result
    public class func restorePurchases(atomically: Bool = true, applicationUsername: String = "", completion: @escaping (RestoreResults) -> Void) {
        
        sharedInstance.restorePurchases(atomically: atomically, applicationUsername: applicationUsername, completion: completion)
    }
    
    /// Complete transactions
    ///  - Parameter atomically: whether the product is purchased atomically (e.g. `finishTransaction` is called immediately)
    ///  - Parameter completion: handler for result
    public class func completeTransactions(atomically: Bool = true, completion: @escaping ([Purchase]) -> Void) {
        
        sharedInstance.completeTransactions(atomically: atomically, completion: completion)
    }
    
    /// Finish a transaction
    /// 
    /// Once the content has been delivered, call this method to finish a transaction that was performed non-atomically
    /// - Parameter transaction: transaction to finish
    public class func finishTransaction(_ transaction: PaymentTransaction) {
        
        sharedInstance.finishTransaction(transaction)
    }
    
    /// Register a handler for `SKPaymentQueue.shouldAddStorePayment` delegate method.
    /// - requires: iOS 11.0+
    public static var shouldAddStorePaymentHandler: ShouldAddStorePaymentHandler? {
        didSet {
            sharedInstance.paymentQueueController.shouldAddStorePaymentHandler = shouldAddStorePaymentHandler
        }
    }
    
    /// Register a handler for `paymentQueue(_:updatedDownloads:)`
    /// - seealso: `paymentQueue(_:updatedDownloads:)`
    public static var updatedDownloadsHandler: UpdatedDownloadsHandler? {
        didSet {
            sharedInstance.paymentQueueController.updatedDownloadsHandler = updatedDownloadsHandler
        }
    }
    
    public class func start(_ downloads: [SKDownload]) {
        sharedInstance.paymentQueueController.start(downloads)
    }
    public class func pause(_ downloads: [SKDownload]) {
        sharedInstance.paymentQueueController.pause(downloads)
    }
    public class func resume(_ downloads: [SKDownload]) {
        sharedInstance.paymentQueueController.resume(downloads)
    }
    public class func cancel(_ downloads: [SKDownload]) {
        sharedInstance.paymentQueueController.cancel(downloads)
    }
}
 
extension SwiftyStoreKit {
    
    // MARK: Public methods - Receipt verification
    
    /// Return receipt data from the application bundle. This is read from `Bundle.main.appStoreReceiptURL`.
    public static var localReceiptData: Data? {
        return sharedInstance.receiptVerificator.appStoreReceiptData
    }
    
    /// Verify application receipt
    /// - Parameter validator: receipt validator to use
    /// - Parameter forceRefresh: If `true`, refreshes the receipt even if one already exists.
    /// - Parameter completion: handler for result
    /// - returns: A cancellable `InAppRequest` object 
    @discardableResult
    public class func verifyReceipt(using validator: ReceiptValidator, forceRefresh: Bool = false, completion: @escaping (VerifyReceiptResult) -> Void) -> InAppRequest? {
        
        return sharedInstance.receiptVerificator.verifyReceipt(using: validator, forceRefresh: forceRefresh, completion: completion)
    }
    
    /// Fetch application receipt
    /// - Parameter forceRefresh: If true, refreshes the receipt even if one already exists.
    /// - Parameter completion: handler for result
    /// - returns: A cancellable `InAppRequest` object 
    @discardableResult
    public class func fetchReceipt(forceRefresh: Bool, completion: @escaping (FetchReceiptResult) -> Void) -> InAppRequest? {
        
        return sharedInstance.receiptVerificator.fetchReceipt(forceRefresh: forceRefresh, completion: completion)
    }
    
    ///  Verify the purchase of a Consumable or NonConsumable product in a receipt
    ///  - Parameter productId: the product id of the purchase to verify
    ///  - Parameter inReceipt: the receipt to use for looking up the purchase
    ///  - returns: A `VerifyPurchaseResult`, which may be either `notPurchased` or `purchased`.
    public class func verifyPurchase(productId: String, inReceipt receipt: ReceiptInfo) -> VerifyPurchaseResult {
        
        return InAppReceipt.verifyPurchase(productId: productId, inReceipt: receipt)
    }
    
    /**
     *  Verify the validity of a subscription (auto-renewable, free or non-renewing) in a receipt.
     *
     *  This method extracts all transactions matching the given productId and sorts them by date in descending order. It then compares the first transaction expiry date against the receipt date to determine its validity.
     *  - Parameter type: `.autoRenewable` or `.nonRenewing`.
     *  - Parameter productId: The product id of the subscription to verify.
     *  - Parameter receipt: The receipt to use for looking up the subscription.
     *  - Parameter validUntil: Date to check against the expiry date of the subscription. This is only used if a date is not found in the receipt.
     *  - returns: Either `.notPurchased` or `.purchased` / `.expired` with the expiry date found in the receipt.
     */
    public class func verifySubscription(ofType type: SubscriptionType, productId: String, inReceipt receipt: ReceiptInfo, validUntil date: Date = Date()) -> VerifySubscriptionResult {
        
        return InAppReceipt.verifySubscriptions(ofType: type, productIds: [productId], inReceipt: receipt, validUntil: date)
    }
    
    /**
     *  Verify the validity of a set of subscriptions in a receipt.
     *
     *  This method extracts all transactions matching the given productIds and sorts them by date in descending order. It then compares the first transaction expiry date against the receipt date, to determine its validity.
     *  - Note: You can use this method to check the validity of (mutually exclusive) subscriptions in a subscription group.
     *  - Remark: The type parameter determines how the expiration dates are calculated for all subscriptions. Make sure all productIds match the specified subscription type to avoid incorrect results.
     *  - Parameter type: `.autoRenewable` or `.nonRenewing`.
     *  - Parameter productIds: The product IDs of the subscriptions to verify.
     *  - Parameter receipt: The receipt to use for looking up the subscriptions
     *  - Parameter validUntil: Date to check against the expiry date of the subscriptions. This is only used if a date is not found in the receipt.
     *  - returns: Either `.notPurchased` or `.purchased` / `.expired` with the expiry date found in the receipt.
     */
    public class func verifySubscriptions(ofType type: SubscriptionType = .autoRenewable, productIds: Set<String>, inReceipt receipt: ReceiptInfo, validUntil date: Date = Date()) -> VerifySubscriptionResult {
        
        return InAppReceipt.verifySubscriptions(ofType: type, productIds: productIds, inReceipt: receipt, validUntil: date)
    }
    
    ///  Get the distinct product identifiers from receipt.
    ///
    /// This Method extracts all product identifiers. (Including cancelled ones).
    /// - Note: You can use this method to get all unique product identifiers from receipt.
    /// - Parameter type: `.autoRenewable` or `.nonRenewing`.
    /// - Parameter receipt: The receipt to use for looking up product identifiers.
    /// - returns: Either `Set<String>` or `nil`.
    public class func getDistinctPurchaseIds(ofType type: SubscriptionType = .autoRenewable, inReceipt receipt: ReceiptInfo) -> Set<String>? {
        
        return InAppReceipt.getDistinctPurchaseIds(ofType: type, inReceipt: receipt)
    }
}