Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StoreKit: Catch failed restore?

I'm implementing In-App purchase feature with Restore button.

I have a brand new test user set up, without any payments made.

When I hit the restore button, and log in with the new test user, I cannot catch any delegated methods that tell me that the restoring transaction has failed (since there is nothing to restore).

The only method that get invoked is -(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue*)queue, but this method gets called in the case when restoring was successful too.

What can I do now? How can I catch such a case?


Addition: I have a progress indicator that says "Contacting App Store", and I need an invocation where I can hide it in failed cases too.

like image 777
Geri Borbás Avatar asked Oct 13 '11 13:10

Geri Borbás


2 Answers

Noah: here's a code snippet for you:

-(void)restore {

    [self.delegate showLoadingScreen];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];


}

And the following method:

-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {

    if (!queue.transactions || [queue.transactions count] == 0) {

        [self showAlertScreenWithTitle:@"Error Restoring Subscription" message:@"No subscription was found to restore."];

    } else {

        BOOL didRestore = NO;

        for (SKPaymentTransaction *t in queue.transactions) {

            if (t.transactionState == SKPaymentTransactionStateRestored || t.transactionState == SKPaymentTransactionStatePurchased) {

                NSTimeInterval now = [[NSDate date] timeIntervalSince1970] - _timeOffset;
                NSTimeInterval expire = [t.transactionDate timeIntervalSince1970] + kExplorerSubscriptionSecondLength;
                NSTimeInterval purchase = [t.transactionDate timeIntervalSince1970];

                if (purchase <= now && now <= expire) {

                    didRestore = YES;
                }

            }


        }

        if (!didRestore)
            [self showAlertScreenWithTitle:@"Error Restoring Subscription" message:@"No subscription was found to restore."];

    }    

    [self.delegate dismissLoadingScreen];

}

Let me know if that helps you....

like image 56
Joris Weimar Avatar answered Sep 29 '22 16:09

Joris Weimar


When you restore a transactions, there're two delegated methods:

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error

The first one (paymentQueueRestoreCompletedTransactionsFinished) is called when all the transactions are restored. If you don't have any previous purchase it also calls this method because the restored worked fine but there's nothing to restore.

The other method (restoreCompletedTransactionsFailedWithError) is called when there's an error restoring the transaction.

If you need to show a message to the user telling him he doesn't have any transaction to restore you can use:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

Here you have a small snippet for this delegate:

//
// called when the transaction status is updated
//
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
                restoredTransaction++;
                break;
            default:
                break;
        }
    }
}

Then you can use the restoredTransaction variable to know if any transaction has been restored on paymentQueueRestoreCompletedTransactionsFinished

like image 43
ventayol Avatar answered Sep 29 '22 15:09

ventayol