Reputation: 526
As I know that Apple recommands to get a Buy and a Restore button (I have these in the settings view of my application), on other views I only have the purchase button.
When a user clicks on the "Buy" button and Apple detects that this user already has purchased this product, he will ask the user to restore this purchase for free (everything is fine here). When the user clicks yes, then updateTransactions:
is called and it always goes on the case SKPaymentTransactionStatePurchased:
and not in the case SKPaymentTransactionStateRestored:
.
Why is that ? Is there a way to distinguish the restore from the new purchase with updatedTransactions:
?
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
for(SKPaymentTransaction *transaction in transactions){
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchasing: //NSLog(@"Transaction state -> Purchasing");
//called when the user is in the process of purchasing, do not add any of your own code here.
break;
case SKPaymentTransactionStatePurchased:
//this is called when the user has successfully purchased the package (Cha-Ching!)
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"Purchase" action:@"Purchase Completed!" label:shopNameSelected value:nil] build]];
[self doGoPremium];
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
//NSLog(@"Transaction state -> Purchased");
break;
case SKPaymentTransactionStateRestored:
//NSLog(@"Transaction state -> Restored Here");
//add the same code as you did from SKPaymentTransactionStatePurchased here
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"Purchase" action:@"Purchase Restored" label:shopNameSelected value:nil] build]];
[self doGoPremium];
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
//called when the transaction does not finnish
[MBProgressHUD hideHUDForView:self.view animated:YES];
if(transaction.error.code != SKErrorPaymentCancelled){
//NSLog(@"Transaction state -> Cancelled");
//the user cancelled the payment ;(
// Add some analytics point.
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"Purchase" action:@"Purchase Canceled" label:shopNameSelected value:nil] build]];
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
Upvotes: 3
Views: 630
Reputation: 9722
Purchasing something you already own (despite the UIAlert saying its being restored) triggers the SKPaymentTransactionStatePurchased state. The SKPaymentTransactionStateRestored state only happens when you do:
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
Upvotes: 1