2
0
Эх сурвалжийг харах

Update RestorePurchasesController to accumulate all relevant updated transaction in an array, and call the callback in restoreCompletedTransactionsFailed or restoreCompletedTransactionsFinished

Andrea Bizzotto 8 жил өмнө
parent
commit
1170440c36

+ 9 - 10
SwiftyStoreKit/RestorePurchasesController.swift

@@ -42,6 +42,8 @@ public class RestorePurchasesController: TransactionController {
     
     public var restorePurchases: RestorePurchases?
     
+    private var restoredProducts: [TransactionResult] = []
+    
     public init() { }
     
     public func processTransaction(_ transaction: SKPaymentTransaction, atomically: Bool, on paymentQueue: PaymentQueue) -> Product? {
@@ -68,7 +70,6 @@ public class RestorePurchasesController: TransactionController {
         }
         
         var unhandledTransactions: [SKPaymentTransaction] = []
-        var restoredProducts: [TransactionResult] = []
         for transaction in transactions {
             if let restoredProduct = processTransaction(transaction, atomically: restorePurchases.atomically, on: paymentQueue) {
                 restoredProducts.append(.restored(product: restoredProduct))
@@ -77,11 +78,6 @@ public class RestorePurchasesController: TransactionController {
                 unhandledTransactions.append(transaction)
             }
         }
-        if restoredProducts.count > 0 {
-            restorePurchases.callback(restoredProducts)
-        }
-        // Reset to nil after purchases complete
-        self.restorePurchases = nil
 
         return unhandledTransactions
     }
@@ -91,9 +87,11 @@ public class RestorePurchasesController: TransactionController {
         guard let restorePurchases = restorePurchases else {
             return
         }
-        restorePurchases.callback([.failed(error: error)])
+        restoredProducts.append(.failed(error: error))
+        restorePurchases.callback(restoredProducts)
         
-        // Reset to nil after error received
+        // Reset state after error received
+        restoredProducts = []
         self.restorePurchases = nil
 
     }
@@ -103,9 +101,10 @@ public class RestorePurchasesController: TransactionController {
         guard let restorePurchases = restorePurchases else {
             return
         }
-        restorePurchases.callback([])
+        restorePurchases.callback(restoredProducts)
         
-        // Reset to nil after error transactions finished
+        // Reset state after error transactions finished
+        restoredProducts = []
         self.restorePurchases = nil
     }
 }

+ 2 - 1
SwiftyStoreKitTests/PaymentQueueControllerTests.swift

@@ -130,10 +130,11 @@ class PaymentQueueControllerTests: XCTestCase {
         paymentQueueController.startPayment(testPayment)
         
         paymentQueueController.restorePurchases(restorePurchases)
-        
+
         paymentQueueController.completeTransactions(completeTransactions)
         
         paymentQueueController.paymentQueue(SKPaymentQueue(), updatedTransactions: transactions)
+        paymentQueueController.paymentQueueRestoreCompletedTransactionsFinished(SKPaymentQueue())
         
         // verify
         XCTAssertTrue(paymentCallbackCalled)

+ 3 - 1
SwiftyStoreKitTests/RestorePurchasesControllerTests.swift

@@ -54,6 +54,7 @@ class RestorePurchasesControllerTests: XCTestCase {
         let spy = PaymentQueueSpy()
 
         let remainingTransactions = restorePurchasesController.processTransactions([transaction], on: spy)
+        restorePurchasesController.restoreCompletedTransactionsFinished()
         
         XCTAssertEqual(remainingTransactions.count, 0)
 
@@ -107,7 +108,8 @@ class RestorePurchasesControllerTests: XCTestCase {
         let spy = PaymentQueueSpy()
         
         let remainingTransactions = restorePurchasesController.processTransactions(transactions, on: spy)
-        
+        restorePurchasesController.restoreCompletedTransactionsFinished()
+
         XCTAssertEqual(remainingTransactions.count, 2)
         
         XCTAssertTrue(callbackCalled)