PaymentsController.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 quantity: Int
  29. let atomically: Bool
  30. let applicationUsername: String
  31. let simulatesAskToBuyInSandbox: Bool
  32. let callback: (TransactionResult) -> Void
  33. func hash(into hasher: inout Hasher) {
  34. hasher.combine(product)
  35. hasher.combine(quantity)
  36. hasher.combine(atomically)
  37. hasher.combine(applicationUsername)
  38. hasher.combine(simulatesAskToBuyInSandbox)
  39. }
  40. static func == (lhs: Payment, rhs: Payment) -> Bool {
  41. return lhs.product.productIdentifier == rhs.product.productIdentifier
  42. }
  43. }
  44. class PaymentsController: TransactionController {
  45. private var payments: [Payment] = []
  46. private func findPaymentIndex(withProductIdentifier identifier: String) -> Int? {
  47. for payment in payments where payment.product.productIdentifier == identifier {
  48. return payments.firstIndex(of: payment)
  49. }
  50. return nil
  51. }
  52. func hasPayment(_ payment: Payment) -> Bool {
  53. return findPaymentIndex(withProductIdentifier: payment.product.productIdentifier) != nil
  54. }
  55. func append(_ payment: Payment) {
  56. payments.append(payment)
  57. }
  58. func processTransaction(_ transaction: SKPaymentTransaction, on paymentQueue: PaymentQueue) -> Bool {
  59. let transactionProductIdentifier = transaction.payment.productIdentifier
  60. guard let paymentIndex = findPaymentIndex(withProductIdentifier: transactionProductIdentifier) else {
  61. return false
  62. }
  63. let payment = payments[paymentIndex]
  64. let transactionState = transaction.transactionState
  65. if transactionState == .purchased {
  66. let purchase = PurchaseDetails(productId: transactionProductIdentifier, quantity: transaction.payment.quantity, product: payment.product, transaction: transaction, originalTransaction: transaction.original, needsFinishTransaction: !payment.atomically)
  67. payment.callback(.purchased(purchase: purchase))
  68. if payment.atomically {
  69. paymentQueue.finishTransaction(transaction)
  70. }
  71. payments.remove(at: paymentIndex)
  72. return true
  73. }
  74. if transactionState == .failed {
  75. payment.callback(.failed(error: transactionError(for: transaction.error as NSError?)))
  76. paymentQueue.finishTransaction(transaction)
  77. payments.remove(at: paymentIndex)
  78. return true
  79. }
  80. if transactionState == .restored {
  81. print("Unexpected restored transaction for payment \(transactionProductIdentifier)")
  82. }
  83. return false
  84. }
  85. func transactionError(for error: NSError?) -> SKError {
  86. let message = "Unknown error"
  87. let altError = NSError(domain: SKErrorDomain, code: SKError.unknown.rawValue, userInfo: [ NSLocalizedDescriptionKey: message ])
  88. let nsError = error ?? altError
  89. return SKError(_nsError: nsError)
  90. }
  91. func processTransactions(_ transactions: [SKPaymentTransaction], on paymentQueue: PaymentQueue) -> [SKPaymentTransaction] {
  92. return transactions.filter { !processTransaction($0, on: paymentQueue) }
  93. }
  94. }