Search code examples
unity-game-enginein-app-purchasein-app-billingin-app

How to find In App Purchases was successfull (Native Plugin Use)


I am using Android Native Plugin in my Unity game to buy coins. How I will know in my Unity script that this purchase is successful?

newScriptAndroidInApp.loadStore(); AndroidInAppPurchaseManager.instance.purchase(GPaymnetManagerExample.coins_2000);

This is how I call the InApp purchase and don’t know what to do after that. Is there is a return bool or anything like that because if the purchase is successful I have to add that number of coins in current coins as well.

That was the native plugin link:

https://www.assetstore.unity3d.com/en/#!/content/10825


Solution

  • According to the documentation https://docs.google.com/document/d/1px0BVXcZqgrW99OQV4bM7q1rD5NYaCYTxXnn4eS4ytY purchase function triggers ON_PRODUCT_PURCHASED event, which you should listen to. Data provided with this event should be treated as BillingResult, which has bool isSuccess field with purchase status. Advanced error code is accessible via int response field (search for BillingResponseCodes class in docs). So the workflow will look like following:

    AndroidInAppPurchaseManager.instance.addEventListener(AndroidInAppPurchaseManager.ON_PRODUCT_PURCHASED, OnProductPurchased);
    
    ...
    
    private void OnProductPurchased(CEvent e) {
        BillingResult result = e.data as BillingResult;
        if(result.isSuccess) { 
            Debug.Log("Purchase successful");
            ...
        } else {
            Debug.Log("Purchase failed with code: " + result.response);
            ...
        }
    }
    

    Note that I don't own this plugin so the code might not compile, but the general idea should be the same.