IAPSK2.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. import Foundation
  19. import StoreKit
  20. @objc
  21. @available(iOS 15.0, *)
  22. public final class IAPSK2: NSObject {
  23. private var isInitialized: Bool
  24. private var updateListenerTask: Task<Void, Error>?
  25. private var products: [Product] = []
  26. private var productOfferings: [String: Product] = [:]
  27. @objc public static let shared = IAPSK2()
  28. @objc public func initialize() {
  29. if isInitialized || products.count > 0 {
  30. return
  31. }
  32. print("Initializing IAP SK2...")
  33. Task {
  34. await fetchProducts(productIdentifiers: ["com.fbsdk.unity.consumable", "com.fbsdk.unity.nonconsumable", "com.fbsdk.unity.subscription"])
  35. isInitialized = true
  36. }
  37. }
  38. @objc public func purchaseConsumable() {
  39. guard let product = productOfferings["com.fbsdk.unity.consumable"] else {
  40. return
  41. }
  42. makePurchase(product: product)
  43. }
  44. @objc public func purchaseNonConsumable() {
  45. guard let product = productOfferings["com.fbsdk.unity.nonconsumable"] else {
  46. return
  47. }
  48. makePurchase(product: product)
  49. }
  50. @objc public func purchaseSubscription() {
  51. guard let product = productOfferings["com.fbsdk.unity.subscription"] else {
  52. return
  53. }
  54. makePurchase(product: product)
  55. }
  56. private override init() {
  57. isInitialized = false
  58. super.init()
  59. updateListenerTask = listenForTransactionUpdates()
  60. }
  61. }
  62. @available(iOS 15.0, *)
  63. public extension IAPSK2 {
  64. private func makePurchase(product: Product) {
  65. Task {
  66. guard let purchaseResult = try? await product.purchase() else {
  67. return
  68. }
  69. await handlePurchaseResult(purchaseResult: purchaseResult)
  70. }
  71. }
  72. private func handlePurchaseResult(purchaseResult: Product.PurchaseResult) async {
  73. switch purchaseResult {
  74. case let .success(verificationResult):
  75. switch verificationResult {
  76. case .unverified:
  77. return
  78. case let .verified(transaction):
  79. await transaction.finish()
  80. print("Successfully purchased \(transaction.productID) with id \(transaction.id)")
  81. return
  82. }
  83. case .userCancelled: return
  84. case .pending: return
  85. default: return
  86. }
  87. }
  88. private func fetchProducts(productIdentifiers: [String]) async {
  89. guard let fetchedProducts = try? await Product.products(for: productIdentifiers) else {
  90. return
  91. }
  92. products = fetchedProducts
  93. for product in fetchedProducts {
  94. productOfferings[product.id] = product
  95. }
  96. print("Fetched Product Offerings: \(self.productOfferings)")
  97. }
  98. private func listenForTransactionUpdates() -> Task<Void, Error> {
  99. return Task.detached {
  100. for await verificationResult in Transaction.updates {
  101. switch verificationResult {
  102. case .unverified: continue
  103. case let .verified(transaction):
  104. await transaction.finish()
  105. }
  106. }
  107. }
  108. }
  109. }