Consumable In App Purchase Already Purchased

2.1k views Asked by At

I have added a consumable In App Purchase to my app. I am able to purchase it but then when I try to purchase it again it says that I have already purchased it so it will download again. The type is definitely consumable. Where am I going wrong? Thanks

func purchasePoints(sender:AnyObject) {
    //Purchase
    buyConsumable()


}

func buyConsumable(){
    print("About to fetch the products");
    // We check that we are allow to make the purchase.
    if (SKPaymentQueue.canMakePayments())
    {
        var productID:NSSet = NSSet(object: "com.companyname.appname.coins");
        var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
        productsRequest.delegate = self;
        productsRequest.start();
        print("Fething Products");
    }else{
        print("can not make purchases");
    }
}


func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
    print("got the request from Apple")
    var count : Int = response.products.count
    if (count>0) {
        var validProducts = response.products
        var validProduct: SKProduct = response.products[0] as SKProduct
        if (validProduct.productIdentifier == "com.companyname.appname.coins") {
            print(validProduct.localizedTitle)
            print(validProduct.localizedDescription)
            print(validProduct.price)
            buyProduct(validProduct);
        } else {
            print(validProduct.productIdentifier)
        }
    } else {
        print("nothing")
    }
}

func buyProduct(product: SKProduct){
    print("Sending the Payment Request to Apple");
    var payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment);
}


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");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                break;
            case .Failed:
                print("Purchased Failed");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                break;
                // case .Restored:
                //[self restoreTransaction:transaction];
            default:
                break;
            }
        }
    }
}
1

There are 1 answers

0
Maks On BEST ANSWER

Did you see this messages in console log:

print("Received Payment Transaction Response from Apple");

and

print("Product Purchased");

or

print("Purchased Failed");

?

If not, it seems like you did not add

SKPaymentQueue.defaultQueue().addTransactionObserver(self)

somewhere before adding payment to queue. Try to add this in your buyConsumable method.

In according to https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Chapters/DeliverProduct.html

Try this with existing transaction, but be sure that it is consumable.