PaymentQueueController.swift 10 KB

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