SKPaymentQueue finishTransaction: in a network termination block?

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?

+4
source share
2 answers

After weeks of buying phantom, I finally realized what was going on. This was not a finishTransaction: or AFNetworking error.

The transactions that occurred at application startup were actually part of the automatic update process that Apple performed. Apple Sandbox has a 1-month subscription for 5 minutes , so I had to have 1, 2, or 3 transactions in the queue when I started my application, depending on whether it was 5, 10, or 15 minutes since I launched it for the last time.

I had to tweak some server options to make sure that updates are not being processed in the same way as fresh subscriptions.

+3
source

It doesn't matter how long the transaction ends. You need to call [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; just in case, if you deliver content to the user (in the case when the subscription is completed successfully).

0
source

Source: https://habr.com/ru/post/1413965/


All Articles