Already purchased StoreKit subscriptions

I use renewable subscriptions when purchasing apps in an iOS app. When a user tries to purchase a subscription that they have already paid for a message, iTunes displays "You are currently subscribed to this."

How can I detect when this event occurred so that I can process the transaction and provide access to my application.

In the process paymentQueue: updatedTransactions: the observer method passes through SKPaymentTransactionStateFailed. How can I distinguish between this type of failure and other failures, such as the user by clicking cancel buttons?

I am sending a transaction that is being returned or I need to call restorePreviousTransactions.

Apple's docs say: β€œIf a user tries to purchase a non-consumable product or a renewable subscription that they have already purchased, your application receives a regular transaction for this item, not a recovery transaction. However, the user is not charged for this product. Your application must process these transactions identically to transactions with the original transaction. "

+6
source share
1 answer
Q: How I can detect when this event (currently subscribed) has occurred so that I can process the transaction and grant access to my app. 

You discover when a subscription exists through verification with Apple (I use the php website code for this), you get a β€œstatus code” response and you can check if it is 21006 (the subscription has expired), or others ( I believe that everything except 0 and 21006 is an actual error).

As I do things, I store transaction data in a PLIST file, which I store in the document directory.

You can add additional fields to PLIST, such as expiryDates, boolean flags, etc.

Thus, you have a copy of the receipt, although you should always check it, because it may have expired.

Q: In paymentQueue: updatedTransactions: the observer method passes through SKPaymentTransactionStateFailed. How can I distinguish between this type of failure and other failures, such as the user presses the cancel button?

You use the switch statement in the updatedTransactions method to define different types of responses.

Example

 -(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error { NSString *message = [NSString stringWithFormat:@"Transaction failed with error - %@", error.localizedDescription]; NSLog(@"Error - %@", message); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; [alertView release]; } -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { NSLog(@"updatedTransactions"); for (SKPaymentTransaction *transaction in transactions) { switch (transaction.transactionState) { case SKPaymentTransactionStatePurchasing: // take action whilst processing payment break; case SKPaymentTransactionStatePurchased: // take action when feature is purchased break; case SKPaymentTransactionStateRestored: // take action to restore the app as if it was purchased break; case SKPaymentTransactionStateFailed: if (transaction.error.code != SKErrorPaymentCancelled) { // Do something with the error } // end if break; default: break; } // end switch } // next 

TransactionStateFailed handles crashes, although I do not code to cancel, as there is no reason for this in my application.

Q: I am sending a transaction that is being returned or I need to call restorePreviousTransactions.

I believe that StoreKit handles this internally with the endTransaction method and restorePreviousTransaction methods

t

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

complete transactions

I hope this helps

0
source

Source: https://habr.com/ru/post/889388/


All Articles