Coding Guidelines
Connect to a service
Connect InApp client to the billing services using the Connect method. Best practice is to do this on your app start.
UM_InAppPurchaseManager.Client.Connect();
As soon as connection flow finishes, OnServiceConnected event will be fired. See the code snippet below:
//subscribing to conection finish action
UM_InAppPurchaseManager.Client.OnServiceConnected += OnBillingConnectFinishedAction;
UM_InAppPurchaseManager.Client.Connect();
private void OnBillingConnectFinishedAction (UM_BillingConnectionResult result) {
UM_InAppPurchaseManager.Client.OnServiceConnected -= OnBillingConnectFinishedAction;
if(result.isSuccess) {
Debug.Log("Connected");
} else {
Debug.Log("Failed to connect");
}
}
You can also find out the current connection state with the IsConnected property:
UM_InAppPurchaseManager.Client.IsConnected
If a connection is failed (for example, no internet connection available) you can try to call the Connect method later, but you can't use any of billing API before UM_InAppPurchaseManager is successfully initialized.
Retrieving In-App product info
After completing a Setup step, info about your products is already filled. However, after you have successfully connected to a billing service, you can get more information about your items like a localised price, for example.
List of all registred product models is available with the InAppProducts property of the UM_InAppPurchaseManager class. See the code snippet below:
UM_InAppPurchaseManager.InAppProducts;
You can also get product template using the following UM_InAppPurchaseManager methods:
static UM_InAppProduct GetProductById(string id);
static UM_InAppProduct GetProductByIOSId(string id);
static UM_InAppProduct GetProductByAndroidId(string id);
static UM_InAppProduct GetProductByAmazonId(string id);
static UM_InAppProduct GetProductByWp8Id(string id);
More information about loaded product template can be retrieved with UM_InAppProduct class.
The code snippet below shows how to print a general products info:
foreach(UM_InAppProduct product in UM_InAppPurchaseManager.InAppProducts) {
Debug.Log("Id: " + product.id);
Debug.Log("IsConsumable: " + product.IsConsumable);
Debug.Log("Title: " + product.Title);
Debug.Log("Description: " + product.Description);
Debug.Log("Price: " + product.Price);
Debug.Log("IOSId: " + product.IOSId);
Debug.Log("AndroidId: " + product.AndroidId);
Debug.Log("WP8Id: " + product.WP8Id);
}
Implementing a Purchase Flow
You purchase flow should look similar to the one below:
- Start purchase using the Purchase method
- Lock the user interface and wait for a complete event
- If a complete event is succeeded, it will be processed to a next step. Otherwise, show user and error message and finish the purchase flow.
- (Optional) Validate purchase info
- Use Consume method (only for the consumable items like coins, point, energy, etc..)
- Reward your customer for a purchase
- (Optional) Finish purchase flow
See the following code snippets as the example how to execute the steps described above.
Start purchase and subscribe to a complete event
UM_InAppPurchaseManager.Client.OnPurchaseFinished += OnPurchaseFlowFinishedAction;
UM_InAppPurchaseManager.Client.Purchase(YOUR_PRODUCT_ID);
private void OnPurchaseFlowFinishedAction (UM_PurchaseResult result) {
UM_InAppPurchaseManager.Client.OnPurchaseFinished -= OnPurchaseFlowFinishedAction;
if(result.isSuccess) {
UM_ExampleStatusBar.text = "Product " + result.product.id + " purchase Success";
} else {
UM_ExampleStatusBar.text = "Product " + result.product.id + " purchase Failed";
}
}
Consume a product
UM_InAppPurchaseManager.Client.Consume(YOUR_PRODUCT_ID);
Tips and Tricks
You may always check if a product has been already purchased with IsProductPurchased method.:
bool IsPurchased = UM_InAppPurchaseManager.Client.IsProductPurchased(YOUR_PRODUCT_ID);
The Ultimate Mobile combines API for a lot of the different platforms. We created a wrapper that allows you to implement InApps API only once, and it will work on all supported platforms.
However, you should know about the differences and possibilities on other platforms.