KMInAppPurchaseManager.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // KMInAppPurchaseManager.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/6/1.
  6. //
  7. import Cocoa
  8. import StoreKit
  9. import AuthenticationServices
  10. class KMInAppPurchaseManager: NSObject {
  11. public static let manager = KMInAppPurchaseManager()
  12. var availableProducts: [SKProduct] = []
  13. override init() {
  14. super.init()
  15. // 注册购买交易观察者
  16. SKPaymentQueue.default().add(self)
  17. }
  18. func fetchProducts() {
  19. let productIdentifiers: Set<String> = ["com.yourapp.product1", "com.yourapp.product2"]
  20. let request = SKProductsRequest(productIdentifiers: productIdentifiers)
  21. request.delegate = self
  22. request.start()
  23. }
  24. func purchaseProduct(productIdentifier: String) {
  25. guard let product = availableProducts.first(where: { $0.productIdentifier == productIdentifier }) else {
  26. // 未找到匹配的产品
  27. return
  28. }
  29. let payment = SKPayment(product: product)
  30. SKPaymentQueue.default().add(payment)
  31. }
  32. func startInAppPurchase() {
  33. let productIdentifier = "your_product_identifier"
  34. if SKPaymentQueue.canMakePayments() {
  35. if let product = availableProducts.first(where: { $0.productIdentifier == productIdentifier }) {
  36. let payment = SKPayment(product: product)
  37. SKPaymentQueue.default().add(payment)
  38. } else {
  39. // 未找到匹配的产品
  40. }
  41. } else {
  42. // 用户无法进行内购
  43. }
  44. }
  45. }
  46. extension KMInAppPurchaseManager: SKProductsRequestDelegate {
  47. func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
  48. availableProducts = response.products
  49. // 处理产品信息
  50. }
  51. func request(_ request: SKRequest, didFailWithError error: Error) {
  52. // 处理请求错误
  53. }
  54. }
  55. extension KMInAppPurchaseManager: SKPaymentTransactionObserver {
  56. func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
  57. for transaction in transactions {
  58. switch transaction.transactionState {
  59. case .purchased:
  60. // 购买成功,进行本地二次验证
  61. validatePurchase(transaction: transaction)
  62. case .failed:
  63. // 购买失败,处理错误
  64. handleError(transaction: transaction)
  65. case .restored:
  66. // 恢复购买,进行本地二次验证
  67. validatePurchase(transaction: transaction)
  68. default:
  69. break
  70. }
  71. }
  72. }
  73. func validatePurchase(transaction: SKPaymentTransaction) {
  74. // 获取购买凭证
  75. if let receiptURL = Bundle.main.appStoreReceiptURL,
  76. let receiptData = try? Data(contentsOf: receiptURL) {
  77. // 进行本地二次验证
  78. if let parsedReceipt = parseReceipt(receiptData: receiptData),
  79. let purchase = parsedReceipt["in_app"] as? [[String: Any]],
  80. let matchingPurchase = purchase.first(where: { $0["transaction_id"] as? String == transaction.transactionIdentifier }) {
  81. // 验证购买凭证,进行相应的处理
  82. if verifyPurchase(purchase: matchingPurchase) {
  83. // 购买凭证验证成功,进行购买成功的逻辑
  84. SKPaymentQueue.default().finishTransaction(transaction)
  85. // ...
  86. } else {
  87. // 购买凭证验证失败,进行购买失败的逻辑
  88. SKPaymentQueue.default().finishTransaction(transaction)
  89. // ...
  90. }
  91. }
  92. }
  93. }
  94. func handleError(transaction: SKPaymentTransaction) {
  95. // 处理购买失败的逻辑
  96. SKPaymentQueue.default().finishTransaction(transaction)
  97. // ...
  98. }
  99. func verifyPurchase(purchase: [String: Any]) -> Bool {
  100. // 执行购买凭证验证的逻辑,例如验证产品标识符、购买日期等
  101. // 如果验证成功返回 true,否则返回 false
  102. // ...
  103. return true
  104. }
  105. func parseReceipt(receiptData: Data) -> [String: Any]? {
  106. guard let receipt = try? JSONSerialization.jsonObject(with: receiptData, options: []) as? [String: Any] else {
  107. return nil
  108. }
  109. return receipt
  110. }
  111. }