TransactionObserver.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // TransactionObserver.swift
  3. // PDFReaderPro
  4. //
  5. // Created by zenghong on 1/8/25.
  6. //
  7. import Foundation
  8. import SwiftUI
  9. import StoreKit
  10. @available(macOS 12.0, *)
  11. @objc final class TransactionObserver: NSObject {
  12. var updates: Task<Void, Never>? = nil
  13. // @Environment(\.displayStoreKitMessage) private var displayStoreMessage
  14. static let shared = TransactionObserver()
  15. override init() {
  16. super.init()
  17. updates = newTransactionListenerTask()
  18. }
  19. deinit {
  20. // Cancel the update handling task when you deinitialize the class.
  21. updates?.cancel()
  22. }
  23. private func newTransactionListenerTask() -> Task<Void, Never> {
  24. Task(priority: .background) {
  25. for await verificationResult in Transaction.updates {
  26. self.handle(updatedTransaction: verificationResult)
  27. }
  28. // for await message in StoreKit.Message.messages {
  29. // if message.reason != .winBackOffer {
  30. // Ask the system to display messages now.
  31. // try? await displayStoreMessage(message)
  32. // }
  33. // }
  34. }
  35. }
  36. private func handle(updatedTransaction verificationResult: VerificationResult<StoreKit.Transaction>) {
  37. guard case .verified(let transaction) = verificationResult else {
  38. // Ignore unverified transactions.
  39. return
  40. }
  41. print("同步购买信息:\(transaction)")
  42. if let revocationDate = transaction.revocationDate {
  43. // Remove access to the product identified by transaction.productID.
  44. // Transaction.revocationReason provides details about
  45. // the revoked transaction.
  46. } else if let expirationDate = transaction.expirationDate,
  47. expirationDate < Date() {
  48. // Do nothing, this subscription is expired.
  49. print("订阅过期")
  50. // return
  51. } else if transaction.isUpgraded {
  52. // Do nothing, there is an active transaction
  53. // for a higher level of service.
  54. // return
  55. } else {
  56. // Provide access to the product identified by
  57. // transaction.productID.
  58. }
  59. let man = IAPProductsManager.default()
  60. // let currentTime = Date().timeIntervalSince1970
  61. // var isActive = false
  62. // if let expirationDate = transaction.expirationDate?.timeIntervalSince1970, expirationDate > currentTime {
  63. // isActive = true
  64. // }
  65. // if "com.pdfreaderpro.mac_free.member.all_access_pack_6months.001" == transaction.productID {
  66. // if let expirationDate = transaction.expirationDate?.timeIntervalSince1970, expirationDate > currentTime {
  67. // man?.fourDevicesAllAccessPackNew6Months_lite.isSubscribed
  68. // }
  69. // } else if "com.pdfreaderpro.mac_free.member.all_access_pack_12months.001" == transaction.productID {
  70. //
  71. // } else if "com.pdfreaderpro.mac_free.member.all_access_pack_new_6months.001" == transaction.productID {
  72. //
  73. // } else if "com.pdfreaderpro.mac_free.member.all_access_pack_advanced_6months.001" == transaction.productID {
  74. //
  75. // } else if "com.pdfreaderpro.mac_free.member.all_access_pack_advanced_annual.001" == transaction.productID {
  76. // if man?.fourDevicesAllAccessPackNew6Months_lite?.isSubscribed == true {
  77. // } else {
  78. // man?.fourDevicesAllAccessPackNew6Months_lite?.isSubscribed = true
  79. // }
  80. // }
  81. man?.winbackUpdateTransaction(withProdcutId: transaction.productID, expiredDate: transaction.expirationDate?.timeIntervalSince1970 ?? 0)
  82. }
  83. }