Storekit Restore delegate method called multiple time - ios

I have implemented in-app purchase in my app. My problem is when a new user clicks on restore button, the process is not stopped after completing 1 cycle And execute again and again. After executing 3 to 4 times process get stopped.
I want to stop process instant when a new user doesn't have any purchased transaction.
Here is code of my IAP class.
- (void)restorePurchases
{
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
[ProgressHUD dismiss];
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
[ProgressHUD dismiss];
}
// Sent when transactions are removed from the queue (via finishTransaction:).
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions
{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
[ProgressHUD dismiss];
}
// Called when the transaction status is updated
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchasing:
[self transactionStart:transaction];
break;
case SKPaymentTransactionStatePurchased:
[self transactionComplete:transaction];
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self transactionFailed:transaction];
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
{
[self transactionRestored:transaction];
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
}
default:
break;
}
}
}
#pragma mark - Local method for mange transactions -
-(void)transactionStart:(SKPaymentTransaction *)transaction
{
if([self.delegate respondsToSelector:#selector(startPayment:)]){
[self.delegate startPayment:transaction];
}
}
-(void)transactionComplete:(SKPaymentTransaction *)transaction
{
if([self.delegate respondsToSelector:#selector(completePayment:)]){
[self.delegate completePayment:transaction];
}
}
-(void)transactionFailed:(SKPaymentTransaction *)transaction
{
if ([self.delegate respondsToSelector:#selector(failedPayment:)]) {
[self.delegate failedPayment:transaction];
}
}
-(void)transactionRestored:(SKPaymentTransaction *)transaction
{
if([self.delegate respondsToSelector:#selector(restoredPayment:)]){
[[AppDelegate sharedAppDelegate].arrayConacatResponse addObject:[NSString stringWithFormat:#"transactionRestored method "] ];
[self.delegate restoredPayment:transaction];
}
}
In Restore payment method
- (void)restoredPayment:(SKPaymentTransaction *)transactions
{
if ([transactions.payment.productIdentifier isEqualToString:monthlySubscription]) {
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSError *receiptError;
BOOL isPresent = [receiptURL checkResourceIsReachableAndReturnError:&receiptError];
if (isPresent)
{
NSData *receiptData = [[NSData alloc] initWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
BOOL isSubscriptionPeriodValid = [[DataBase sharedInstance] getSubscriptionStatusFromAppleWithReceipt:receiptData];
if (isSubscriptionPeriodValid)
{
[self removeFromSuperview];
}else{
// Here I want to stop
[CommonUtilities showAlertWithTitle:#"" Message:#"You have no transaction to restore."];
}
}
}
[ProgressHUD dismiss];
}

Related

how to Restore IAP for non-consumable product in iOS

I want to know that
do one have to put two buttons separate one for Upgrade and Other for Restore for restore previous transactions??
What steps should be taken by my self to avoid reject chances of App for Restore transaction.
I used below code for restore non-consumable In App Purchase.
If any changes in below code please let me know.
- (void)buyProduct:(SKProduct *)product {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
//[[SKPaymentQueue defaultQueue] addPayment:payment];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction * transaction in transactions) {
switch (transaction.transactionState)
{
// Call the appropriate custom method for the transaction state.
case SKPaymentTransactionStatePurchasing:
[self showTransactionAsInProgress:transaction deferred:NO];
break;
case SKPaymentTransactionStateDeferred:
[self showTransactionAsInProgress:transaction deferred:YES];
break;
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
// For debugging
NSLog(#"Unexpected transaction state %#", #(transaction.transactionState));
break;
}
};
}
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
NSLog(#"Restore Completed Transactions Failed WithError...%#",error);
[self failedMessage:#"Restore Completed Transactions Failed"];
[self stopIndicator];
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSMutableArray *arrPurchasedItemIDs = [[NSMutableArray alloc] init];
for (SKPaymentTransaction *transaction in queue.transactions)
{
NSString *productID = transaction.payment.productIdentifier;
[arrPurchasedItemIDs addObject:productID];
NSLog(#"arrPurchasedItemIDs : %#",arrPurchasedItemIDs);
}
NSLog(#"Restore Completed");
[self completeMessage:#"Restore Completed"];
[self stopIndicator];
}
Here how you can separate the Restore purchase code :
-(void)doClickRestore {
[APP_DEL doStartSpinner];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
// Purchase success Transaction
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
// Purchase fail Transaction
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
{
[self doStopSpinner];
[self restoreTransaction:transaction];
}
default:
break;
}
}
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction {
[self doStopSpinner];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
Note :
Clicking on Restore purchases will restore all purchased non consumable inApp purchases
Hope it will helps.
These below two mehods are optional but if you want you can use it.
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue

Restoring in app purchase issues

I have successfully implemented my ad removal non-consumable in-app purchase. I read that I must also have a "restore" button in my app. I have tried implementing a restore function for the last two days, reading all other posts on stackoverflow and nothing has worked for me. When the restore button is pressed, it requests an email and password as expected but even if I haven't previously purchased the product on that Apple ID it will restore it anyway. Below is the code i am using. Any help will be greatly appreciated!
- (IBAction)RestoreProduct:(id)sender {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
NSLog(#"Calling Restore");
_restoreButton.enabled = NO;
_productTitle.text = [NSString stringWithFormat:#"Checking for product"];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(#"Calling First Function");
NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];
NSLog(#"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
NSString *productID = transaction.payment.productIdentifier;
[purchasedItemIDs addObject:productID];
NSLog(#"This Part");
[self unlockFeature];
_productTitle.text = [NSString stringWithFormat:#"Purchase Restored!"];
}
}
- (void)paymentQueue:(SKPaymentQueue*)queue restoreCompletedTransactionsFailedWithError: (NSError*)error
{
NSLog(#"error");
}
EDIT.
Okay, Here is all the code I have from the restore button to the #end.
- (IBAction)RestoreProduct:(id)sender {
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
NSLog(#"Calling Restore");
_restoreButton.enabled = NO;
_productTitle.text = [NSString stringWithFormat:#"Checking for product"];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(#"Calling First Function");
NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];
NSLog(#"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
NSString *productID = transaction.payment.productIdentifier;
[purchasedItemIDs addObject:productID];
NSLog(#"This Part");
}
}
- (void)paymentQueue:(SKPaymentQueue*)queue restoreCompletedTransactionsFailedWithError: (NSError*)error
{
NSLog(#"error");
}
#pragma mark -
#pragma mark SKPaymentTransactionObserver
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
[self unlockFeature];
_productTitle.text = [NSString stringWithFormat:#"Purchase Complete!"];
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
NSLog(#"Transaction Failed");
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
default:
break;
}
}
}
-(void)unlockFeature
{
_buyButton.enabled = NO;
[_buyButton setTitle:#"Purchased"
forState:UIControlStateDisabled];
[_homeViewController enableLevel2];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_buyButton.enabled = NO;
_restoreButton.enabled = NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have a button that calls
+ (void)restoreABC
{
NSLog(#"Restore Purchases");
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
I'm guessing your issue is that you are also calling
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
immediately above:
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
Delete that first line and you are probably good to go.
EDIT:
Based on the comments below, sounds like the [self unlockfeature] line is the culprit :-)
EDIT #2:
Add
case SKPaymentTransactionStateRestored:
NSLog(#"Feature Previously Purchased");
[self unlockFeature];
break;
to
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

Restore inapp purchase Ios

I having problem to get the previus purchases from app store. I can buy products and validate the receipt but when I try to restore them, the list of purchased transactions is empty. :(
Any idea? Something i forgoten or dosen't it work in sandbox?
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray*)transactions
{
//Is only fired when buying and not restoring.
for (SKPaymentTransaction * transaction in transactions) {
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
};
}
- (void)restoreCompletedTransactions {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
}
-(void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"completeTransaction...");
[self validateReceiptForTransaction:transaction];
_isPurchased = YES;
[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
define kMDPulseSubscriptionProductIdentifier #"Your Product ID here...."
SKPaymentTransactionOBserver:
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
NSLog(#"Prossing.............");
break;
case SKPaymentTransactionStatePurchased:
{
[self completeTransaction:transaction];
NSError* error;
NSDictionary* jsonDict = [NSJSONSerialization
JSONObjectWithData:transaction.transactionReceipt
options:kNilOptions
error:&error];
NSLog(#"JSON Receipt: %#",jsonDict);
[[NSUserDefaults standardUserDefaults] setObject:jsonDict forKey:#"A"];
NSLog(#"Purchase was a Success.....");
}
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
NSLog(#"Purchase cancelled");
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
}
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"completeTransaction...");
[self validateReceiptForTransaction:transaction];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"restoreTransaction...");
[self validateReceiptForTransaction:transaction];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)failedTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"failedTransaction...");
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
if (transaction.error.code != SKErrorPaymentCancelled)
{
NSLog(#"Transaction error: %#", transaction.error.localizedDescription);
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void)provideContentForProductIdentifier:(NSString *)productIdentifier {
if ([productIdentifier isEqualToString:kMDPulseSubscriptionProductIdentifier]) {
[self purchaseSubscriptionWithMonths:1];
}
[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier userInfo:nil];
}
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {
NSLog(#"%s","User Cancel.");
}
- (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]);
if ([[[trans payment] productIdentifier] isEqual: kMDPulseSubscriptionProductIdentifier]) {
NSLog(#"Purchase Restored");
// Do your stuff to unlock
}
}
}
- (void)restoreCompletedTransactions
{
NSLog(#"Restore Tapped in transaction process");
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
When you call this method for restoring
-(void)restoreCompletedTransactions
{
NSLog(#"Restore Tapped in transaction process");
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
//Then this delegate Function Will be fired
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
alert = [[UIAlertView alloc] initWithTitle:#"IN APP PURCHASE" message:#"Processing" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[alert addSubview:progress];
[progress startAnimating];
[alert show];
[progress release];
for (SKPaymentTransaction *transaction in queue.transactions)
{
// Use this Commented code to Log and check details about your Products
/**
NSLog(#"%d",queue.transactions.count);
NSMutableArray * purchasedItemIDs = [[NSMutableArray alloc] init];
for (SKPaymentTransaction *transaction in queue.transactions)
{
NSString *productID = transaction.payment.productIdentifier;
[purchasedItemIDs addObject:productID];
NSLog(#"%#",purchasedItemIDs);
}*/
if( queue.transactions.count == 0 ) {
// User hav't purchased yet.
NSLog(#"User hav't purchased yet.");
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Restore" message:#"You have not purchased yet"
delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:Nil, nil];
[alertView show];
[alertView release];
}else {
// Upgrade to Full version.
NSLog(#"Upgrade to Full version.");
// Do stuffs here
//finally finish transaction
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
}
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
Hope this helps!!

Transaction isn't being completed

I want the payment transaction to complete once the download transaction has completed but when the download finishes for some reason the transaction state is not completed. Do i have to put the payment transaction back in the queue after the download is complete here is more code.
- (void)buyProduct:(SKProduct *)product {
NSLog(#"Buying %#...", product.productIdentifier);
SKPayment * payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for (SKPaymentTransaction * transaction in transactions) {
if(transaction.downloads){
[[SKPaymentQueue defaultQueue] startDownloads:transaction.downloads];
}else{
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
}
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray *)downloads;
{
for (SKDownload *download in downloads) {
if (download.downloadState == SKDownloadStateFinished) {
[self processDownload:download]; //handle download
// now we're done
NSLog(#"Download finished");
[[SKPaymentQueue defaultQueue] finishTransaction:download.transaction];
//Do I need to add the payment transaction back in the queue? Im worried the payment will go through twice?
} else if (download.downloadState == SKDownloadStateActive) {
NSString *productID = download.contentIdentifier; // in app purchase identifier
NSTimeInterval remaining = download.timeRemaining; // secs
float progress = download.progress; // 0.0 -> 1.0
NSLog(#"Product ID:%#", productID);
NSLog(#"Time remaining: %f", remaining);
NSLog(#"Download progress percent: %f", progress);
} else { // waiting, paused, failed, cancelled
NSLog(#"Warn: not handled: %d", download.downloadState);
}
}
}
These are my methods I created for my transaction state if it matters at all? None of them are being executed anyways so I don't think it matter but here it is.
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"completeTransaction...");
[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"restoreTransaction...");
[self provideContentForProductIdentifier:transaction.originalTransaction.payment.productIdentifier];
if(transaction.downloads){
[[SKPaymentQueue defaultQueue] startDownloads:transaction.downloads];
NSLog(#"Download exists");
}else{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
}
- (void)failedTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"failedTransaction...");
if (transaction.error.code != SKErrorPaymentCancelled)
{
NSLog(#"Transaction error: %#", transaction.error.localizedDescription);
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

Restore transaction doesn't download the content IAP IOS

I have been struggling with this issue since 2 days. I have implemented In App purchase successfully with downloadable content . But whenever I delete app and restore the purchase , restore doesn't actually download the content. But if I add download code in restoreTransaction it never calls finish transaction which cause the app to behave weirdly(which is resonable) , every time I delete app and reinstall it. Any help is appreciated!!
Please let me know if anyone needs further explanation. Thanks!!
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction * transaction in transactions) {
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
if(transaction.downloads)
[self download:transaction];
else
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
if(transaction.downloads)
[self restoreDownload:transaction];
else
[self restoreTransaction:transaction];
default:
break;
}
};
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray *)downloads
{
for (SKDownload *download in downloads)
{
switch (download.downloadState) {
case SKDownloadStateActive:
NSLog(#"Download progress = %f",
download.progress);
NSLog(#"Download time = %f",
download.timeRemaining);
break;
case SKDownloadStateFinished:
{
NSLog(#"URL %#",download.contentURL);
}
break;
default:
break;
}
}
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"completeTransaction...");
[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"restoreTransaction...");
[self provideContentForProductIdentifier:transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)restoreDownload:(SKPaymentTransaction *)transaction {
NSLog(#"restoreDownload...");
//[self validateReceiptForTransaction:transaction];
[self provideContentForProductIdentifier:transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] startDownloads:transaction.downloads];
}
- (void)download:(SKPaymentTransaction *)transaction {
NSLog(#"Download Content...");
[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] startDownloads:transaction.downloads];
//[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

Resources