0

I got an app where I used the SKPayment, SKPaymentTransactionObserver and so on. However, everything is going fine with the first payment, but whenever I select any other thing I want to purchase, it starts purchasing again all the things I already purchased with the new thing at the end. Tried looking it up on internet but no luck.

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    
    
    transactions.forEach( {
        switch $0.transactionState {
        
        case .purchasing:
            print("purchasing item 1")
        case .purchased:
            print("purchased item 1")
            SKPaymentQueue.default().finishTransaction($0)
            
            downloadFileButton.setTitle("Download", for: .normal)
            
            let userId = Auth.auth().currentUser?.uid
            
            let childUpdate = ["item1": true]
            
            Database.database().reference().child("users").child(userId!).child("purchase_status").updateChildValues(childUpdate)
            
            
        case .failed:
            print("user cancelled the sheet or an error occured")
            SKPaymentQueue.default().finishTransaction($0)
        case .restored:
            break
        case .deferred:
            break
        @unknown default:
            break
        }
    })
    
    
}

I got this same code everywhere I can purchase something, but except for little changes like "item 2". When I purchase lets say 6 items in order, with the sixth purchase the console looks like this:

purchasing item 1

purchasing item 2

purchasing item 3

purchasing item 4

purchasing item 5

purchasing item 6

// After purchase:

purchased item 1

purchased item 2

purchased item 3

purchased item 4

purchased item 5

purchased item 6

EDIT: I am a beginner so any explanation, suggestions and corrections will be awesome :)

markm
  • 3
  • 2
  • Where do the strings "purchasing item 2" and "purchased item 2" etc come from - they aren't shown in this code. The item being purchased should be obtained from the transaction – Paulw11 Mar 03 '21 at 22:20
  • As I mentioned, I have this exact same code basically copied in every viewcontroller where I can purchase something. So in ViewController2 I have in case .purchasing: print("purchasing item 2") and so on – markm Mar 03 '21 at 22:27
  • You should not do that. You have a single object that implements you transaction observer. This object should be instantiated as soon as your app launches ( in `didFinishLaunching` for example). Every purchase is being presented to every observer. – Paulw11 Mar 04 '21 at 02:28

0 Answers0