我在 iPhone 上的应用程序中模拟购买自动续订订阅。我的问题是购买被认为是由 App Store 完成的,而实际上不是。
这是发生了什么:
- 用户按下按钮购买续订订阅
- 用户提供他的 iTunes 密码并确认购买
- 该应用程序将从应用程序商店收到的收据提交到我的服务器以检查有效性
- 服务器返回“ok”或“not ok”字符串。该应用程序仅在“确定”时调用 finishTransaction
当第 3 步出现网络故障时,我遇到了问题。我无法验证收据。但是如果用户第二次尝试购买,应用商店告诉他他已经订阅了,即使我没有调用该finishTransaction
方法完成购买!
这是预期的行为吗?应用商店不应该将未完成的交易视为未完成,还是我错过了什么?我欢迎任何解决此问题的建议。
-(void) userPurchase:(SKProduct*) product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
NSLog(@"paymentQueue updatedTransaction");
for (SKPaymentTransaction * transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
break;
case SKPaymentTransactionStatePurchased:
[self recordSubscription:transaction];
break;
case SKPaymentTransactionStateFailed:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self recordSubscription:transaction];
break;
default: NSLog(@"Default");
break;
}
};
}
-(void) recordSubscription:(SKPaymentTransaction*)transaction{
NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:jsonObjectString,@"receiptdata", nil];
[[AFNetworkSubClass sharedClient] postPath:@"myserver" params:params
success:^(AFHTTPRequestOperation *operation, id output) {
/* some code */
if([valstring isEqualToString:@"ok"]){
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"validation failed");
}