2012-06-13 69 views
2

我正在iOS中使用inapp購買項目sdk.i已經準備了inapp購買類從appstore購買應用程序與啓用項目的內部內容。但是,我的問題是SKPaymentQueue沒有完成我的應用程式內購類的我class.here的代碼工作:InApp購買SKPaymentQueue完成交易不起作用

- (void)purchase { 
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self]; 
    SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.test"]; 
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 
    [[SKPaymentQueue defaultQueue] addPayment:payment]; 

} 

- (void)provideContent:(NSString *)productIdentifier { 

    NSLog(@"Toggling flag for: %@", productIdentifier); 
    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:productIdentifier]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
    //[_purchasedProducts addObject:productIdentifier]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchasedNotification object:productIdentifier]; 

} 

- (void)completeTransaction:(SKPaymentTransaction *)transaction { 

    NSLog(@"completeTransaction..."); 

    //[self recordTransaction: transaction]; 
    [self provideContent: transaction.payment.productIdentifier]; 
    [[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 { 

    if (transaction.error.code != SKErrorPaymentCancelled) 
    { 
     NSLog(@"Transaction error: %@", transaction.error.localizedDescription); 
    } 


    [[NSNotificationCenter defaultCenter] removeObserver:self name:kProductPurchaseFailedNotification object:transaction]; 


    [[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchaseFailedNotification object:transaction]; 


    //[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:transaction]; 

    [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 

} 



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

    for (SKPaymentTransaction *transaction in transactions) { 

     switch (transaction.transactionState) { 

      case SKPaymentTransactionStatePurchasing: 

       break; 

      case SKPaymentTransactionStatePurchased: 
       [self completeTransaction:transaction]; 
       //[[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 

       break; 

      case SKPaymentTransactionStateRestored: 
       //[[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 
       [self restoreTransaction:transaction]; 

       break; 

      case SKPaymentTransactionStateFailed: 
       if (transaction.error.code != SKErrorPaymentCancelled) { 
        NSLog(@"An error encounterd"); 
       } 
       else { 
        NSLog(@"Cancelled!"); 
       } 
       //[[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 

       [self failedTransaction:transaction]; 

       break; 

     } 

    } 

} 

- (void)dealloc 
{ 
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self]; 

    [request release]; 
    [super dealloc]; 

} 

這行不上我的課[[SKPaymentQueue defaultQueue] finishTransaction:transaction];交易沒有完成工作。 和我的另一個問題是nsnotificationcenter調用多次,就像我第一次點擊它只是一次調用沒有問題,另一次是nsnotificationcenter調用3次。

回答

9

我得到了解決您的問題addTransaction服務器複製到您的問題,下面的代碼

嘗試的必備工作:

static bool hasAddObserver=NO; 

- (void)purchase { 
    if (!hasAddObserver) {//flag to fix this bug 
     /*=====================================*/ 
     [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 
     hasAddObserver=YES; 
    } 
    SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.test"]; 
    //[[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 
    [[SKPaymentQueue defaultQueue] addPayment:payment]; 

} 


- (void)provideContent:(NSString *)productIdentifier { 

    NSLog(@"Toggling flag for: %@", productIdentifier); 
    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:productIdentifier]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
    //[_purchasedProducts addObject:productIdentifier]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchasedNotification object:productIdentifier]; 

} 

- (void)completeTransaction:(SKPaymentTransaction *)transaction { 

    NSLog(@"completeTransaction..."); 

    //[self recordTransaction: transaction]; 
    [self provideContent: transaction.payment.productIdentifier]; 
    [[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 { 

    if (transaction.error.code != SKErrorPaymentCancelled) 
    { 
     NSLog(@"Transaction error: %@", transaction.error.localizedDescription); 
    } 




    [[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchaseFailedNotification object:transaction]; 


    //[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:transaction]; 

    [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 

} 



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

    for (SKPaymentTransaction *transaction in transactions) { 

     switch (transaction.transactionState) { 

      case SKPaymentTransactionStatePurchasing: 

       break; 

      case SKPaymentTransactionStatePurchased: 
       //[self completeTransaction:transaction]; 
       [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 

       break; 

      case SKPaymentTransactionStateRestored: 
       [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 
       //[self restoreTransaction:transaction]; 

       break; 

      case SKPaymentTransactionStateFailed: 
       if (transaction.error.code != SKErrorPaymentCancelled) { 
        NSLog(@"An error encounterd"); 
       } 
       else { 
        NSLog(@"Cancelled!"); 
       } 
       //[[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 

       [self failedTransaction:transaction]; 

       break; 

     } 

    } 

} 

- (void)dealloc 
{ 
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self]; 

    [request release]; 
    [super dealloc]; 

} 

歡迎您!

0

你可以這樣做:

- (void)buyProduct:(SKProduct *)product { 

NSLog(@"Buying %@...", product.productIdentifier); 
SKPayment * payment = [SKPayment paymentWithProduct:product]; 
[[SKPaymentQueue defaultQueue] addPayment:payment]; 

} 


- (void)validateReceiptForTransaction:(SKPaymentTransaction *)transaction { 
VerificationController * verifier = [VerificationController sharedInstance]; 
[verifier verifyPurchase:transaction completionHandler:^(BOOL success) { 
    if (success) { 
     NSLog(@"Successfully verified receipt!"); 
     [self provideContentForProductIdentifier:transaction.payment.productIdentifier]; 
    } else { 
     NSLog(@"Failed to validate receipt."); 
     [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 

    } 
}]; 
} 


#pragma mark SKPaymentTransactionOBserver 

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions 
{ 
for (SKPaymentTransaction *transaction in transactions) { 
    switch (transaction.transactionState) { 
     case SKPaymentTransactionStatePurchasing: 
      NSLog(@"Prossing............."); 

      break; 

     case SKPaymentTransactionStatePurchased: 
     { 

      [self completeTransaction:transaction]; 
      NSError* error; 
      NSDictionary* jsonDict = [NSJSONSerialization 
            JSONObjectWithData:transaction.transactionReceipt 

            options:kNilOptions 
            error:&error]; 
      NSLog(@"JSON Receipt: %@",jsonDict); 

      [[NSUserDefaults standardUserDefaults] setObject:jsonDict forKey:@"A"]; 
      NSLog(@"Purchase was a Success....."); 
     } 
      break; 
     case SKPaymentTransactionStateFailed: 

      [self failedTransaction:transaction]; 

      NSLog(@"Purchase cancelled"); 


      break; 

     case SKPaymentTransactionStateRestored: 

      [self restoreTransaction:transaction]; 

      default: 

      break; 
    }   
} 
} 



- (void)completeTransaction:(SKPaymentTransaction *)transaction { 
NSLog(@"completeTransaction..."); 

[self validateReceiptForTransaction:transaction]; 
[[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 
} 


- (void)restoreTransaction:(SKPaymentTransaction *)transaction { 
NSLog(@"restoreTransaction..."); 

[self validateReceiptForTransaction:transaction]; 
[[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 
} 


- (void)failedTransaction:(SKPaymentTransaction *)transaction { 

NSLog(@"failedTransaction..."); 

[[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 


if (transaction.error.code != SKErrorPaymentCancelled) 
{ 
    NSLog(@"Transaction error: %@", transaction.error.localizedDescription); 
    MedPulseAppDelegate *appdelegate =(MedPulseAppDelegate *)[[UIApplication sharedApplication]delegate]; 
    [appdelegate hideLoading]; 
} 

[[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 
} 



- (void)provideContentForProductIdentifier:(NSString *)productIdentifier { 

if ([productIdentifier isEqualToString:kMDPulseSubscriptionProductIdentifier]) { 
    [self purchaseSubscriptionWithMonths:1]; 
} 

[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier userInfo:nil]; 

    } 



    - (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error { 
NSLog(@"%s","User Cancel."); 



    } 



- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue 
{ 
NSLog(@"Restore completed transactions finished."); 
NSLog(@" Number of transactions in queue: %d", [[queue transactions] count]); 
for (SKPaymentTransaction *trans in [queue transactions]) 
    { 
    NSLog(@" transaction id %@ for product %@.", [trans transactionIdentifier], [[trans payment] productIdentifier]); 
    NSLog(@" original transaction id: %@ for product %@.", [[trans originalTransaction] transactionIdentifier], 
      [[[trans originalTransaction] payment]productIdentifier]); 


    if ([[[trans payment] productIdentifier] isEqual: kMDPulseSubscriptionProductIdentifier]) { 

     NSLog(@"Purchase Restored"); 

     // Do your stuff to unlock 
    } 
    } 
} 


- (void)restoreCompletedTransactions 
{ 
    NSLog(@"Restore Tapped in transaction process"); 
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; 
    } 

歡迎您!
如果您有任何疑問,意見下來

2

要解決此問題,只需添加[[SKPaymentQueue defaultQueue]removeTransactionObserver:self]; 右後[[SKPaymentQueue defaultQueue] finishTransaction:transaction];