This problem gave me headaches for a while.
My application works with an automatic renewable subscription model. I have a SKPaymentQueue transactional transaction that notifies me that the purchase has been successfully completed through Apple, and at this point I send the Apple receipt to my server to check at my end. As soon as I confirm this, I will send a message about a successful call to my application, which provides content to the user, and then calls finishTransaction: by transaction (which , according to the documents, is the correct course of action).
The problem is that I'm afraid to wait for this for a long time to call finishTransaction: after Apple expects to be called (3-4 seconds), the transaction ban actually "ends". If I successfully make a purchase in my application (and subtracted Transaction: was called), then stop my application ("stop" in Xcode) and start it again, immediately at startup there will be one, two, three, and sometimes four or five transactions in SKPaymentQueue before the user interface even loads.
Here is my server call that occurs after transaction with Apple is completed
-(void)completeTransaction:(SKPaymentTransaction *)transaction { NSLog(@"complete transaction"); [self sendReceiptToServer:transaction]; } -(void)sendReceiptToServer:(SKPaymentTransaction*)transaction { NSString *rawReceiptData = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding]; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:rawReceiptData, @"receipt", [[UserSingleton sharedInstance] memberID], @"member", nil]; NSMutableURLRequest *request = [[HTTPRequestSingleton sharedClient] requestWithMethod:@"POST" path:cFunctionAddCredits parameters:params]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"%@", JSON); NSString *creditsString = [JSON valueForKey:@"limit"]; int numberOfCredits = [creditsString intValue]; [self provideContent:transaction.payment.productIdentifier withNumberOfCredits:numberOfCredits]; [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"failed to send receipt to server"); [[NSNotificationCenter defaultCenter] postNotificationName:cFailedToSendReceiptToServer object:transaction]; [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; }]; [operation start]; }
As you can see, [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
called in success or failure request blocks on my server. Could this be problematic?
* EDIT * This problem does not seem to occur if I call SKPaymentQueue:finishTransaction:
BEFORE sending the receipt to my server for verification, which makes me think that calling it in the network request blocks really causes the problem. But this is not the right way to handle things, so I'm still stuck.
* EDIT 2 * It turns out that the transactions are still "animated" when the application starts, even if I make a call to finishTransaction:
before sending the receipt to my server. What's happening?