PaymentQueueController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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: class {
  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. #if os(watchOS)
  45. func resumeDownloads(_ downloads: [SKDownload])
  46. #else
  47. func resume(_ downloads: [SKDownload])
  48. #endif
  49. func cancel(_ downloads: [SKDownload])
  50. func restoreCompletedTransactions(withApplicationUsername username: String?)
  51. func finishTransaction(_ transaction: SKPaymentTransaction)
  52. }
  53. extension SKPaymentQueue: PaymentQueue { }
  54. extension SKPaymentTransaction {
  55. open override var debugDescription: String {
  56. let transactionId = transactionIdentifier ?? "null"
  57. return "productId: \(payment.productIdentifier), transactionId: \(transactionId), state: \(transactionState), date: \(String(describing: transactionDate))"
  58. }
  59. }
  60. extension SKPaymentTransactionState: CustomDebugStringConvertible {
  61. public var debugDescription: String {
  62. switch self {
  63. case .purchasing: return "purchasing"
  64. case .purchased: return "purchased"
  65. case .failed: return "failed"
  66. case .restored: return "restored"
  67. case .deferred: return "deferred"
  68. @unknown default: return "default"
  69. }
  70. }
  71. }
  72. class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
  73. private let paymentsController: PaymentsController
  74. private let restorePurchasesController: RestorePurchasesController
  75. private let completeTransactionsController: CompleteTransactionsController
  76. unowned let paymentQueue: PaymentQueue
  77. deinit {
  78. paymentQueue.remove(self)
  79. }
  80. init(paymentQueue: PaymentQueue = SKPaymentQueue.default(),
  81. paymentsController: PaymentsController = PaymentsController(),
  82. restorePurchasesController: RestorePurchasesController = RestorePurchasesController(),
  83. completeTransactionsController: CompleteTransactionsController = CompleteTransactionsController()) {
  84. self.paymentQueue = paymentQueue
  85. self.paymentsController = paymentsController
  86. self.restorePurchasesController = restorePurchasesController
  87. self.completeTransactionsController = completeTransactionsController
  88. super.init()
  89. paymentQueue.add(self)
  90. }
  91. private func assertCompleteTransactionsWasCalled() {
  92. let message = "SwiftyStoreKit.completeTransactions() must be called when the app launches."
  93. assert(completeTransactionsController.completeTransactions != nil, message)
  94. }
  95. func startPayment(_ payment: Payment) {
  96. assertCompleteTransactionsWasCalled()
  97. let skPayment = SKMutablePayment(product: payment.product)
  98. skPayment.applicationUsername = payment.applicationUsername
  99. skPayment.quantity = payment.quantity
  100. if #available(iOS 12.2, tvOS 12.2, OSX 10.14.4, *) {
  101. if let discount = payment.paymentDiscount?.discount as? SKPaymentDiscount {
  102. skPayment.paymentDiscount = discount
  103. }
  104. }
  105. #if os(iOS) || os(tvOS)
  106. if #available(iOS 8.3, *) {
  107. skPayment.simulatesAskToBuyInSandbox = payment.simulatesAskToBuyInSandbox
  108. }
  109. #endif
  110. paymentQueue.add(skPayment)
  111. paymentsController.append(payment)
  112. }
  113. func restorePurchases(_ restorePurchases: RestorePurchases) {
  114. assertCompleteTransactionsWasCalled()
  115. if restorePurchasesController.restorePurchases != nil {
  116. return
  117. }
  118. paymentQueue.restoreCompletedTransactions(withApplicationUsername: restorePurchases.applicationUsername)
  119. restorePurchasesController.restorePurchases = restorePurchases
  120. }
  121. func completeTransactions(_ completeTransactions: CompleteTransactions) {
  122. guard completeTransactionsController.completeTransactions == nil else {
  123. print("SwiftyStoreKit.completeTransactions() should only be called once when the app launches. Ignoring this call")
  124. return
  125. }
  126. completeTransactionsController.completeTransactions = completeTransactions
  127. }
  128. func finishTransaction(_ transaction: PaymentTransaction) {
  129. guard let skTransaction = transaction as? SKPaymentTransaction else {
  130. print("Object is not a SKPaymentTransaction: \(transaction)")
  131. return
  132. }
  133. paymentQueue.finishTransaction(skTransaction)
  134. }
  135. func start(_ downloads: [SKDownload]) {
  136. paymentQueue.start(downloads)
  137. }
  138. func pause(_ downloads: [SKDownload]) {
  139. paymentQueue.pause(downloads)
  140. }
  141. func resume(_ downloads: [SKDownload]) {
  142. #if os(watchOS)
  143. paymentQueue.resumeDownloads(downloads)
  144. #else
  145. paymentQueue.resume(downloads)
  146. #endif
  147. }
  148. func cancel(_ downloads: [SKDownload]) {
  149. paymentQueue.cancel(downloads)
  150. }
  151. var shouldAddStorePaymentHandler: ShouldAddStorePaymentHandler?
  152. var updatedDownloadsHandler: UpdatedDownloadsHandler?
  153. // MARK: SKPaymentTransactionObserver
  154. func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
  155. /*
  156. * Some notes about how requests are processed by SKPaymentQueue:
  157. *
  158. * SKPaymentQueue is used to queue payments or restore purchases requests.
  159. * Payments are processed serially and in-order and require user interaction.
  160. * Restore purchases requests don't require user interaction and can jump ahead of the queue.
  161. * SKPaymentQueue rejects multiple restore purchases calls.
  162. * Having one payment queue observer for each request causes extra processing
  163. * Failed transactions only ever belong to queued payment requests.
  164. * restoreCompletedTransactionsFailedWithError is always called when a restore purchases request fails.
  165. * paymentQueueRestoreCompletedTransactionsFinished is always called following 0 or more update transactions when a restore purchases request succeeds.
  166. * A complete transactions handler is require to catch any transactions that are updated when the app is not running.
  167. * Registering a complete transactions handler when the app launches ensures that any pending transactions can be cleared.
  168. * If a complete transactions handler is missing, pending transactions can be mis-attributed to any new incoming payments or restore purchases.
  169. *
  170. * The order in which transaction updates are processed is:
  171. * 1. payments (transactionState: .purchased and .failed for matching product identifiers)
  172. * 2. restore purchases (transactionState: .restored, or restoreCompletedTransactionsFailedWithError, or paymentQueueRestoreCompletedTransactionsFinished)
  173. * 3. complete transactions (transactionState: .purchased, .failed, .restored, .deferred)
  174. * Any transactions where state == .purchasing are ignored.
  175. */
  176. var unhandledTransactions = transactions.filter { $0.transactionState != .purchasing }
  177. if unhandledTransactions.count > 0 {
  178. unhandledTransactions = paymentsController.processTransactions(transactions, on: paymentQueue)
  179. unhandledTransactions = restorePurchasesController.processTransactions(unhandledTransactions, on: paymentQueue)
  180. unhandledTransactions = completeTransactionsController.processTransactions(unhandledTransactions, on: paymentQueue)
  181. if unhandledTransactions.count > 0 {
  182. let strings = unhandledTransactions.map { $0.debugDescription }.joined(separator: "\n")
  183. print("unhandledTransactions:\n\(strings)")
  184. }
  185. }
  186. }
  187. func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
  188. }
  189. func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
  190. restorePurchasesController.restoreCompletedTransactionsFailed(withError: error)
  191. }
  192. func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
  193. restorePurchasesController.restoreCompletedTransactionsFinished()
  194. }
  195. func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) {
  196. updatedDownloadsHandler?(downloads)
  197. }
  198. #if os(iOS) && !targetEnvironment(macCatalyst)
  199. func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {
  200. return shouldAddStorePaymentHandler?(payment, product) ?? false
  201. }
  202. #endif
  203. }