在开发 iPhone 应用程序时,我在 restoreCompletedTransactions 方面遇到了一些麻烦。在沙盒环境中工作时会出现下面列出的所有问题。该应用程序尚未销售。它是使用在模拟器 5.0 和 5.1 中运行的 Xcode 4.3.2 开发的。我遇到的问题是:
- 每次应用程序启动并调用 addTransactionObserver 时,都会调用 updatedTransactions 并购买一笔交易。在每次回调时,我的代码都会调用 finishTransaction: 来完成购买,但每次我启动应用程序时都会出现这个问题。对完全相同的购买进行确认。
- 调用 [[SKPaymentQueue defaultQueue] restoreCompletedTransactions] 将不会列出使用该帐户购买的所有非消耗品。我购买了 2 次非消耗品,每次只退回 1 次。如果我尝试购买列表中缺少的物品,我将收到一条消息,指出该物品已被购买。列表中缺少的项目不是我遇到问题 1 的项目(如上所列)。
在这一点上,我完全被卡住了。我的应用程序依赖 AppStore 来返回有关非消耗品的信息,因为我们没有将这些数据保存在我们自己的服务器中。但是我们需要确保 AppStoreKit 返回包含所有购买项目的列表。不只是一些。
这是我用来测试 restoreCompletedTransactions 的相关代码:
- (void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSLog(@"updatedTransactions started. -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ");
NSLog(@"Number of transactions received: %d.", [transactions count]);
int count = 0;
for (SKPaymentTransaction *trans in transactions)
{
NSLog(@"Data for transaction %d: ", ++count);
NSString *transId = [trans transactionIdentifier];
switch ([trans transactionState])
{
case SKPaymentTransactionStatePurchasing:
NSLog(@"Purchasing transaction: %@", transId);
if (transId == nil)
{
NSLog(@" Original transaction Id: %@", [[trans originalTransaction] transactionIdentifier]);
}
NSLog(@" No action taken in update");
break;
case SKPaymentTransactionStateFailed:
NSLog(@"Purchase transaction failed for transaction %@", transId);
NSLog(@" Error %d (%@)", [[trans error] code], [[trans error] localizedDescription]);
NSLog(@" Action Taken: finish transaction.");
[queue finishTransaction: trans];
break;
case SKPaymentTransactionStatePurchased:
NSLog(@"Purchased transaction %@", transId);
NSLog(@" Purchased qty %d of product %@", [[trans payment] quantity], [[trans payment] productIdentifier]);
NSLog(@" Action: called [queue finishTransaction:] to complete purchase");
[queue finishTransaction: trans];
break;
case SKPaymentTransactionStateRestored:
{
SKPayment *paym = [trans payment];
SKPaymentTransaction *origTrans = [trans originalTransaction];
SKPayment *origPayment = [[trans originalTransaction] payment];
NSLog(@"Transaction restored: %@ with original transaction %@", transId, [[trans originalTransaction] transactionIdentifier]);
NSLog(@" TRANSACTION DATA:");
NSLog(@" purchased %d of product %@ on %@.",
[paym quantity],
[paym productIdentifier],
[[trans transactionDate] description]);
NSLog(@" ORIGINAL TRANSACTION DATA:");
NSLog(@" purchased %d of product %@ on %@.",
[origPayment quantity],
[origPayment productIdentifier],
[[origTrans transactionDate] description]);
NSLog(@" No action taken.");
break;
}
default:
NSLog(@"Unexpected transaction state: %d", [trans transactionState]);
NSLog(@" No action taken.");
break;
}
}
NSLog(@"");
NSLog(@"updatedTransactions ended. -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ");
}
- (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]);
}
NSLog(@"");
}
// Method to restore transactions when user clicks button in application
- (void) onRestoreCompletedTransactions:(id)sender
{
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}