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

Wiring up CompleteTransactionsController

Andrea Bizzotto 8 жил өмнө
parent
commit
4eca383348

+ 38 - 19
SwiftyStoreKit/CompleteTransactionsController.swift

@@ -25,32 +25,51 @@
 import Foundation
 import StoreKit
 
+public struct CompleteTransactions {
+    public let atomically: Bool
+    public let callback: ([Product]) -> ()
+    
+    public init(atomically: Bool, callback: @escaping ([Product]) -> ()) {
+        self.atomically = atomically
+        self.callback = callback
+    }
+}
+
 public class CompleteTransactionsController: TransactionController {
 
+    public var completeTransactions: CompleteTransactions?
+
     public func processTransactions(_ transactions: [SKPaymentTransaction], on paymentQueue: PaymentQueue) -> [SKPaymentTransaction] {
-     
-        for transaction in transactions {
+        
+        guard let completeTransactions = completeTransactions else {
+            return transactions
+        }
 
+        var unhandledTransactions: [SKPaymentTransaction] = []
+        var products: [Product] = []
+        
+        for transaction in transactions {
+            
             let transactionState = transaction.transactionState
-
-            switch transactionState {
-            case .purchased:
-
-                break
-            case .failed:
-                break
-
-            case .restored:
-                break
-
-            case .purchasing:
-                // In progress: do nothing
-                break
-            case .deferred:
-                break
+            
+            if transactionState != .purchasing {
+                
+                let product = Product(productId: transaction.payment.productIdentifier, transaction: transaction, needsFinishTransaction: !completeTransactions.atomically)
+                
+                products.append(product)
+                
+                print("Finishing transaction for payment \"\(transaction.payment.productIdentifier)\" with state: \(transactionState.stringValue)")
+                
+                if completeTransactions.atomically {
+                    paymentQueue.finishTransaction(transaction)
+                }
+            }
+            else {
+                unhandledTransactions.append(transaction)
             }
         }
+        completeTransactions.callback(products)
 
-        return transactions
+        return unhandledTransactions
     }
 }

+ 5 - 0
SwiftyStoreKit/PaymentQueueController.swift

@@ -109,6 +109,11 @@ public class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
         restorePurchasesController.restorePurchases = restorePurchases
     }
     
+    public func completeTransactions(_ completeTransactions: CompleteTransactions) {
+        
+        completeTransactionsController.completeTransactions = completeTransactions
+    }
+    
     
     // MARK: SKPaymentTransactionObserver
     public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {