PaymentQueueController.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. public protocol TransactionController {
  27. /**
  28. * - param transactions: transactions to process
  29. * - param paymentQueue: payment queue for finishing transactions
  30. * - return: array of unhandled transactions
  31. */
  32. func processTransactions(_ transactions: [SKPaymentTransaction], on paymentQueue: PaymentQueue) -> [SKPaymentTransaction]
  33. }
  34. public enum TransactionResult {
  35. case purchased(product: Product)
  36. case restored(product: Product)
  37. case failed(error: Error)
  38. }
  39. public protocol PaymentQueue: class {
  40. func add(_ observer: SKPaymentTransactionObserver)
  41. func remove(_ observer: SKPaymentTransactionObserver)
  42. func add(_ payment: SKPayment)
  43. func restoreCompletedTransactions()
  44. func finishTransaction(_ transaction: SKPaymentTransaction)
  45. }
  46. extension SKPaymentQueue: PaymentQueue { }
  47. public class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
  48. private let paymentsController: PaymentsController
  49. private let restorePurchasesController: RestorePurchasesController
  50. unowned let paymentQueue: PaymentQueue
  51. deinit {
  52. paymentQueue.remove(self)
  53. }
  54. public init(paymentQueue: PaymentQueue = SKPaymentQueue.default(),
  55. paymentsController: PaymentsController = PaymentsController(),
  56. restorePurchasesController: RestorePurchasesController = RestorePurchasesController()) {
  57. self.paymentQueue = paymentQueue
  58. self.paymentsController = paymentsController
  59. self.restorePurchasesController = restorePurchasesController
  60. super.init()
  61. paymentQueue.add(self)
  62. }
  63. public func startPayment(_ payment: Payment) {
  64. if paymentsController.hasPayment(payment) {
  65. // return .inProgress
  66. return
  67. }
  68. let skPayment = SKMutablePayment(product: payment.product)
  69. skPayment.applicationUsername = payment.applicationUsername
  70. paymentQueue.add(skPayment)
  71. paymentsController.insert(payment)
  72. }
  73. public func startRestorePurchases(_ restorePurchases: RestorePurchases) {
  74. if restorePurchasesController.restorePurchases != nil {
  75. // return .inProgress
  76. return
  77. }
  78. paymentQueue.restoreCompletedTransactions()
  79. restorePurchasesController.restorePurchases = restorePurchases
  80. }
  81. // MARK: SKPaymentTransactionObserver
  82. public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
  83. /*
  84. The payment queue seems to process payments in-order, however any calls to restorePurchases can easily jump
  85. ahead of the queue as the user flows for restorePurchases are simpler.
  86. SKPaymentQueue rejects multiple restorePurchases calls
  87. Having one payment queue observer for each request causes extra processing
  88. Can a failed translation ever belong to a restore purchases request?
  89. No. restoreCompletedTransactionsFailedWithError is called instead.
  90. */
  91. var unhandledTransactions = paymentsController.processTransactions(transactions, on: paymentQueue)
  92. unhandledTransactions = restorePurchasesController.processTransactions(unhandledTransactions, on: paymentQueue)
  93. // TODO: Complete transactions
  94. // for transaction in transactionsExcludingPayments {
  95. //
  96. // let transactionState = transaction.transactionState
  97. //
  98. // switch transactionState {
  99. // case .purchased:
  100. //
  101. // break
  102. // case .failed:
  103. // break
  104. //
  105. // case .restored:
  106. // break
  107. //
  108. // case .purchasing:
  109. // // In progress: do nothing
  110. // break
  111. // case .deferred:
  112. // break
  113. // }
  114. //
  115. // }
  116. }
  117. public func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
  118. }
  119. public func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
  120. restorePurchasesController.restoreCompletedTransactionsFailed(withError: error)
  121. }
  122. public func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
  123. }
  124. public func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) {
  125. }
  126. }