Development Steps - Aptoide/bds-unity-plugin GitHub Wiki
The developer needs to import the package Appcoins.Purchasing to gain access to the API.To do that the developer should use “using Appcoins.Purchasing” in the import section of the script file.
Appcoins.Purchasing package contains a number of class files and Interfaces that can be use to make a transaction.
In the next few sections the classes and methods used are described.
The purchaser class is the core of the whole system and can be tweaked and customised to suit any needs of the developer. One can register products and be notified of important events like initialization and purchasing success and failure. It's also the class used to actually go through with a purchase.
| Modifier and Type | Field and Description |
|---|---|
UnityEvent |
onInitializeSuccess This event handles the Initialization of the plugin in case of success. |
UnityEvent |
onInitializeFailed This event handles the Initialization of the plugin in case of failure. |
PurchaseEvent |
onPurchaseSuccess This event handles the purchase of an item (Consumable / Non-consumable) in case of success. The function that handles this event should receive as argument an AppcoinsProduct object. |
PurchaseEvent |
onPurchaseFailed This event handles the purchase of an item (Consumable / Non-consumable) in case of failure. The function that handles this event should receive as argument an AppcoinsProduct object. |
| Modifier and Type | Field and Description |
|---|---|
void |
AddProduct(string skuID, AppcoinsProductType type) Method that registers a product on the Purchaser by skuID(as defined and created on your app's page in the portal) and type.This is where you can define how your product will behave when bought by the user. If type is Consumable the product will be consumed from the user's inventory right after the purchase. If type is NonConsumable the product will remain forever unconsumed on the inventory. |
void |
InitializePurchasing() Method that handles Purchaser internal initialisation after its setup is done. |
void |
BuyProductID(string skuID) Method that buys the product , given a skuID. |
To be notified of the purchaser events the developer needs to AddListerners to them like shown on the example below. This shows a standard way of setting up the purchaser for usage.
private Purchaser _purchaser;
void SetupPurchaser() {
_purchaser.onInitializeSuccess.AddListener(OnInitializeSuccess);
_purchaser.onPurchaseSuccess.AddListener(OnPurchaseSuccess);
_purchaser.AddProduct(kProductIDConsumable, AppcoinsProductType.Consumable);
_purchaser.AddProduct(kProductIDNonConsumable, AppcoinsProductType.NonConsumable);
_purchaser.InitializePurchasing();
}
public void BuyGas() {
_purchaser.BuyProductID(kProductIDConsumable);
}
public void OnInitializeSuccess() { //Code ran after initialization }
public void OnPurchaseSuccess(AppcoinsProduct product) { }
The AppcoinsPurchasing class allows for complementary interaction with the purchasing system and gives a finer control over the process. You can add a custom payload validator for an extra layer of security or fetch the value in APPC of a product.
| Modifier and Type | Field and Description |
|---|---|
void |
SetupCustomValidator(IPayloadValidator customValidator) Method that sets up the validation of the payload.The object customValidator has to implement the interface IPayloadValidator. |
string |
GetAPPCPriceStringForSKU(string skuID) Returns the product price, in the format “X APPC” for the given skuID. |
Bellow is an example of how to use both of these methods.
private AppcoinsPurchasing _appcoins;
public class Logic : MonoBehaviour, IPayloadValidator {
public static string kProductIDConsumable = "gas";
void SetupPurchaser() {
_purchaser.onInitializeSuccess.AddListener(OnInitializeSuccess);
_purchaser.onPurchaseSuccess.AddListener(OnPurchaseSuccess);
_purchaser.AddProduct(kProductIDConsumable, AppcoinsProductType.Consumable);
_purchaser.AddProduct(kProductIDNonConsumable, AppcoinsProductType.NonConsumable);
_purchaser.InitializePurchasing();
_appcoins.SetupCustomValidator(this);
}
private void OnInitializeSuccess() {
//Can fetch the price after intialization is successful
_priceTxt.text = _appcoins.GetAPPCPriceStringForSKU("gas");
}
public bool IsValidPayload(string payload) {
Debug.Log("Custom payload validation in place! Payload is " + payload);
return true;
}
}
| Modifier and Type | Field and Description |
|---|---|
string |
skuID The SKU id of the product. |
string |
hasReceipt The transaction has a Receipt |
string |
receipt The receipt of the transaction. |
string |
transactionID The transactionID. |
| Modifier and Type | Field and Description |
|---|---|
AppcoinsProductType |
CONSUMABLE Represents the value of a consumable product. |
AppcoinsProductType |
NON_CONSUMABLE Represents the value of a non-consumable product. |
| Modifier and Type | Field and Description |
|---|---|
bool |
IsValidPayload(string payload) Method that needs to be implemented,by using this interface, and defines an extra security layer by having your own custom validation of the payload of a transaction (for instance with server side confirmation). |
When processing a purchase there can be 2 statuses:
| Transaction State | Description |
|---|---|
| Complete | The purchase is complete with no errors. |
| Pending | The purchase is still pending and waiting to be processed. |
Errors thrown in the plugin initialization phase.
| Error Message | Description |
|---|---|
| Purchasing Unavailable | The purchasing is not available. |
| No Products Available | There are no products available. |
| App Not Known | The package name of the APK is different from what is registered in BDS. |
| Wallet Not Installed | The BDS wallet application isn’t installed. |
| Network Not Available | There is not an available connection to the internet. |
Errors thrown in the Purchasing phase.
| Error Message | Description |
|---|---|
| Purchasing Unavailable | The purchasing is not available. |
| Existing Purchase Pending | There is a Purchase over the product that is pending. |
| Product Unavailable | The product is not available. |
| Signature Invalid | The signature of the purchase is invalid. |
| User Cancelled | The user cancelled the Purchase. |
| Payment Declined | The payment of the Purchase was declined. |
| Duplicate Transaction | The transaction already exists. |
| Unknown | The purchasing is not known. |