'Restoring in-app purchases needs payment confirmation before it goes through

This could be hard to explain but il give it a try. I am trying to put in app purchases in an app. I am having trouble with the restore purchases element of it.

I have a uialert that shows 3 options:

  1. Continue to purchase
  2. Restore purchase
  3. Cancel

The purchase one works perfectly, the payment window pops up and the user can buy. However, when the user clicks on the restore button (option 2.) the following is called SKPaymentQueue.default().restoreCompletedTransactions(). but in order for it to go through the user has to press option 1 after pressing option 2 (for some reason). if user does this it seems like the restore goes through. I can't figure out why if the user only presses option 2 then nothing happens.

//in-app purchases-----------------------
enum Product: String, CaseIterable {
    case noadspremium = "testapp"
}

func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
    if let oProduct = response.products.first {
        print("product is available")
        self.purchase(aproduct: oProduct)
    }
    else {
        print("product not available")
    }
}

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction in transactions {
        
        switch transaction.transactionState {
        case .purchasing:
            print("customer is in the process of purchase")
        case .purchased:
            SKPaymentQueue.default().finishTransaction(transaction)
            UserDefaults.standard.set(true, forKey: "unlocked")
            print("purchased")
            collectionView.reloadData()
            print("collection view now updated")
        case .failed:
            SKPaymentQueue.default().finishTransaction(transaction)
            print("failed")
        case .restored:
            print("Restored")
            UserDefaults.standard.set(true, forKey: "unlocked")
        case .deferred:
            print("deferred")
        default:
            break
        }
        
    }
}

func purchase(aproduct: SKProduct) {
    let payment = SKPayment(product: aproduct)
    SKPaymentQueue.default().add(self)
    SKPaymentQueue.default().add(payment)
}



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    print(indexPath.item)
    
    //checking if character is unlocked
    if !UserDefaults.standard.bool(forKey: "unlocked") && indexPath.item > 0 {
        
        print("this item is locked")
        let lockedItemAlert = UIAlertController(title: "Locked", message: "unlock for $0.99", preferredStyle: .alert)
        lockedItemAlert.addAction(UIAlertAction(title: "Continue", style: UIAlertAction.Style.default, handler: { _ in self.paymentFunction() }))
        lockedItemAlert.addAction(UIAlertAction(title: "Restore Old Purchases", style: UIAlertAction.Style.default, handler: { _ in self.restorePressed() }))
        lockedItemAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: nil))
        self.present(lockedItemAlert, animated: true, completion: nil)
        
    } else {
        print("item is unlocked")
    }
}

 func paymentFunction(){
    print("payment function called")
    if SKPaymentQueue.canMakePayments(){
        let set: Set<String> = [Product.noadspremium.rawValue]
        let productRequest = SKProductsRequest(productIdentifiers: set)
        productRequest.delegate = self
        productRequest.start()
    }
    
}


func restorePressed(){
    print("restore function called")
    SKPaymentQueue.default().restoreCompletedTransactions()
}

I have tried to call they payment function at the end of option 2 function to try and push it through. but the payment window opens up again. i dont understand why the payment window doesn't open up when option 2 is pressed and then option 1.

func restorePressed(){
    print("restore function called")
    SKPaymentQueue.default().restoreCompletedTransactions()
    paymentFunction()
 }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source