lobianco
lobianco

Reputation: 6276

SKPaymentQueue finishTransaction: within network completion block?

This problem has been giving me headaches for a while now.

My app operates on an auto-renewable subscription model. I have an SKPaymentQueue transactionObserver that is notified when a purchase is successfully completed through Apple, and at this point I send the Apple receipt to my server to verify on my end. Once I verify it, I send a success call back to my app which provides the content to the user and subsequently calls finishTransaction: on the transaction (which according to the docs is the proper course of actions).

The problem is, I'm afraid waiting this long to call finishTransaction: after Apple expects it to be called (3-4 seconds) is prohibiting the transaction from actually "finishing". If I successfully make a purchase within my app (and finishTransaction: has been called), and then terminate my app ("stop" in Xcode) and run it again, immediately on startup there will be one, two, three, and sometimes four or five transactions in the SKPaymentQueue before the UI has even loaded.

Here is the call to my server which takes place after the transaction is completed with Apple :

-(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]; gets called within the success or failure blocks of the request to my server. Could this be problematic somehow?

*EDIT* It seems this issue does not occur if I call SKPaymentQueue:finishTransaction: BEFORE sending the receipt to my server for verification, which leads me to believe that calling it within the network request blocks is indeed causing the problem. But this is not the correct way to handle things, so I'm still stuck.

*EDIT 2* Turns out the transactions sometimes still get "revived" when starting up the app, even if I make the finishTransaction: call before sending the receipt to my server. What's going on?

Upvotes: 4

Views: 2379

Answers (2)

lobianco
lobianco

Reputation: 6276

After weeks of "phantom" purchases, I've finally realized what is going on. It wasn't the fault of finishTransaction: or AFNetworking.

The transactions that were occurring at app launch were actually part of the auto-renewal process that Apple was performing. The life of a 1 month subscription in the Apple Sandbox is 5 minutes, so I would have 1, 2, or 3 transactions in the queue when launching my app depending upon whether it has been 5, 10, or 15 minutes since I launched it last.

I had to tweak some server-side settings to make sure the renewals weren't being treated the same as fresh subscriptions.

Upvotes: 3

Malek_Jundi
Malek_Jundi

Reputation: 6160

It doesn't matter how it takes long to finish the transaction .. you need to call [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; just in case you delivered the content to the user (in your case when the subscription is completed successfully).

Upvotes: 0

Related Questions