开发者

finishTransaction: doesn't work

I build a project for in-app purchase. There are parts of source code bellow:

    - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    NSLog(@"paymentQueue update");
    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];
            default:
                break;
  开发者_StackOverflow中文版      }
    }
}

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@"completeTransaction");
    // Your application should implement these two methods.
    [self recordTransaction: transaction];
    [self provideContent: transaction.payment.productIdentifier];
    // Remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@"restoreTransaction");
    [self recordTransaction: transaction];
    [self provideContent: transaction.originalTransaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@"failedTransaction");
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        // Optionally, display an error here.
    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

//Optional, record the transaction infomation
- (void) recordTransaction: (SKPaymentTransaction *)transaction{

    receipt=transaction.transactionReceipt;

    [[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:@"proUpgradeTransactionReceipt" ];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self sendReceipt:receipt];

    expiredTime=transaction.transactionDate;
}

I can add payment and make purchase in sandbox successfully,

but the finished transactions doesn't removed from the defaultQueue after finishTransaction: executed.

The defaultQueue accumulates when I add payment.

Does I make some mistake?

Thanks for any helps.


It's really my fault...
I duplicate StoreObserver object everytime I do purchase
After I add a flag to prevent the duplication(as follows), the bug is fixed.

static bool hasAddObserver=NO;
    -(void) recvPurchase:(NSString *)purchaseString{
        //check user's in-app purchase status
        if ([self CanPurchase]) {
                /*=====================================*/
            if (!hasAddObserver) {//flag to fix this bug
                /*=====================================*/
                observer=[[StoreObserver alloc]init];
                [[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
                hasAddObserver=YES;
            }
            SKPayment *payment = [SKPayment paymentWithProductIdentifier:purchaseString];
            [[SKPaymentQueue defaultQueue] addPayment:payment];
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜