首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何完成在应用程序中的支付转换

如何完成在应用程序中的支付转换
EN

Stack Overflow用户
提问于 2012-06-29 09:24:45
回答 2查看 3.3K关注 0票数 0

我正在应用程序购买(非耗用类型歌曲).when中实现一个用户点击购买每一首歌曲,我称之为startPurchase功能.my歌曲内容是通过我的服务器交付的。

在我购买了一些东西并再次尝试重新购买时,它不会被视为purchase.it的还原,使一个新的purchase.the委托方法被多次调用。

实际上,我的问题是,我点击购买和继续付款,并购买了该项目。

再次,当我试图购买同样的项目苹果警报说“你已经购买了这个项目,点击可以下载”当我点击" ok ".this不属于SKPaymentTransactionStatePurchased.why,而是它去了SKPaymentTransactionStatePurchased.why,这正在发生吗?请帮帮忙

请帮帮我

代码语言:javascript
复制
- (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];
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-29 09:40:34

我认为您的问题是添加事务服务器副本,反映下面代码的尝试,以避免添加事务服务器副本,这可能是有效的:

代码语言:javascript
复制
- (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];
}

在您的I am检查中,是否在代码下面添加了事务服务器:

代码语言:javascript
复制
if (!hasAddObserver) {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    hasAddObserver=YES;
    }

hasAddObserver变量静态bool数据类型是检查是否已经添加,易于添加检查事务服务器!

票数 0
EN

Stack Overflow用户

发布于 2013-04-22 13:05:40

就苹果官方文件上的说法而言,这是正常行为:

“如果用户试图购买可恢复的产品(而不是使用您实现的恢复接口),则应用程序将接收该项的常规事务,而不是恢复事务。但是,该产品不会再次向用户收取费用。您的应用程序应该以与原始事务相同的方式对待这些事务。”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11259238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档