|
@@ -61,6 +61,7 @@ enum KMInAppPurchaseType: Int, CaseIterable {
|
|
|
case unknow = 0
|
|
|
case restore
|
|
|
case purchase
|
|
|
+ case check
|
|
|
}
|
|
|
|
|
|
class KMInAppPurchaseManager: NSObject {
|
|
@@ -79,7 +80,9 @@ class KMInAppPurchaseManager: NSObject {
|
|
|
}
|
|
|
|
|
|
var type: KMInAppPurchaseType = .unknow
|
|
|
-
|
|
|
+ var tempTransaction: SKPaymentTransaction?
|
|
|
+ var isPurchaseed = false //是否购买过
|
|
|
+
|
|
|
var orderId: String?
|
|
|
|
|
|
deinit {
|
|
@@ -208,8 +211,10 @@ class KMInAppPurchaseManager: NSObject {
|
|
|
|
|
|
//判断是否订阅过
|
|
|
func checkSubscriptionStatus(_ completion: @escaping KMPurchaseCheckSubscriptionStatusCompletion) {
|
|
|
- self.checkSubscriptionStatusCompletion = completion
|
|
|
- SKPaymentQueue.default().restoreCompletedTransactions()
|
|
|
+ self.type = .check
|
|
|
+// self.checkSubscriptionStatusCompletion = completion
|
|
|
+ completion(isPurchaseed || self.isInAppPurchaseReceiptPurchased())
|
|
|
+// SKPaymentQueue.default().restoreCompletedTransactions()
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -251,6 +256,9 @@ extension KMInAppPurchaseManager: SKProductsRequestDelegate {
|
|
|
|
|
|
extension KMInAppPurchaseManager: SKPaymentTransactionObserver {
|
|
|
func paymentQueueDidChangeStorefront(_ queue: SKPaymentQueue) {
|
|
|
+ // 用户更改了 storefront
|
|
|
+ // 在这里可以执行相关的操作,例如更新价格信息或用户订阅选项
|
|
|
+ // 你可以根据新的 storefront 来调整你的 App 内购买或订阅流程
|
|
|
KMPrint("paymentQueueDidChangeStorefront")
|
|
|
}
|
|
|
|
|
@@ -269,52 +277,70 @@ extension KMInAppPurchaseManager: SKPaymentTransactionObserver {
|
|
|
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
|
|
|
KMPrint("苹果服务器返回数据")
|
|
|
if transactions.count > 0 {
|
|
|
- var isSuccess = false
|
|
|
- var isDefault = false
|
|
|
+ var restored = false
|
|
|
+ var purchased = false
|
|
|
+ var failed = false
|
|
|
for transaction in transactions {
|
|
|
- if kPRODUCTS.contains(transaction.original?.payment.productIdentifier ?? PRODUCT_1) {
|
|
|
+ debugPrint(transaction.original?.payment.productIdentifier)
|
|
|
+ if transaction.original?.payment.productIdentifier != nil {
|
|
|
+ SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
+ }
|
|
|
+
|
|
|
+ if kPRODUCTS.contains(transaction.payment.productIdentifier) {
|
|
|
// 检查购买的产品是否是订阅产品
|
|
|
switch transaction.transactionState {
|
|
|
case .purchased:
|
|
|
- KMPrint("purchased")
|
|
|
- isSuccess = true
|
|
|
- SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
+ debugPrint("purchased")
|
|
|
+ purchased = true
|
|
|
+ SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
+ break
|
|
|
case .failed:
|
|
|
- KMPrint("failed")
|
|
|
- break
|
|
|
+ debugPrint("failed")
|
|
|
+ failed = true
|
|
|
+ break
|
|
|
case .restored:
|
|
|
- KMPrint("restored")
|
|
|
- isSuccess = true
|
|
|
- SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
+ debugPrint("restored")
|
|
|
+ restored = true
|
|
|
+ isPurchaseed = true
|
|
|
+ SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
+ break
|
|
|
+ case .purchasing:
|
|
|
+ debugPrint("purchasing")
|
|
|
+ tempTransaction = transaction
|
|
|
+ break
|
|
|
default:
|
|
|
- KMPrint("default")
|
|
|
- isDefault = true
|
|
|
+ debugPrint("default")
|
|
|
break
|
|
|
}
|
|
|
+ } else {
|
|
|
+ debugPrint("其他状态")
|
|
|
}
|
|
|
}
|
|
|
- if isSuccess {
|
|
|
- if let purchase = purchaseProductCompletion {
|
|
|
+
|
|
|
+ if purchased {
|
|
|
+ debugPrint("恢复购买,进行本地二次验证")
|
|
|
+ validatePurchase(transaction: nil)
|
|
|
+ } else if restored {
|
|
|
+ if self.type == .restore {
|
|
|
KMPrint("购买成功,进行本地二次验证")
|
|
|
validatePurchase(transaction: nil)
|
|
|
- } else if let callBack = self.checkSubscriptionStatusCompletion {
|
|
|
+ } else if self.type == .check {
|
|
|
KMPrint("恢复购买 checkSubscriptionStatusCompletion")
|
|
|
handleAction(state: .checkSubscriptionSuccess)
|
|
|
- } else if let restore = self.restoreCompletion {
|
|
|
- KMPrint("恢复购买,进行本地二次验证")
|
|
|
+ } else if self.type == .purchase {
|
|
|
validatePurchase(transaction: nil)
|
|
|
}
|
|
|
- } else if isDefault {
|
|
|
-
|
|
|
- } else {
|
|
|
- KMPrint("购买失败,处理错误")
|
|
|
- if let callBack = self.checkSubscriptionStatusCompletion {
|
|
|
+ } else if failed {
|
|
|
+ debugPrint("回调失败")
|
|
|
+ if self.type == .check {
|
|
|
handleAction(state: .checkSubscriptionFailed)
|
|
|
- } else if let restore = self.restoreCompletion {
|
|
|
+ } else if self.type == .restore {
|
|
|
handleAction(state: .restoreFailed)
|
|
|
- } else if let purchase = purchaseProductCompletion {
|
|
|
+ } else if self.type == .purchase {
|
|
|
handleAction(state: .productFailed)
|
|
|
}
|
|
|
+ } else {
|
|
|
+ debugPrint("其他原因")
|
|
|
}
|
|
|
} else {
|
|
|
self.handleAction(state: .productCorrespondenceFailed)
|
|
@@ -483,6 +509,11 @@ extension KMInAppPurchaseManager: SKPaymentTransactionObserver {
|
|
|
// 解析服务器返回的验证结果
|
|
|
// 如果验证成功返回 true,否则返回 false
|
|
|
// ...
|
|
|
+ if tempTransaction != nil {
|
|
|
+ SKPaymentQueue.default().finishTransaction(tempTransaction!)
|
|
|
+ tempTransaction = nil
|
|
|
+ }
|
|
|
+
|
|
|
let receipt: [String: Any] = self.parseReceipt(receiptData: data) ?? [:]
|
|
|
KMPrint(receipt)
|
|
|
let status: Int = receipt["status"] as! Int
|
|
@@ -493,16 +524,16 @@ extension KMInAppPurchaseManager: SKPaymentTransactionObserver {
|
|
|
} else if status == 21008 {
|
|
|
self.validatePurchase(false, isDebug: true, transaction: nil)
|
|
|
} else if status == 0 {
|
|
|
- //保存票据信息
|
|
|
- self.saveReceiptInfo(receipt: receipt)
|
|
|
+ if let receiptURL = Bundle.main.appStoreReceiptURL,
|
|
|
+ let receiptData = try? Data(contentsOf: receiptURL) {
|
|
|
+ //给服务器发送票据验证
|
|
|
+ appStoreEquityVerification(receiptData:receiptData, type: self.type)
|
|
|
+ }
|
|
|
|
|
|
let state = self.verifyPurchase(purchase: receipt)
|
|
|
if state == .subscription {
|
|
|
- if let receiptURL = Bundle.main.appStoreReceiptURL,
|
|
|
- let receiptData = try? Data(contentsOf: receiptURL) {
|
|
|
- //给服务器发送票据验证
|
|
|
- appStoreEquityVerification(receiptData:receiptData, type: self.type)
|
|
|
- }
|
|
|
+ //保存票据信息
|
|
|
+ self.saveReceiptInfo(receipt: receipt)
|
|
|
self.handleAction(state: .verSuccess)
|
|
|
} else {
|
|
|
self.handleAction(state: .verFailed)
|
|
@@ -620,6 +651,10 @@ extension KMInAppPurchaseManager {
|
|
|
//保存票据信息
|
|
|
UserDefaults.standard.set(receipt, forKey: "kInAppPurchaseReceipt")
|
|
|
UserDefaults.standard.synchronize()
|
|
|
+
|
|
|
+ //保存是否购买过本产品
|
|
|
+ UserDefaults.standard.set("1", forKey: "kInAppPurchaseReceiptPurchased")
|
|
|
+ UserDefaults.standard.synchronize()
|
|
|
}
|
|
|
|
|
|
func removeReceiptInfo() {
|
|
@@ -627,4 +662,13 @@ extension KMInAppPurchaseManager {
|
|
|
UserDefaults.standard.set([], forKey: "kInAppPurchaseReceipt")
|
|
|
UserDefaults.standard.synchronize()
|
|
|
}
|
|
|
+
|
|
|
+ func isInAppPurchaseReceiptPurchased() -> Bool{
|
|
|
+ let temp: String = UserDefaults.standard.object(forKey: "kInAppPurchaseReceiptPurchased") as? String ?? ""
|
|
|
+ if temp == "1" {
|
|
|
+ return true
|
|
|
+ } else {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|