Deleting 'Old' Restore Purchase Transactions

Viewed 1489

I have been trying to test the Restore Purchases function in a project, but I'm faced with a continual feed of this error:

Ignoring restoreCompletedTransactionsWithApplicationUsername: because already restoring transactions

I deleted a sandbox tester account, I presume one that has a restore purchase still caught in the payments loop. I realise I should NEVER delete a sandbox test account, but it's a little late for that.

Is there anything I can do to flush out the Restore Transactions caught in this never ending loop?

EDIT:

My amended code:

Pre-amble:

class InAppPurchaseViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {

let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;

@IBOutlet weak var unlockAction: UIButton!

override func viewDidLoad() {   
    super.viewDidLoad()

    SKPaymentQueue.defaultQueue().addTransactionObserver(self)   
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
... }
  1. The Purchase:

    @IBAction func unlockAction(sender: AnyObject) {

    product_id = "xxxx.iap.removeads";
    
    
    //Check if product is purchased
    if (defaults.boolForKey("purchased")){
        print("User already purchased this")
        // Hide a view or show content depends on your requirement
    }
    
    else if (!defaults.boolForKey("Purchased")){
        print("User has not yet purchased this")
    }
    
    
    print("About to fetch the products");
    
    // We check that user can make payments.
    if (SKPaymentQueue.canMakePayments())
    {
        let productID:NSSet = NSSet(object: self.product_id!);
        let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
        productsRequest.delegate = self;
        productsRequest.start();
        print("User can make purchases and will fetch products from Apple Store now");
    }else{
        print("User can't make purchases");
    }
    

    }

    // Proceed to buy the product.
    func buyProduct(product: SKProduct){
    print("Sending the Payment Request to Apple");
    let payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment);
    
    }
    
    func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
    
    let count : Int = response.products.count
    if (count>0) {
    
        let validProduct: SKProduct = response.products[0] as SKProduct
        if (validProduct.productIdentifier == self.product_id) {
            print(validProduct.localizedTitle)
            print(validProduct.localizedDescription)
            print(validProduct.price)
            buyProduct(validProduct);
        } else {
            print(validProduct.productIdentifier)
        }
    } else {
        print("nothing")
    }
    

    }

    func request(request: SKRequest, didFailWithError error: NSError) {
    print("Error Fetching product information");
    }
    
    
    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])   {
    print("Received Payment Transaction Response from Apple");
    
    for transaction:AnyObject in transactions {
        if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction {
            switch trans.transactionState {
    
            case .Purchased:
                print("Product Purchased")
    
                let alert = UIAlertController(title: "Thank You.", message: "Thank you for your purchase!", preferredStyle: UIAlertControllerStyle.Alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
                self.presentViewController(alert, animated: true, completion: nil)
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                defaults.setBool(true , forKey: "purchased")
                break;
    
            case .Failed:
                print("Purchased Failed");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                break;
    
            case .Restored:
                print("Already Purchased");
                SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
                break;
    
            default:
                break;
            }
        }
    }
    

    }

  2. The Restore:

    @IBAction func restorePurchases(sender: AnyObject) {
    product_id = "xxxx.iap.removeads";
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
    
    func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
    
        for transaction:AnyObject in transactions {
            if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
                switch trans.transactionState {
                case .Restored:
                    SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                    let alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored.", delegate: nil, cancelButtonTitle: "OK")
                    alert.show()
                    break;
    
                case .Failed:
                    SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                    let alert = UIAlertView(title: "Sorry", message: "Your purchase(s) could not be restored.", delegate: nil, cancelButtonTitle: "OK")
                    alert.show()
                    break;
    
                default:
                    break;
                }
            }
        }
    }
    

    }

1 Answers
Related