且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果iOS购买收据验证失败怎么办?

更新时间:2023-02-03 09:25:49

如果您使用的是 SKPaymentQueue ,那么这很容易.您要做的就是在服务器上成功/失败验证结果后,在SKPaymentQueue中保持保持交易,直到"第5步".

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.

如果在第1步到第5步中发生任何问题,您的应用仍然可以访问SKPaymentQueue中的交易,并且可以对其进行"重新处理".
未完成交易的重新处理可以在应用程序的开始(或您希望的某个时间间隔)内开始.
只需检查SKPaymentQueue即可获得待处理/未完成交易并将它们发送到您的服务器(就像"第2步"一样​​).如果您的服务器仍然无法访问,显然您将无法执行第5步,因此您不会从队列中删除事务,并且每次下一次启动应用程序时,都会再次再次进行此重新处理(或下一个队列检查时间间隔)直到已实现.

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.

实现也很容易,您需要具有 SKPaymentTransactionObserver 的'交易观察器类'.
在应用启动时,创建事务观察器类"的实例,该实例应通过调用以下内容进行注册:

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.

请注意,您的服务器必须是幂等(即如果已处理重复交易,则能够处理重复交易)
服务器处理并完成第2步至第4步后,它就会出现具有成功/失败结果的应用程序,这是您要删除的唯一时间strong>通过调用以下命令从队列中进行交易:

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.