|
@@ -88,6 +88,8 @@ public class PurchaseManager: NSObject {
|
|
|
// 免费使用会员转 livew的次数
|
|
|
var freeNum:Int = 0
|
|
|
|
|
|
+ //原始订单交易id dict
|
|
|
+ var originalTransactionIdentifierDict:[String:String] = [:]
|
|
|
|
|
|
override init() {
|
|
|
super.init()
|
|
@@ -287,28 +289,41 @@ extension PurchaseManager: SKProductsRequestDelegate {
|
|
|
|
|
|
extension PurchaseManager: SKPaymentTransactionObserver {
|
|
|
public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
|
|
|
- debugPrint("PurchaseManager paymentQueue transactions = \(transactions)")
|
|
|
+ debugPrint("PurchaseManager paymentQueue transactions.count = \(transactions.count)")
|
|
|
+// debugPrint("PurchaseManager paymentQueue transactions = \(transactions)")
|
|
|
+
|
|
|
+ originalTransactionIdentifierDict.removeAll()
|
|
|
// 因为只有订阅类的购买项
|
|
|
for transaction in transactions {
|
|
|
-// if transaction.transactionState != .purchasing {
|
|
|
-// SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
-// continue
|
|
|
-// }
|
|
|
+
|
|
|
+// debugPrint("PurchaseManager paymentQueue transactions transactionIdentifier original= \(transaction.original?.transactionIdentifier)")
|
|
|
+// debugPrint("PurchaseManager paymentQueue transactions transactionIdentifier = \(transaction.transactionIdentifier)")
|
|
|
+// debugPrint("PurchaseManager paymentQueue transactions transactionIdentifier productIdentifier = \(transaction.payment.productIdentifier)")
|
|
|
+
|
|
|
switch transaction.transactionState {
|
|
|
case .purchasing:
|
|
|
// Transaction is being added to the server queue.
|
|
|
purchase(self, didChaged: .paying, object: nil)
|
|
|
|
|
|
case .purchased:
|
|
|
+ SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
+ //同样的原始订单,只处理一次.
|
|
|
+ guard judgeWhether(transaction: transaction) else {
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
// Transaction is in queue, user has been charged. Client should complete the transaction.
|
|
|
#if DEBUG
|
|
|
verifyPayResult(transaction: transaction, useSandBox: true)
|
|
|
#else
|
|
|
verifyPayResult(transaction: transaction, useSandBox: false)
|
|
|
#endif
|
|
|
+
|
|
|
+
|
|
|
case .failed:
|
|
|
- // Transaction was cancelled or failed before being added to the server queue.
|
|
|
+
|
|
|
SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
+ // Transaction was cancelled or failed before being added to the server queue.
|
|
|
var message = "Payment Failed"
|
|
|
if let error = transaction.error as? SKError,
|
|
|
error.code == SKError.paymentCancelled {
|
|
@@ -317,6 +332,12 @@ extension PurchaseManager: SKPaymentTransactionObserver {
|
|
|
purchase(self, didChaged: .payFail, object: message)
|
|
|
|
|
|
case .restored:
|
|
|
+ SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
+ //同样的原始订单,只处理一次.
|
|
|
+ guard judgeWhether(transaction: transaction) else {
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
// Transaction was restored from user's purchase history. Client should complete the transaction.
|
|
|
if let original = transaction.original,
|
|
|
original.transactionState == .purchased {
|
|
@@ -327,9 +348,8 @@ extension PurchaseManager: SKPaymentTransactionObserver {
|
|
|
#endif
|
|
|
} else {
|
|
|
purchase(self, didChaged: .restoreFail, object: "Failed to restore subscribe, please try again")
|
|
|
- SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
case .deferred: // The transaction is in the queue, but its final status is pending external action.
|
|
|
break
|
|
|
@unknown default:
|
|
@@ -349,6 +369,17 @@ extension PurchaseManager: SKPaymentTransactionObserver {
|
|
|
purchase(self, didChaged: .restoreFail, object: "You don't have an active subscription")
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ func judgeWhether(transaction:SKPaymentTransaction) -> Bool {
|
|
|
+ let id = transaction.original?.transactionIdentifier
|
|
|
+ if let id = id {
|
|
|
+ if let value = originalTransactionIdentifierDict[id] {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ originalTransactionIdentifierDict[id] = "1"
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
extension PurchaseManager {
|
|
@@ -375,7 +406,7 @@ extension PurchaseManager {
|
|
|
guard let self = self else { return }
|
|
|
if let data = data,
|
|
|
let jsonResponse = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
|
|
|
- debugPrint("PurchaseManager verifyPayResult = \(jsonResponse)")
|
|
|
+// debugPrint("PurchaseManager verifyPayResult = \(jsonResponse)")
|
|
|
let status = jsonResponse["status"]
|
|
|
if let status = status as? String, status == "21007" {
|
|
|
self.verifyPayResult(transaction: transaction, useSandBox: true)
|
|
@@ -436,8 +467,6 @@ extension PurchaseManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
-
|
|
|
DispatchQueue.main.async {
|
|
|
if transaction.transactionState == .restored {
|
|
|
self.purchase(self, didChaged: .restoreSuccess, object: nil)
|