浏览代码

Merge pull request #248 from bizz84/feature/pre-filter-transaction-state-purchasing

Filter out transactions in purchasing state
Andrea Bizzotto 8 年之前
父节点
当前提交
838681df5d

+ 4 - 0
CHANGELOG.md

@@ -2,6 +2,10 @@
 
 All notable changes to this project will be documented in this file.
 
+## 0.10.5 Filter out transactions in purchasing state
+* Filter out all transactions with state == .purchasing early in purchase flows (related to [#169](https://github.com/bizz84/SwiftyStoreKit/issues/169), [#188](https://github.com/bizz84/SwiftyStoreKit/pull/188), [#179](https://github.com/bizz84/SwiftyStoreKit/issues/179))
+* Sample app: print localized description when a purchase fails with `.unknown` error
+
 ## 0.10.4 Documentation and updates for Xcode 9
 
 * Update to Xcode 9 recommended project settings ([#247](https://github.com/bizz84/SwiftyStoreKit/pull/247))

+ 1 - 1
SwiftyStoreKit-iOS-Demo/ViewController.swift

@@ -209,7 +209,7 @@ extension ViewController {
         case .error(let error):
             print("Purchase Failed: \(error)")
             switch error.code {
-            case .unknown: return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
+            case .unknown: return alertWithTitle("Purchase failed", message: error.localizedDescription)
             case .clientInvalid: // client is not allowed to issue the request, etc.
                 return alertWithTitle("Purchase failed", message: "Not allowed to make the payment")
             case .paymentCancelled: // user cancelled the request, etc.

+ 1 - 1
SwiftyStoreKit-macOS-Demo/ViewController.swift

@@ -200,7 +200,7 @@ extension ViewController {
         case .error(let error):
             print("Purchase Failed: \(error)")
             switch error.code {
-            case .unknown: return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
+            case .unknown: return alertWithTitle("Purchase failed", message: error.localizedDescription)
             case .clientInvalid: // client is not allowed to issue the request, etc.
                 return alertWithTitle("Purchase failed", message: "Not allowed to make the payment")
             case .paymentCancelled: // user cancelled the request, etc.

+ 11 - 7
SwiftyStoreKit/PaymentQueueController.swift

@@ -167,16 +167,20 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
          * 3. complete transactions (transactionState: .purchased, .failed, .restored, .deferred)
          * Any transactions where state == .purchasing are ignored.
          */
-        var unhandledTransactions = paymentsController.processTransactions(transactions, on: paymentQueue)
+        var unhandledTransactions = transactions.filter { $0.transactionState != .purchasing }
+        
+        if unhandledTransactions.count > 0 {
+        
+            unhandledTransactions = paymentsController.processTransactions(transactions, on: paymentQueue)
 
-        unhandledTransactions = restorePurchasesController.processTransactions(unhandledTransactions, on: paymentQueue)
+            unhandledTransactions = restorePurchasesController.processTransactions(unhandledTransactions, on: paymentQueue)
 
-        unhandledTransactions = completeTransactionsController.processTransactions(unhandledTransactions, on: paymentQueue)
+            unhandledTransactions = completeTransactionsController.processTransactions(unhandledTransactions, on: paymentQueue)
 
-        unhandledTransactions = unhandledTransactions.filter { $0.transactionState != .purchasing }
-        if unhandledTransactions.count > 0 {
-            let strings = unhandledTransactions.map { $0.debugDescription }.joined(separator: "\n")
-            print("unhandledTransactions:\n\(strings)")
+            if unhandledTransactions.count > 0 {
+                let strings = unhandledTransactions.map { $0.debugDescription }.joined(separator: "\n")
+                print("unhandledTransactions:\n\(strings)")
+            }
         }
     }