巫妖王
巫妖王

Reputation: 435

What if iOS purchase receipt verification failed?

The iOS purchase receipt server verification usually works like this:

  1. User purchase on iPhone

  2. Your app sent the purchase recipt to server

  3. Server receive the recipt and send it to Apple to verify

  4. Server gets the verify results from Apple .

  5. Server sents the verify result to app

BUT what if only Step 1 is successful? For example, The app can't send request to the server in Step 2 or app can't get response from server in Step 5. The problem is user already paid. What is the best way to handle this problem?

Upvotes: 10

Views: 5478

Answers (1)

Sia
Sia

Reputation: 111

If you are using SKPaymentQueue, then it's easy. All you have to do is to keep the transaction in SKPaymentQueue until 'step 5' when you get a success/failure verify result from your server.

If anything goes wrong in between step 1 to 5 your app still has access to the transaction in the SKPaymentQueue and can 'reprocess' it.
The reprocessing of incomplete transactions could kick in at start of your app (or some time interval as you prefer).
Just check SKPaymentQueue to get the pending/incomplete transactions and send them to your server (just like 'step 2'). If your server is still not accessible obviously you won't get to step 5, therefore you don't remove the transaction from the queue and this reprocessing happens again and again every time at the next app start (or next queue check time interval) until is fulfilled.

Implementation

The implementation is also easy, you need to have a 'transaction observer class' of SKPaymentTransactionObserver.
At app start create an instance of the 'transaction observer class' and that should register itself by call to:

[[SKPaymentQueue defaultQueue] addTransactionObserver:self]

Then 'transaction observer class' gets the transactions in method:

(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

This method is where you can handle and reprocess all incomplete transactions.

Note that your server must be idempotent (i.e able to handle repeated transactions if they already been processed)
Once the server processes and completes steps 2 to 4, then it comes to app with success/failure result and that's the only time when you want to remove that transaction from the queue by call to:

[[SKPaymentQueue defaultQueue] finishTransaction: transaction]

And finally give your user the premium feature they purchased at this point.

Upvotes: 7

Related Questions