Reputation: 159
I'm having issues getting In-App purchases to work. Every time I create a new test user and debug it on my iPhone after I hit okay with the password I automatically get "You've already purchased this but it hasn't bee downloaded."
I've added this to my viewDidLoad:
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
My purchase function looks like this:
- (IBAction)purchaseProduct:(id)sender {
SKProduct *product = [productArray objectAtIndex:0];
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
I also have:
-(void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions) {
if ([transaction transactionState] == SKPaymentTransactionStatePurchased) {
[self transactionDidComplete:transaction];
} else if ([transaction transactionState] == SKPaymentTransactionStateFailed) {
[self transactionDidFail:transaction];
} else if ([transaction transactionState] == SKPaymentTransactionStateRestored) {
[self transactionDidRestore:transaction];
} else {
NSLog(@"Unhandled case: %@", transaction);
}
}
}
As soon as I hit the purchase button without even entering in my test user account password I receive this error:
Unhandled case: <SKPaymentTransaction: 0x25a9a0>
Which means, I'm assuming the payment queue is being called before I can enter my password and hit okay to make the purchase!
I do have the code below in the functions "transactionDidComplete, transactiondidFail, transactionDidRestore"
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
Upvotes: 2
Views: 1801
Reputation: 5156
You need to process SKPaymentTransactionStatePurchasing
something like:
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
//NSLog(@"updatedTransactions %@",transactions);
NSNumber *paymentOk = [NSNumber numberWithBool:NO];
for(SKPaymentTransaction *t in transactions){
//transaction failed
if(t.transactionState == SKPaymentTransactionStateFailed){
//[self doTransactionFailure:t.error.localizedDescription];
[[SKPaymentQueue defaultQueue] finishTransaction:t];
continue;
}
if(t.transactionState == SKPaymentTransactionStatePurchasing){
continue;
}
//transaction type here is success or restored
//extract the app object
if([t.payment.productIdentifier caseInsensitiveCompare:self.inAppPurchaseIdentifier]== NSOrderedSame){
NSLog(@"Got payment for %@",t.payment.productIdentifier);
paymentOk = [NSNumber numberWithBool:YES];
}
[[SKPaymentQueue defaultQueue] finishTransaction:t];
}
if([paymentOk boolValue]==YES){
[self.delegate didPurchaseWithIdentifier:self.inAppPurchaseIdentifier];
}
else{
[self.delegate didFailPurchaseWithIdentifier:self.inAppPurchaseIdentifier andReason:@"Unknown"];
}
}
Upvotes: 1