0

私はアプリの購入 (非消耗型 - 曲) に実装しています。ユーザーが曲を購入するたびに購入をタップすると、startPurchase 関数を呼び出します。曲のコンテンツはサーバー経由で配信されます。

何かを購入し、もう一度同じものを再購入しようとすると、購入の復元として扱われません。新しい購入が行われます。デリゲート メソッドが複数回呼び出されます。

実は私の問題は、購入をタップして支払いを続行し、そのアイテムを購入したことです。

繰り返しますが、同じアイテムを購入しようとすると、「このアイテムを既に購入しています。[OK] をタップすると、ダウンロードするには [OK] をタップしてください」という Apple アラートが表示れます ? 助けてください

私を助けてください

- (void)startPurchase:(NSString*)inProductId{

  if ([SKPaymentQueue canMakePayments])
  {
     myProductId = inProductId

    SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:myProductId]];
    productsRequest.delegate = self;
    [productsRequest start];
  }
  else {
    NSLog(@"Parental-controls are enabled");
      }

}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:
  (SKProductsResponse *)response {

  NSLog(@"response received");
  SKProduct *validProduct = nil;
  int count = [response.products count];

  UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Message" message:
  [NSString stringWithFormat:@"%d",response.products.count] delegate:
  self cancelButtonTitle:@"OK" otherButtonTitles:nil];

  [alert show];
  [alert release];

  if (count > 0) {
    validProduct = [response.products objectAtIndex:0];
    NSLog(@"products available");
    SKPayment *payment = [SKPayment paymentWithProductIdentifier:myProductId];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
  }
  else if (!validProduct) {
    NSLog(@"No products available");
  }
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        SKPayment *payment = [transaction payment];

      if([payment.productIdentifier isEqualToString:myProductId])
      {
          NSLog(@"%@payement queue payment.productIdentifier",payment.productIdentifier);

        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                NSLog(@"completeTransaction");
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                NSLog(@"failedTransaction");
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                NSLog(@"restoreTransaction");
                [self restoreTransaction:transaction];
            default:
                break;
        }
      }
    }
}

- (void)provideContent:(NSString *)productIdentifier
{
    NSLog(@"Provide Content %@", productIdentifier);

    }

- (void)recordTransaction:(SKPaymentTransaction *)transaction {
    NSLog(@"inside the recordTransaction");

}

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    [self recordTransaction: transaction];
    [self provideContent: transaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];


}

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

   }

- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        if(transaction.error.code == SKErrorUnknown) {
            NSLog(@"Unknown Error (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
            UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
            message: @"There was an error purchasing this item please try again."
            delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [failureAlert show];
            [failureAlert release];
        }

        if(transaction.error.code == SKErrorClientInvalid) {
            NSLog(@"Client invalid (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
            UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
            message: @"There was an error purchasing this item please try again."
            delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [failureAlert show];
            [failureAlert release];
        }

        if(transaction.error.code == SKErrorPaymentInvalid) {
            NSLog(@"Payment invalid (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
            UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
            message: @"There was an error purchasing this item please try again."
            delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [failureAlert show];
            [failureAlert release];
        }

        if(transaction.error.code == SKErrorPaymentNotAllowed) {
            NSLog(@"Payment not allowed (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
            UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
            message: @"There was an error purchasing this item please try again."
            delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [failureAlert show];
            [failureAlert release];
        }
    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
4

2 に答える 2

0

私はあなたの問題がトランザクションサーバーの重複を追加することを反映していると思いますトランザクションサーバーの重複を避けるために以下のコードを試してください

- (void)startPurchase:(NSString*)inProductId{

  if ([SKPaymentQueue canMakePayments])
  {
     myProductId = inProductId

    SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:myProductId]];
    productsRequest.delegate = self;
    [productsRequest start];
  }
  else {
    NSLog(@"Parental-controls are enabled");
      }

}
static bool hasAddObserver=NO;
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:
  (SKProductsResponse *)response {

  NSLog(@"response received");
  SKProduct *validProduct = nil;
  int count = [response.products count];

  UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Message" message:
  [NSString stringWithFormat:@"%d",response.products.count] delegate:
  self cancelButtonTitle:@"OK" otherButtonTitles:nil];

  [alert show];
  [alert release];

  if (count > 0) {
    validProduct = [response.products objectAtIndex:0];
    NSLog(@"products available");
    SKPayment *payment = [SKPayment paymentWithProductIdentifier:myProductId];
    if (!hasAddObserver) {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    hasAddObserver=YES;
    }
    [[SKPaymentQueue defaultQueue] addPayment:payment];
  }
  else if (!validProduct) {
    NSLog(@"No products available");
  }
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        SKPayment *payment = [transaction payment];

      if([payment.productIdentifier isEqualToString:myProductId])
      {
          NSLog(@"%@payement queue payment.productIdentifier",payment.productIdentifier);

        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                NSLog(@"completeTransaction");
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                NSLog(@"failedTransaction");
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                NSLog(@"restoreTransaction");
                [self restoreTransaction:transaction];
            default:
                break;
        }
      }
    }
}

- (void)provideContent:(NSString *)productIdentifier
{
    NSLog(@"Provide Content %@", productIdentifier);

    }

- (void)recordTransaction:(SKPaymentTransaction *)transaction {
    NSLog(@"inside the recordTransaction");

}

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    [self recordTransaction: transaction];
    [self provideContent: transaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];


}

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

   }

- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        if(transaction.error.code == SKErrorUnknown) {
            NSLog(@"Unknown Error (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
            UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
            message: @"There was an error purchasing this item please try again."
            delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [failureAlert show];
            [failureAlert release];
        }

        if(transaction.error.code == SKErrorClientInvalid) {
            NSLog(@"Client invalid (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
            UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
            message: @"There was an error purchasing this item please try again."
            delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [failureAlert show];
            [failureAlert release];
        }

        if(transaction.error.code == SKErrorPaymentInvalid) {
            NSLog(@"Payment invalid (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
            UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
            message: @"There was an error purchasing this item please try again."
            delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [failureAlert show];
            [failureAlert release];
        }

        if(transaction.error.code == SKErrorPaymentNotAllowed) {
            NSLog(@"Payment not allowed (%d), product: %@", (int)transaction.error.code, transaction.payment.productIdentifier);
            UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle :@"In-App-Purchase Error:"
            message: @"There was an error purchasing this item please try again."
            delegate : self cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [failureAlert show];
            [failureAlert release];
        }
    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

あなたの私は、トランザクションサーバーが追加されているか、コードの下にないことを確認しています:

if (!hasAddObserver) {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    hasAddObserver=YES;
    }

hasAddObserver 変数 static bool データ型は、すでに追加されているかどうかをチェック 追加されたトランザクション サーバーを簡単にチェックできます。

于 2012-06-29T09:40:34.663 に答える