Explorar el Código

Merge pull request #145 from bizz84/feature/improve-debug-logging

Improved debug logging for SKPaymentTransaction
Andrea Bizzotto hace 8 años
padre
commit
6395a8ff6b

+ 1 - 14
SwiftyStoreKit/CompleteTransactionsController.swift

@@ -35,19 +35,6 @@ struct CompleteTransactions {
     }
     }
 }
 }
 
 
-extension SKPaymentTransactionState {
-
-    var stringValue: String {
-        switch self {
-        case .purchasing: return "purchasing"
-        case .purchased: return "purchased"
-        case .failed: return "failed"
-        case .restored: return "restored"
-        case .deferred: return "deferred"
-        }
-    }
-}
-
 class CompleteTransactionsController: TransactionController {
 class CompleteTransactionsController: TransactionController {
 
 
     var completeTransactions: CompleteTransactions?
     var completeTransactions: CompleteTransactions?
@@ -71,7 +58,7 @@ class CompleteTransactionsController: TransactionController {
 
 
                 products.append(product)
                 products.append(product)
 
 
-                print("Finishing transaction for payment \"\(transaction.payment.productIdentifier)\" with state: \(transactionState.stringValue)")
+                print("Finishing transaction for payment \"\(transaction.payment.productIdentifier)\" with state: \(transactionState.debugDescription)")
 
 
                 if completeTransactions.atomically {
                 if completeTransactions.atomically {
                     paymentQueue.finishTransaction(transaction)
                     paymentQueue.finishTransaction(transaction)

+ 24 - 2
SwiftyStoreKit/PaymentQueueController.swift

@@ -55,6 +55,28 @@ public protocol PaymentQueue: class {
 
 
 extension SKPaymentQueue: PaymentQueue { }
 extension SKPaymentQueue: PaymentQueue { }
 
 
+extension SKPaymentTransaction {
+
+    open override var debugDescription: String {
+        let transactionId = transactionIdentifier ?? "null"
+        return "productId: \(payment.productIdentifier), transactionId: \(transactionId), state: \(transactionState), date: \(transactionDate)"
+    }
+}
+
+extension SKPaymentTransactionState: CustomDebugStringConvertible {
+
+    public var debugDescription: String {
+
+        switch self {
+        case .purchasing: return "purchasing"
+        case .purchased: return "purchased"
+        case .failed: return "failed"
+        case .restored: return "restored"
+        case .deferred: return "deferred"
+        }
+    }
+}
+
 class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
 class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
 
 
     private let paymentsController: PaymentsController
     private let paymentsController: PaymentsController
@@ -94,7 +116,6 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
     func restorePurchases(_ restorePurchases: RestorePurchases) {
     func restorePurchases(_ restorePurchases: RestorePurchases) {
 
 
         if restorePurchasesController.restorePurchases != nil {
         if restorePurchasesController.restorePurchases != nil {
-            // return .inProgress
             return
             return
         }
         }
 
 
@@ -147,7 +168,8 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
         unhandledTransactions = completeTransactionsController.processTransactions(unhandledTransactions, on: paymentQueue)
         unhandledTransactions = completeTransactionsController.processTransactions(unhandledTransactions, on: paymentQueue)
 
 
         if unhandledTransactions.count > 0 {
         if unhandledTransactions.count > 0 {
-            print("unhandledTransactions: \(unhandledTransactions)")
+            let strings = unhandledTransactions.map { $0.debugDescription }.joined(separator: "\n")
+            print("unhandledTransactions:\n\(strings)")
         }
         }
     }
     }
 
 

+ 2 - 0
SwiftyStoreKit/RestorePurchasesController.swift

@@ -81,6 +81,7 @@ class RestorePurchasesController: TransactionController {
     func restoreCompletedTransactionsFailed(withError error: Error) {
     func restoreCompletedTransactionsFailed(withError error: Error) {
 
 
         guard let restorePurchases = restorePurchases else {
         guard let restorePurchases = restorePurchases else {
+            print("Callback already called. Returning")
             return
             return
         }
         }
         restoredProducts.append(.failed(error: SKError(_nsError: error as NSError)))
         restoredProducts.append(.failed(error: SKError(_nsError: error as NSError)))
@@ -95,6 +96,7 @@ class RestorePurchasesController: TransactionController {
     func restoreCompletedTransactionsFinished() {
     func restoreCompletedTransactionsFinished() {
 
 
         guard let restorePurchases = restorePurchases else {
         guard let restorePurchases = restorePurchases else {
+            print("Callback already called. Returning")
             return
             return
         }
         }
         restorePurchases.callback(restoredProducts)
         restorePurchases.callback(restoredProducts)