Greetings Unity Developers and other IAP aficionados,
I am making this post to attempt to clarify any confusions pertaining to deferred purchases in Unity IAP.
I am working with Unity IAP v4.1.4 and only use Google Play (Android).
My scenario and question: (scroll to bottom for TLDR)
In my testing of consumables and deferred payments, I have deferred purchases working on my local device using the ‘slow test card’ process, I have SetDeferredPurchaseListener() and everything else required to get it to work resulting in ProcessPurchase spitting out a PurchaseEvent (receipt) POST payment received.
I also have ‘instant’ purchases, non-consumables, and subscriptions seemingly working as well.
While debugging the deferred purchase process, as I just mentioned, I noticed ProcessPurchase is not called until the payment is received (in testing, this would be when Google Play is notified the payment has been received).
Upon noticing this, I dug a little deeper and noticed I am only returning PurchaseProcesssingResult.Complete, but in the most recent sample Unity documentation and code, it is suggested that you return PurchaseProcessingResult.Pending for deferred purchases and how you go about doing this is by parsing the receipt given to you from ProcessPurchase, find the purchase state, and set the PurchaseProcessingResult based on the number given. ie 0 for unknown, 1 for success, 2 for pending, and possibly 4 for pending as well.
Reference: Google Play IAP Problem
I added this to my code and did more testing:
ProcessPurchase isn’t called until the purchase is complete or paid for by the user (I’ve only ever dared to test this with a test card, never on a live/real application). The receipt given was always returning a 0 purchase state, only after the payment was processed, but there was never a chance for my ProcessPurchase code to return a Pending processing result because by the time it hits that code, the payment is received and thus is complete.
I thought this was weird, so I sought out answers only to be more confused. I went and found the Unity IAP sample project which is supposed to be used for code samples for our own projects.
Last posted here on Feb 2022: Sample IAP Project
The developer of the sample project also incorporated the Pending processing result as part of the ProcessPurchase method, but upon doing the same deferred purchase testing as above, I was left with the same result: the pending processing result is never reached in the code, only the complete result is ever returned. Upon further inspection, I noticed that the code sample given for checking for pending or complete purchases, doesn’t actually check any purchase states or receipts, it’s flipped off of a seemingly arbitrary boolean which doesn’t appear to be tied to anything anymore, likely some depreciated code.
The Unity sample code given for ProcessPurchase:
The Unity sample code given which looks like it was tied to a button event at one point, but now is tied to nothing and thus since this bool can’t be ‘toggled’, the code returning the Pending result is never reached:
So after all of this, my question is (TL;DR):
In testing of both my own and the Unity sample project given, ProcessPurchase method isn’t called until the payment is completed/paid regardless of whether the purchase is deferred or not.
So is there any need to even separate the logic for complete and pending anymore?
Should I expect this process to somehow change when running in a live production environment?
ie ProcessPurchase is executed after a deferred purchase to trigger a pending result as opposed to only being triggered after, like my testing.
I currently have my code reading receipts and if the purchase state is a 2 or 4, it will return pending result, otherwise it will return a complete result, but as stated, the pending state is never reached.
Is this the proper implementation for production?
Thank you for reading and for your consideration,
Pleebie Jeebies