InAppCompleteTransactionsObserver.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // InAppCompleteTransactionsObserver.swift
  3. // SwiftyStoreKit
  4. //
  5. // Copyright (c) 2016 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 StoreKit
  25. extension SKPaymentTransactionState {
  26. var stringValue: String {
  27. switch self {
  28. case .purchasing: return "Purchasing"
  29. case .purchased: return "Purchased"
  30. case .failed: return "Failed"
  31. case .restored: return "Restored"
  32. case .deferred: return "Deferred"
  33. }
  34. }
  35. }
  36. class InAppCompleteTransactionsObserver: NSObject, SKPaymentTransactionObserver {
  37. private var callbackCalled: Bool = false
  38. typealias TransactionsCallback = ([Product]) -> ()
  39. var paymentQueue: SKPaymentQueue {
  40. return SKPaymentQueue.default()
  41. }
  42. let atomically: Bool
  43. deinit {
  44. paymentQueue.remove(self)
  45. }
  46. let callback: TransactionsCallback
  47. init(atomically: Bool, callback: @escaping TransactionsCallback) {
  48. self.atomically = atomically
  49. self.callback = callback
  50. super.init()
  51. paymentQueue.add(self)
  52. }
  53. func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
  54. if callbackCalled {
  55. return
  56. }
  57. if SwiftyStoreKit.hasInFlightPayments {
  58. return
  59. }
  60. var completedTransactions: [Product] = []
  61. for transaction in transactions {
  62. let transactionState = transaction.transactionState
  63. if transactionState != .purchasing {
  64. let product = Product(productId: transaction.payment.productIdentifier, transaction: transaction, needsFinishTransaction: !atomically)
  65. completedTransactions.append(product)
  66. print("Finishing transaction for payment \"\(transaction.payment.productIdentifier)\" with state: \(transactionState.stringValue)")
  67. paymentQueue.finishTransaction(transaction)
  68. }
  69. }
  70. callbackCalled = true
  71. callback(completedTransactions)
  72. }
  73. }