What is a proper way to validate consumable IAP with Apple server? - ios

From iOS7 Apple deprecated transactionReceipt property of SKPaymentTransaction instance, and now we have one big receipt contained everything. In my app I have several consumable purchases. My code:
#pragma mark -
#pragma mark SKPaymentTransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
...
}
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
// [self sendRecieptToServer:transaction.transactionReceipt]; // deprecated
[self testMainReceipt];
[self deliverPurchaseNotificationFirIdentifier:transaction.payment.productIdentifier];
[SKPaymentQueue.defaultQueue finishTransaction:transaction];
}
- (void)testMainReceipt {
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
[self sendRecieptToServer:receipt]; //look at the 'status' field and determine whether a good purchase or not
}
From Apple response I see 'in_app' filed with an array, containing always 1 item - my most recent purchase, no matter how many times I made purchases before.
Am I right that this how consumable purchases work? And I'll always get array with 1 item and 'status' field for this most recent purchase? Or there its better way?

You'll need to buy these items every time you want them, and you can't download them again for free. If you remove and reinstall an app, or install an app on a new device, you might lose your consumable purchases. For example, if you install a game on your iPod touch that you started playing on your iPhone, the game levels sync, but extra health or experience points you bought on your iPhone don't sync.
Consumable Products can be Game Currency,Extra Health,Extra experience points.
Consumable Products can't be restored,& also appear once in receipt.
You can find more details link.
you can also check this.

You can go for appStoreReceiptURL method and if it is not available (on older systems), you can fall back to validating the transactionReceipt property of an SKPaymentTransaction object with the App Store.
For details, see Validating Receipts With the App Store.
Refer: https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateLocally.html#//apple_ref/doc/uid/TP40010573-CH1-SW2

Related

In-App Purchase - Restore only one product

I am currently implementing the restore functionality for my app. I have several In-App Purchase products (non-consumable) and I want to allow the user to restore only one or two, so that I don't download more content than needed. In the In-App Purchase Programming Guide its stated that I can do that like this:
NSMutableArray *productIDsToRestore = <# From the user #>;
SKPaymentTransaction *transaction = <# Current transaction #>;
if ([productIDsToRestore containsObject:transaction.transactionIdentifier]) {
// Re-download the Apple-hosted content, then finish the transaction
// and remove the product identifier from the array of product IDs.
} else {
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
However, in my experience, the product ID doesn't match the transaction identifier. By product ID I understand the string used to retrieve the products from the App Store (something like "com.mydomain.myproduct"). The transaction identifier seems to be a long number, even if it's received as a string.
What am I missing and how to implement this?
Thanks!

What should the app do in response to a deferred SKPaymentTransaction?

I have in-app purchases in my app, and new to iOS 8 are "deferred" transactions, partially described in a tech note
I understand what it does and that I need to not block the UI, and update my UI to reflect that the transaction state is deferred. But what am I supposed to place in the method -(void)transactionDeferred:(SKPaymentTransaction *)transaction to disregard the transaction for the time being?
Do I only have update the UI? Also what should the content of the UI be? Do I need to replace the price label with something like "Your purchase is deferred"? I don't think there is a way to test this, at least I haven't seen anything about it with my sandbox test account. If there was a way to go through the process and see how it works, it would make a lot more sense to me.
What I am doing is:
Stopping indicator animation
Activating buy and restore buttons
Showing an alert:
Waiting For Approval
Thank you! You can continue to use Altershot while your purchase is pending an approval from your parent.
I watched WWDC 14 video. Apple says you should't block UI and allow to click on buy button again. I think we need this in case parent miss alert, so child can send one more.
What I know is that we should not call following method for deferred transactions:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
The code bellow will allow you to check if the product ID you want to sell is in deferred mode. Use it to update the UI accordingly.
if ([[SKPaymentQueue defaultQueue].transactions count] > 0) {
for (SKPaymentTransaction *transaction in [SKPaymentQueue defaultQueue].transactions) {
if ([#"your.product.id" isEqualToString:transaction.payment.productIdentifier]) {
if (transaction.transactionState == SKPaymentTransactionStateDeferred) {
// update UI that you are still waiting for parent approval. You'll get "PURCHASED" if parent approved or "FAILD" if parent declined or 24 hours passed since request.
}
break;
}
}
}

Autorenewable Subscriptions won't be autorenewed

Sorry for that millionth question on autorenewable subscriptions, but i don't get it.
I've done everything as describet in Apples In-App Purchase Guidelines but it didn't solve the problem.
My problem is that i have created autorenewable subscriptions but they won't be autorenewed.
I've create a Payment Transaction Observer class, which implements the SKPaymentTransactionObserver interface. This class will be installed as a paymentObserver at Application startup in the viewDidLoad: method.
PaymentTransactionObserver *observer = [[PaymentTransactionObserver alloc] init];
[[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
In the paymenttransactionobserver i have the paymentQueue:updateTransactions method: (same as describet in Apple's documentation)
(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
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];
break;
default:
break;
}
}
When i buy a autorenewable product, the product will successfully be purchase.
But it will never be autorenewed. I thought of, that the transaction observer, somehow will get deallocated, but it won't (Otherwhise, i would be notified by the debugger). I also though, i did remove the observer but it will never be removed.
I used the debugger to ensure, that the updateTranscations: method will get called, but nothing. When i buy a test product (in sandbox-mode) with autorenewal time of one week, the method should get called after 3 minutes, but it wont.
What am i doing wrong?
Can anybody help?
Br Nic
If a subscription is autorenewed, the transaction won't pass the paymentQueue:updateTransactions method. The renew just happens on the Store. If you want to test for it you have to either:
Revalidate the receipt on your application server, if you store the receipt there.
Revalidate the receipt on ur iOS client
(http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/StoreKitGuide/VerifyingStoreReceipts/VerifyingStoreReceipts.html#//apple_ref/doc/uid/TP40008267-CH104-SW1)
In order to avoid testing for an autorenew each launch/activation you should store the endDate of the subscription period to test for a renew afterwards.
Also see:
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/StoreKitGuide/RenewableSubscriptions/RenewableSubscriptions.html#//apple_ref/doc/uid/TP40008267-CH4-SW4
However, there seems to be a bug in the sandbox. Subscriptions sometimes get renewed, sometimes not. Hard to test....
Auto-renewals only get posted to your app's purchase queue on app launch or when it cycles back from being backgrounded. Try clicking the home button and then returning to your app.

Restore already bought in-app-purchases on iPhone?

I got so far: After a reinstall, a user needs to click "buy feature", then he gets scared with the $0.99 question, then has to login and then gets told the feature is already bought and he gets it for free.
I know apple is a religion and users are strong believers, but isn't there a better way? :-) What I want is to check for the feature without actually buying it. Letting the user enter his account info seems to be neccessary, maybe buy a $0.00 feature? or is there a method somewhere that does this?
I'm using MKStoreKit for the whole In-App-Purchase, but any solution would be great.
UPDATE
thanx to darvids0n, your method solved my problem! here's some working code for others trying the same:
- (void)removePreviousPurchases { //just for sandbox testing
[[MKStoreManager sharedManager] removeAllKeychainData];
}
- (void)restorePreviousPurchases { //needs account info to be entered
if([SKPaymentQueue canMakePayments]) {
[[MKStoreManager sharedManager] restorePreviousTransactionsOnComplete:^(void) {
NSLog(#"Restored.");
/* update views, etc. */
}
onError:^(NSError *error) {
NSLog(#"Restore failed: %#", [error localizedDescription]);
/* update views, etc. */
}];
}
else
{
NSLog(#"Parental control enabled");
/* show parental control warning */
}
}
If the $0.99 item is non-consumable, then you should provide a "Restore Purchases" button (or similar) which calls
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
Assuming you've added a transaction observer already, and implemented the protocol including a case to handle a restored transaction (with state SKPaymentTransactionStateRestored) this will work.
Add these two methods :
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];

iOS In App Purchase non-consumable product determine if new purchase or restore [duplicate]

This question already has answers here:
Differentiating between initial buy and free "re-buy" in StoreKit/In-App Purchase
(3 answers)
Closed 9 years ago.
Is there a way to find out when a user buys a non-consumable, if he buys it for the first time or has bought it already and gets it again for free?
I checked transactionState, transactionDate of the transaction, but in both cases the data is the same:
transactionState: SKPaymentTransactionStatePurchased (and not SKPaymentTransactionStateRestored in case the user bought it already)
transactionDate: the date at which the transaction was added to the AppStore's payment queue.
You can check Array of transactions which will fill after restoreTransaction method and if Array of transactions is empty it means that user download this upgrade for the first time. In another case you'll check all transactions in Array and compare transaction.payment.productIdentifier with needful product identifire. If it doesn't exist add payment in transaction Array.
For Non-Consumable In-App Purchase I used following code:
#define kInAppPurchaseProUpgradeProductId #"upgradeProductId"
//...
//your payment code for all SKPaymentTransactionStates
//...
//method called when BUY button press
-(void)purchaseProUpgrade{
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
//when restore completed delegate method calls
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue{
if([[[SKPaymentQueue defaultQueue] transactions] count]==0)
[self addNewPaymentForProductId:kInAppPurchaseProUpgradeProductId];
else
for (SKPaymentTransaction *transaction in [[SKPaymentQueue defaultQueue] transactions]){
if (![transaction.payment.productIdentifier isEqualToString:kInAppPurchaseProUpgradeProductId]){
[self addNewPaymentForProductId:kInAppPurchaseProUpgradeProductId];
break;
}
}
}
-(void)addNewPaymentForProductId:(NSString *)productId{
if([SKPaymentQueue canMakePayments]){
SKPayment *payment = [SKPayment paymentWithProductIdentifier:productId];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
}
the only disadvantage is that every time you call restoreCompletedTransactions, window will pop up asking you to enter the password of the current user. This solution ensures that the buy window does not appear more than 1 time for each upgrade but all the upgrades will restore every time when you'll try to buy one of them.
Not sure but maybe you can check if transaction.originalTransacion exists or if is different.

Resources