123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- //
- // KMInAppPurchaseManager.swift
- // PDF Master
- //
- // Created by lizhe on 2023/6/1.
- //
- import Cocoa
- import StoreKit
- import AuthenticationServices
- class KMInAppPurchaseManager: NSObject {
- public static let manager = KMInAppPurchaseManager()
-
- var availableProducts: [SKProduct] = []
-
- override init() {
- super.init()
- // 注册购买交易观察者
- SKPaymentQueue.default().add(self)
- }
-
- func fetchProducts() {
- let productIdentifiers: Set<String> = ["com.yourapp.product1", "com.yourapp.product2"]
- let request = SKProductsRequest(productIdentifiers: productIdentifiers)
- request.delegate = self
- request.start()
- }
-
- func purchaseProduct(productIdentifier: String) {
- guard let product = availableProducts.first(where: { $0.productIdentifier == productIdentifier }) else {
- // 未找到匹配的产品
- return
- }
-
- let payment = SKPayment(product: product)
- SKPaymentQueue.default().add(payment)
- }
-
- func startInAppPurchase() {
- let productIdentifier = "your_product_identifier"
- if SKPaymentQueue.canMakePayments() {
- if let product = availableProducts.first(where: { $0.productIdentifier == productIdentifier }) {
- let payment = SKPayment(product: product)
- SKPaymentQueue.default().add(payment)
- } else {
- // 未找到匹配的产品
- }
- } else {
- // 用户无法进行内购
- }
- }
-
- }
- extension KMInAppPurchaseManager: SKProductsRequestDelegate {
- func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
- availableProducts = response.products
- // 处理产品信息
- }
-
- func request(_ request: SKRequest, didFailWithError error: Error) {
- // 处理请求错误
- }
- }
- extension KMInAppPurchaseManager: SKPaymentTransactionObserver {
- func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
- for transaction in transactions {
- switch transaction.transactionState {
- case .purchased:
- // 购买成功,进行本地二次验证
- validatePurchase(transaction: transaction)
- case .failed:
- // 购买失败,处理错误
- handleError(transaction: transaction)
- case .restored:
- // 恢复购买,进行本地二次验证
- validatePurchase(transaction: transaction)
- default:
- break
- }
- }
- }
-
- func validatePurchase(transaction: SKPaymentTransaction) {
- // 获取购买凭证
- if let receiptURL = Bundle.main.appStoreReceiptURL,
- let receiptData = try? Data(contentsOf: receiptURL) {
- // 进行本地二次验证
- if let parsedReceipt = parseReceipt(receiptData: receiptData),
- let purchase = parsedReceipt["in_app"] as? [[String: Any]],
- let matchingPurchase = purchase.first(where: { $0["transaction_id"] as? String == transaction.transactionIdentifier }) {
- // 验证购买凭证,进行相应的处理
- if verifyPurchase(purchase: matchingPurchase) {
- // 购买凭证验证成功,进行购买成功的逻辑
- SKPaymentQueue.default().finishTransaction(transaction)
- // ...
- } else {
- // 购买凭证验证失败,进行购买失败的逻辑
- SKPaymentQueue.default().finishTransaction(transaction)
- // ...
- }
- }
- }
- }
-
- func handleError(transaction: SKPaymentTransaction) {
- // 处理购买失败的逻辑
- SKPaymentQueue.default().finishTransaction(transaction)
- // ...
- }
-
- func verifyPurchase(purchase: [String: Any]) -> Bool {
- // 执行购买凭证验证的逻辑,例如验证产品标识符、购买日期等
- // 如果验证成功返回 true,否则返回 false
- // ...
- return true
- }
-
- func parseReceipt(receiptData: Data) -> [String: Any]? {
- guard let receipt = try? JSONSerialization.jsonObject(with: receiptData, options: []) as? [String: Any] else {
- return nil
- }
-
- return receipt
- }
- }
|