我正在做一个“应用内购买 - 消耗品”系统。第一次可以正常购买,但是下次尝试购买时,就不行了。所以我换了一个测试用户,它也只能第一次购买。显示“SK payment transaction state failed”,我该如何解决?
- (void)buyProduct:(int)myNumber{
SKPayment *payment = [SKPayment paymentWithProductIdentifier:[appDelegate.ArrayProductName objectAtIndex:myNumber]];
[[SKPaymentQueue defaultQueue] addPayment:payment];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
- (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:
[self restoreTransaction:transaction];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled){
NSLog(@"An error encounterd : %@",transaction.error.code);
}
break;
default:
break;
}
}
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
[self verifyReceipt:(SKPaymentTransaction *)transaction];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
}
- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction {
NSString* jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];
NSString* completeString = [NSString stringWithFormat: @"myurl?receipt=%@", jsonObjectString];
NSURL* urlForValidation = [NSURL URLWithString: completeString];
NSMutableURLRequest* validationRequest = [[NSMutableURLRequest alloc] initWithURL: urlForValidation];
[validationRequest setHTTPMethod: @"GET"];
NSData * responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse: nil error: nil];
NSString* responseString = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];
NSLog(@"return data : %@",responseString);
int success = [responseString intValue];
return (success == 0);
}