Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SKPaymentQueue updatedTransactions not being called

I am getting "this in app purchase has already been bought it will be restored for free" but the delegate updatedTransactions is not being called and nothing happens, the IAP doesn't execute.

I have implemented Restore Purchase which is working fine but how do i prevent a user from making purchase for a non-consumable item again?And why is the delegate updatedTransactions(SKPaymentTransactionState.Restored) not being called?

Even making purchase after deleting and reinstalling the app shows this pop up.

Here is my code.Please let me know if i am doing anything wrong.Thanks

func makePaymentForProduct(product:SKProduct){
    let payment = SKPayment.init(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment)
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}

func restorePurchases(){
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}


//MARK: SKProductsRequestDelegate

func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
    self.delegate?.didGetProducts(response.products)
}

func request(request: SKRequest, didFailWithError error: NSError) {
    self.delegate?.purchaseFailed(error.localizedDescription)
}

//MARK: SKPaymentTransactionObserver

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

    for (_, transaction) in transactions.enumerate() {
        switch (transaction.transactionState) {
        case SKPaymentTransactionState.Purchased:
            self.completeTransaction(transaction)
            break
        case SKPaymentTransactionState.Restored:
            self.restoreTransaction(transaction)
            break
        case SKPaymentTransactionState.Failed:
            self.failedTransaction(transaction)
            break
        default:
            break
        }
    }
}

func completeTransaction(transaction:SKPaymentTransaction){
    self.delegate?.purchaseSuccessful()
    SKPaymentQueue.defaultQueue().finishTransaction(transaction)
}

func restoreTransaction(transaction:SKPaymentTransaction){
    self.delegate?.purchaseSuccessful()
    SKPaymentQueue.defaultQueue().finishTransaction(transaction)
}

func failedTransaction(transaction:SKPaymentTransaction){
    self.delegate?.purchaseFailed("")
    SKPaymentQueue.defaultQueue().finishTransaction(transaction)
}


//Restore Purchase

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
    self.delegate?.purchaseRestored()
}

func paymentQueue(queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: NSError) {
    self.delegate?.purchaseFailed(error.localizedDescription)
}
like image 746
iYousafzai Avatar asked May 21 '16 11:05

iYousafzai


1 Answers

You're setting the transaction observer after calling .restoreCompletedTransactions() which could be the issue - I'd recommend trying

func restorePurchases(){
    SKPaymentQueue.default().add(self)
    SKPaymentQueue.default().restoreCompletedTransactions()
}

instead of the implementation you have above.

As a side-note, I see that your original code is adding the transaction observer in multiple places - you might consider trying to reduce this to just one location (if possible!).

like image 121
gemmakbarlow Avatar answered Oct 19 '22 19:10

gemmakbarlow