Unity In-app billing

Setup

Download Unity iab sample here.

To run the plugin, download the package and add it to your project. Then make the two following changes to your AndroidManifest.xml file:

1. Permissions to connect to the internet and Myket Billing:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="ir.mservices.market.BILLING" />

2. Add this activity to the Application section:

<meta-data android:name="billing.service" android:value="myket.MyketIabService"/>
<activity android:name="com.myket.MyketIABProxyActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />

Notice that you do not need to add any Prefab or Script to your project. Plugin automatically creates an entity called MyketIABPlugin and that creates MyketPlugin.IABEventManager, which is quite essential and shall not be removed or renamed throughout the project.

Methods

public static void init(string publicKey)

Before making any use of the library’s method, you need to call this method once. Your app PublicKey (RSA) which you should receive from Myket developers’ panel, is in fact your entry for this method. If the event runs successfully then the billingSupportedEvent will be called and if not, the billingNotSupportedEvent is called. Please notice, after the init method is called your app will try to connect to Myket’s in-app purchase service, so you need to have the latest version of Myket installed on your device.

public static void enableLogging(bool shouldEnable)

This method enables file logs. Notice that the value of shouldEnable in the state of Release should equal False.

public static void unbindService()

Once you are done with the in-app purchase service, call the following method.

public static void queryInventory(string[] skus)

To receive information about the product you’ve formerly defined in in-app purchase panel, such as price, title etc. you should call this method. A list of the products which have not been consumed yet, will also be called by this method. This method will ask for a list of your products’ IDs.

To prevent any troubles regarding the in-app purchase you need to call this method in the beginning of your app. If your product is consumable yet you received a product which was not consumed, consume the product and deliver it to your user. For non-consumable products you need to call this method every time you open your program to realize whether any of your products have been bought.

public static void querySkuDetails(string[] skus)

To receive information about the products you’ve formerly defined in Myket developer panel, such as price, title etc. you should call this method.

public static void purchaseProduct(string sku)public static void purchaseProduct(string sku, string developerPayload)

To begin the purchasing process of a product called sku you need to call this method. After you try to purchase a product, two events will be called. First, purchaseSucceededEvent will be called when the purchase is successful or when you try to repurchase the product you have not consumed. Now if the purchase is not successful, purchaseFailedEvent will be called.

public static void consumeProduct(string sku)
public static void consumeProducts(string[] skus)

To consume your sku, you should use consumeProduct method

Events

Events are accessible through MyketIABEventManager class.

public static event Action billingSupportedEvent;

This event will be called when the device supports in-app purchase.

public static event Action<string> billingNotSupportedEvent;

If in-app purchase is not possible after the init method call, then this event will be called.

public static event Action<List<MyketPurchase>, List<MyketSkuInfo>> queryInventorySucceededEvent;

If the call for queryInventory methods successful, then this event will be called.

public static event Action<string> queryInventoryFailedEvent;

If the process to recover products and purchases data fails, then this event will be called.

public static event Action<MyketPurchase> purchaseSucceededEvent;

This event will be called when the purchase is successful.

public static event Action<string> purchaseFailedEvent;

This event will be called when the purchase is not successful.

public static event Action<MyketPurchase> consumePurchaseSucceededEvent;

If you successfully consume an already-purchased product, then this event will be called.

public static event Action<string> consumePurchaseFailedEvent;

If you fail to consume an already-purchased product, then this event will be called.

Was this article helpful?
Dislike 0
Previous: Check Version Update service
Next: Cordova and PhoneGap In-App Purchases