PaymentsController.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // PaymentsController.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. struct Payment: Hashable {
  27. let product: SKProduct
  28. let paymentDiscount: PaymentDiscount?
  29. let quantity: Int
  30. let atomically: Bool
  31. let applicationUsername: String
  32. let simulatesAskToBuyInSandbox: Bool
  33. let callback: (TransactionResult) -> Void
  34. func hash(into hasher: inout Hasher) {
  35. hasher.combine(product)
  36. hasher.combine(quantity)
  37. hasher.combine(atomically)
  38. hasher.combine(applicationUsername)
  39. hasher.combine(simulatesAskToBuyInSandbox)
  40. }
  41. static func == (lhs: Payment, rhs: Payment) -> Bool {
  42. return lhs.product.productIdentifier == rhs.product.productIdentifier
  43. }
  44. }
  45. public struct PaymentDiscount {
  46. let discount: AnyObject?
  47. @available(iOS 12.2, tvOS 12.2, OSX 10.14.4, watchOS 6.2, macCatalyst 13.0, *)
  48. public init(discount: SKPaymentDiscount) {
  49. self.discount = discount
  50. }
  51. private init() {
  52. self.discount = nil
  53. }
  54. }
  55. class PaymentsController: TransactionController {
  56. private var payments: [Payment] = []
  57. private func findPaymentIndex(withProductIdentifier identifier: String) -> Int? {
  58. for payment in payments where payment.product.productIdentifier == identifier {
  59. return payments.firstIndex(of: payment)
  60. }
  61. return nil
  62. }
  63. func hasPayment(_ payment: Payment) -> Bool {
  64. return findPaymentIndex(withProductIdentifier: payment.product.productIdentifier) != nil
  65. }
  66. func append(_ payment: Payment) {
  67. payments.append(payment)
  68. }
  69. func processTransaction(_ transaction: SKPaymentTransaction, on paymentQueue: PaymentQueue) -> Bool {
  70. let transactionProductIdentifier = transaction.payment.productIdentifier
  71. guard let paymentIndex = findPaymentIndex(withProductIdentifier: transactionProductIdentifier) else {
  72. return false
  73. }
  74. let payment = payments[paymentIndex]
  75. let transactionState = transaction.transactionState
  76. if transactionState == .purchased {
  77. let purchase = PurchaseDetails(productId: transactionProductIdentifier, quantity: transaction.payment.quantity, product: payment.product, transaction: transaction, originalTransaction: transaction.original, needsFinishTransaction: !payment.atomically)
  78. payment.callback(.purchased(purchase: purchase))
  79. if payment.atomically {
  80. paymentQueue.finishTransaction(transaction)
  81. }
  82. payments.remove(at: paymentIndex)
  83. return true
  84. }
  85. if transactionState == .restored {
  86. print("Unexpected restored transaction for payment \(transactionProductIdentifier)")
  87. let purchase = PurchaseDetails(productId: transactionProductIdentifier, quantity: transaction.payment.quantity, product: payment.product, transaction: transaction, originalTransaction: transaction.original, needsFinishTransaction: !payment.atomically)
  88. payment.callback(.purchased(purchase: purchase))
  89. if payment.atomically {
  90. paymentQueue.finishTransaction(transaction)
  91. }
  92. payments.remove(at: paymentIndex)
  93. return true
  94. }
  95. if transactionState == .failed {
  96. payment.callback(.failed(error: transactionError(for: transaction.error as NSError?)))
  97. paymentQueue.finishTransaction(transaction)
  98. payments.remove(at: paymentIndex)
  99. return true
  100. }
  101. return false
  102. }
  103. func transactionError(for error: NSError?) -> SKError {
  104. let message = "Unknown error"
  105. let altError = NSError(domain: SKErrorDomain, code: SKError.unknown.rawValue, userInfo: [ NSLocalizedDescriptionKey: message ])
  106. let nsError = error ?? altError
  107. return SKError(_nsError: nsError)
  108. }
  109. func processTransactions(_ transactions: [SKPaymentTransaction], on paymentQueue: PaymentQueue) -> [SKPaymentTransaction] {
  110. return transactions.filter { !processTransaction($0, on: paymentQueue) }
  111. }
  112. }