PaymentQueueController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // PaymentQueueController.swift
  3. // SwiftyStoreKit
  4. //
  5. // Copyright (c) 2017 Andrea Bizzotto (bizz84@gmail.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import StoreKit
  26. protocol TransactionController {
  27. /// Process the supplied transactions on a given queue.
  28. /// - parameter transactions: transactions to process
  29. /// - parameter paymentQueue: payment queue for finishing transactions
  30. /// - returns: array of unhandled transactions
  31. func processTransactions(_ transactions: [SKPaymentTransaction], on paymentQueue: PaymentQueue) -> [SKPaymentTransaction]
  32. }
  33. public enum TransactionResult {
  34. case purchased(purchase: PurchaseDetails)
  35. case restored(purchase: Purchase)
  36. case failed(error: SKError)
  37. }
  38. public protocol PaymentQueue: AnyObject {
  39. func add(_ observer: SKPaymentTransactionObserver)
  40. func remove(_ observer: SKPaymentTransactionObserver)
  41. func add(_ payment: SKPayment)
  42. func start(_ downloads: [SKDownload])
  43. func pause(_ downloads: [SKDownload])
  44. func resume(_ downloads: [SKDownload])
  45. func cancel(_ downloads: [SKDownload])
  46. func restoreCompletedTransactions(withApplicationUsername username: String?)
  47. func finishTransaction(_ transaction: SKPaymentTransaction)
  48. }
  49. extension SKPaymentQueue: PaymentQueue {
  50. #if os(watchOS) && swift(<5.3)
  51. public func resume(_ downloads: [SKDownload]) {
  52. resumeDownloads(downloads)
  53. }
  54. #endif
  55. }
  56. extension SKPaymentTransaction {
  57. open override var debugDescription: String {
  58. let transactionId = transactionIdentifier ?? "null"
  59. return "productId: \(payment.productIdentifier), transactionId: \(transactionId), state: \(transactionState), date: \(String(describing: transactionDate))"
  60. }
  61. }
  62. extension SKPaymentTransactionState: CustomDebugStringConvertible {
  63. public var debugDescription: String {
  64. switch self {
  65. case .purchasing: return "purchasing"
  66. case .purchased: return "purchased"
  67. case .failed: return "failed"
  68. case .restored: return "restored"
  69. case .deferred: return "deferred"
  70. @unknown default: return "default"
  71. }
  72. }
  73. }
  74. struct EntitlementRevocation {
  75. let callback: ([String]) -> Void
  76. init(callback: @escaping ([String]) -> Void) {
  77. self.callback = callback
  78. }
  79. }
  80. class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
  81. private let paymentsController: PaymentsController
  82. private let restorePurchasesController: RestorePurchasesController
  83. private let completeTransactionsController: CompleteTransactionsController
  84. unowned let paymentQueue: PaymentQueue
  85. private var entitlementRevocation: EntitlementRevocation?
  86. deinit {
  87. paymentQueue.remove(self)
  88. }
  89. init(paymentQueue: PaymentQueue = SKPaymentQueue.default(),
  90. paymentsController: PaymentsController = PaymentsController(),
  91. restorePurchasesController: RestorePurchasesController = RestorePurchasesController(),
  92. completeTransactionsController: CompleteTransactionsController = CompleteTransactionsController()) {
  93. self.paymentQueue = paymentQueue
  94. self.paymentsController = paymentsController
  95. self.restorePurchasesController = restorePurchasesController
  96. self.completeTransactionsController = completeTransactionsController
  97. super.init()
  98. paymentQueue.add(self)
  99. }
  100. private func assertCompleteTransactionsWasCalled() {
  101. let message = "SwiftyStoreKit.completeTransactions() must be called when the app launches."
  102. assert(completeTransactionsController.completeTransactions != nil, message)
  103. }
  104. func startPayment(_ payment: Payment) {
  105. assertCompleteTransactionsWasCalled()
  106. let skPayment = SKMutablePayment(product: payment.product)
  107. skPayment.applicationUsername = payment.applicationUsername
  108. skPayment.quantity = payment.quantity
  109. if #available(iOS 12.2, tvOS 12.2, OSX 10.14.4, watchOS 6.2, *) {
  110. if let discount = payment.paymentDiscount?.discount as? SKPaymentDiscount {
  111. skPayment.paymentDiscount = discount
  112. }
  113. }
  114. #if os(iOS) || os(tvOS) || os(watchOS)
  115. if #available(iOS 8.3, watchOS 6.2, *) {
  116. skPayment.simulatesAskToBuyInSandbox = payment.simulatesAskToBuyInSandbox
  117. }
  118. #endif
  119. paymentQueue.add(skPayment)
  120. paymentsController.append(payment)
  121. }
  122. func onEntitlementRevocation(_ revocation: EntitlementRevocation) {
  123. guard entitlementRevocation == nil else {
  124. print("SwiftyStoreKit.onEntitlementRevocation() should only be called once when the app launches. Ignoring this call")
  125. return
  126. }
  127. self.entitlementRevocation = revocation
  128. }
  129. func restorePurchases(_ restorePurchases: RestorePurchases) {
  130. assertCompleteTransactionsWasCalled()
  131. if restorePurchasesController.restorePurchases != nil {
  132. return
  133. }
  134. paymentQueue.restoreCompletedTransactions(withApplicationUsername: restorePurchases.applicationUsername)
  135. restorePurchasesController.restorePurchases = restorePurchases
  136. }
  137. func completeTransactions(_ completeTransactions: CompleteTransactions) {
  138. guard completeTransactionsController.completeTransactions == nil else {
  139. print("SwiftyStoreKit.completeTransactions() should only be called once when the app launches. Ignoring this call")
  140. return
  141. }
  142. completeTransactionsController.completeTransactions = completeTransactions
  143. }
  144. func finishTransaction(_ transaction: PaymentTransaction) {
  145. guard let skTransaction = transaction as? SKPaymentTransaction else {
  146. print("Object is not a SKPaymentTransaction: \(transaction)")
  147. return
  148. }
  149. paymentQueue.finishTransaction(skTransaction)
  150. }
  151. func start(_ downloads: [SKDownload]) {
  152. paymentQueue.start(downloads)
  153. }
  154. func pause(_ downloads: [SKDownload]) {
  155. paymentQueue.pause(downloads)
  156. }
  157. func resume(_ downloads: [SKDownload]) {
  158. paymentQueue.resume(downloads)
  159. }
  160. func cancel(_ downloads: [SKDownload]) {
  161. paymentQueue.cancel(downloads)
  162. }
  163. var shouldAddStorePaymentHandler: ShouldAddStorePaymentHandler?
  164. var updatedDownloadsHandler: UpdatedDownloadsHandler?
  165. // MARK: - SKPaymentTransactionObserver
  166. func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
  167. /*
  168. * Some notes about how requests are processed by SKPaymentQueue:
  169. *
  170. * SKPaymentQueue is used to queue payments or restore purchases requests.
  171. * Payments are processed serially and in-order and require user interaction.
  172. * Restore purchases requests don't require user interaction and can jump ahead of the queue.
  173. * SKPaymentQueue rejects multiple restore purchases calls.
  174. * Having one payment queue observer for each request causes extra processing
  175. * Failed transactions only ever belong to queued payment requests.
  176. * restoreCompletedTransactionsFailedWithError is always called when a restore purchases request fails.
  177. * paymentQueueRestoreCompletedTransactionsFinished is always called following 0 or more update transactions when a restore purchases request succeeds.
  178. * A complete transactions handler is required to catch any transactions that are updated when the app is not running.
  179. * Registering a complete transactions handler when the app launches ensures that any pending transactions can be cleared.
  180. * If a complete transactions handler is missing, pending transactions can be mis-attributed to any new incoming payments or restore purchases.
  181. *
  182. * The order in which transaction updates are processed is:
  183. * 1. payments (transactionState: .purchased and .failed for matching product identifiers)
  184. * 2. restore purchases (transactionState: .restored, or restoreCompletedTransactionsFailedWithError, or paymentQueueRestoreCompletedTransactionsFinished)
  185. * 3. complete transactions (transactionState: .purchased, .failed, .restored, .deferred)
  186. * Any transactions where state == .purchasing are ignored.
  187. */
  188. var unhandledTransactions = transactions.filter { $0.transactionState != .purchasing }
  189. if unhandledTransactions.count > 0 {
  190. unhandledTransactions = paymentsController.processTransactions(transactions, on: paymentQueue)
  191. unhandledTransactions = restorePurchasesController.processTransactions(unhandledTransactions, on: paymentQueue)
  192. unhandledTransactions = completeTransactionsController.processTransactions(unhandledTransactions, on: paymentQueue)
  193. if unhandledTransactions.count > 0 {
  194. let strings = unhandledTransactions.map { $0.debugDescription }.joined(separator: "\n")
  195. print("unhandledTransactions:\n\(strings)")
  196. }
  197. }
  198. }
  199. func paymentQueue(_ queue: SKPaymentQueue, didRevokeEntitlementsForProductIdentifiers productIdentifiers: [String]) {
  200. self.entitlementRevocation?.callback(productIdentifiers)
  201. }
  202. func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
  203. }
  204. func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
  205. restorePurchasesController.restoreCompletedTransactionsFailed(withError: error)
  206. }
  207. func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
  208. restorePurchasesController.restoreCompletedTransactionsFinished()
  209. }
  210. func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) {
  211. updatedDownloadsHandler?(downloads)
  212. }
  213. #if !os(watchOS)
  214. func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {
  215. return shouldAddStorePaymentHandler?(payment, product) ?? false
  216. }
  217. #endif
  218. }