Usage
Billing Services allows to monetize your app on iOS and Android platforms
Once after you setup and add billing products, you can purchase a product by referring them in code.
Before using any of the billing services features, first make sure if it's available on the running platform with IsAvailable method. This returns true if you are allowed to make purchases on the current platform.
Events
Once you see that billing services are available, you should register for the billing services events.
Events are triggered based on an action initiated by the plugin's api calls, asynchronously. For ex: Calling InitializeStore triggers OnInitializeStoreComplete with the product details if it's successful.
Get Billing Product Details
After event registration, you need to call InitializeStore to fetch the complete details of the in-app purchase billing products.
This call fetches the details(you set in iTunes connect and google play) and triggers BillingServices.OnInitializeStoreComplete callback with the details.
Optionally, you can pass your own BillingProducts created at runtime to InitializeStore call. This will be handy if you have an API to fetch platform id's runtime.
The platform product id's you set in Essential Kit Settings will be used internally by the plugin to fetch the complete details of the product on current platform.
Making a purchase
Once you have the details about pricing and description of a product, you can check if your device is ready to make payments. CanMakePayments returns true when its fine to make purchases. If purchases are not currently allowed, it returns false.
Consumable products can be purchased multiple time where as Non-Consumable products can be bought only once. To unlock the content purchased with non-consumable product you can check if its already bought or not with IsProductPurchased
If the product is not purchased or if its a consumable product you can proceed with the purchase by calling BuyProduct. BuyProduct takes the BillingProduct instance you got from OnInitializeStoreComplete event.
BuyProduct shows native purchase dialogs to proceed with the purchase and fires BillingServices.OnTransactionStateChange event callback during the purchase process.
Once you BillingServices.OnTransactionStateChange event fired, you will get a list of transactions that are currently getting processed. You can check if the transaction state is BillingTransactionState.Purchased or BillingTransactionState.Failed.
If its Purchased state, you can proceed with unlocking the content to the user. If Failed, It could be because the user cancelled the payment or due a failed transaction. You can get more details from Error property of a transaction.
Get previous purchases (Restore Purchases)
There are scenarios where
The user uninstalls your game and re-installs later
The user installs the game on multiple devices
As Non-Consumable product purchases need to be maintained across devices and installs, you need a way to fetch the old purchases of the user so that you can unlock the content for the user.
This can be achieved through RestorePurchases call.
Calling RestorePurchases fetches the old purchase transactions linked to the user account and fires BillingServices.OnRestorePurchasesComplete event with all purchase details.
Once you get the old purchase transactions, you can unlock the content for the user.
On iOS, it's required to have an explicit button to restore purchases as per Apple guidelines.
It's always good to call Restore Purchases once you are done with InitializeStore call as the user will be available with unlocked content if he/she has purchased any earlier.
Manually handling transactions (Auto Finish Transactions disabled)
There are scenarios where you want to validate a purchase receipt on your server and unlock the content to the user. If you have such requirement, you need to disable "Auto Finish Transactions" in Billing Services settings and close the transactions manually.
GetTransactions gives the list of all pending transactions and you need to
Process each transaction returned from GetTransactions
Pass the transaction receipt details to your backend server
Set ReceiptVerificationState of the transaction to BillingReceiptVerificationState.Success or BillingReceiptVerificationState.Failed
Call FinishTransactions with the updated transaction verification details
If you have "Auto Finish Transactions" enabled in Essential Kit Settings -> Billing Services, you don't need to call GetTransactions as we handle this automatically.
Platform Specific (Advanced)
Android
Have a look at the below table for the BillingTransaction to Google Play purchase response mapping.