Check when buying an application

I am creating an iPhone application that should process subscriptions through an in app purchase. When I receive a receipt from Apple in my application, I want to save the receipt on my own server and on my server, I also want to check receipt with the Apple server. It is very important that this connection with my server is made, as this saves the user information that will be needed later.

In my SKPaymentTransactionObserver, now I'm trying to call my server on SKPaymentTransactionStatePurchased. The problem is that on SKPaymentTransactionStatePurchased there seems to be a standard β€œThank you” warning and that the purchase is complete. Since the application must contact my server, this standard warning is displayed before the call to my server is completed, and what happens if the user leaves the application here, he thinks that the purchase is completed as she said? When should I contact my server? And if I want to show a warning to the user when my server call is completed, when should I do it?

This is the code I'm using:

//This is called on "SKPaymentTransactionStatePurchased" - (void) completeTransaction: (SKPaymentTransaction *)transaction { [self saveAndVerifyOnServer:transaction]; [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; } 
+2
source share
2 answers

This is what the documentation for SKPaymentQueue about the finishTransaction method:

Your application should call finishTransaction: only after it successfully processed the transaction and unlocked the functionality acquired by the user.

From this, I think you need to save and verify the transaction on your server, and only when this confirmed call calls the finishTransaction method. As the dean notes, you will need to solve a situation where the user shuts down before you can verify.

The good news is that SKPaymentQueue saved in all sessions, so it remembers that you have not finished buying something. This means that the next time you add a transaction observer, you can immediately call your delegate. You may need to encode the server process so that it can restart the transaction.

+2
source

You can immediately save the purchase in the application (only in NSUserDefaults) until you can successfully talk to your server.

When your server returns to you, remove the key from NSUserDefaults.

If the application shuts down before the server returns to you, the key will still be in NSUserDefaults the next time it starts, and you can send the purchase then.

Sam

PS Remember to call [[NSUserDefaults standardUserDefaults] synchronize] to make sure your key is written!

PPS You will need to somehow protect against several purchases. Perhaps with some unique key created by the application.

0
source

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


All Articles