B4A In-App-Purchase

Anywhere software Company has developed a platform called Basic4Android to streamline applications running on the Android operating system. This platform uses the Basic programming language. Anywhere software has provided a module for users to connect to the Google Play In-App purchase service. Given that the Myket In-App Purchase API is exactly like Google Play In-App purchase service, we’ve made this module moderated with little change, to connect to Myket. In the following section of the documentation, you will learn how to add this module to your application. 

 In-App Purchase Definition in B4A 

The B4A In-App purchase module in Myket supports consumable, non-consumable and subscription product type. You can read about in-App purchase concepts here. There is no difference between definitions of in-App Purchase on B4A with Google Play In-App Purchase. Steps to Adding Myket Module To add Myket In-App Purchase service library on the B4A platform, you need to do below steps: 

 1- In the first step, download the Myket In-App Purchase library at B4A from here.

 2- Copy both of MyketIAP.jar and MyketIAP.xml files to the Basic4Android libraries folder.

 3- In the right panel of the B4A, select the libs tab and refresh to display the MyketIAP library.

 4- Use the Manifest Editor (located in the Project menu) to add the below code to your application’s Manifest:

AddPermission(ir.mservices.market.BILLING)

Introducing the API in B4A 

BillingManager3 class 

BillingManager3 connects B4A to Myket In-App Purchase service. To use this class, it’s enough to define this class in Process_Globals of your application:

Dim oBillingManager As BillingManager3


BillingManager3 is used to get information about purchased products, purchasing a product (General or subscription) and consuming it. In the following, we will introduce you to how to use the BillingManager3 methods:

Initialize method 

To use BillingManager3, you must first initialize this class:

oBillingManager.Initialize("BillingManager", publicKey)

The first parameter is EventName, this parameter specifies the beginning of the Callbacks name of BillingManager3. In this documentary, it is assumed that the EventName parameter has been set to “BillingManager”.


 The second parameter is your product PublicKey. Note that you receive PublicKey after adding your product or products to the developer’s panel. To learn how to define products and receive PublicKey from the developer panel, visit here. 

Checking Support for Myket Purchase service 

This callback is called upon the completion of the Initialize method to verify that the user device supports the Myket In-app purchase or not. If the Supported value is true, the user device supports Myket In-App Purchase service.

Sub BillingManager_BillingSupported(Supported As Boolean, Message As String)

RequestPayment Method RequestPayment is used to purchase a product (General or subscription):

oBillingManager.RequestPayment("gas", "inapp", "DeveloperPayload")

The first parameter is the name of the product that you entered in the developer panel. 
The second parameter specifies the type of product that accepts “inapp” value for General products and “subs” value for subscription products. The third parameter is additional information or Developer Payload. 
This field contains a string that is optional and does not affect on Myket processing. The value assigned to this parameter will be returned to Callback at the end of the purchase (described below). To learn more about the concept of Developer Payload, see here.

 Completion of the purchase process 

This Callback is called after the RequestPayment method is completed and it specifies the status of the Product. If Success is true, the purchase has been successful. If the product was Consumable and Purchasable, it should consume this product by using the ConsumeProduct method on the Myket server. The ConsumeProduct method should not be called if the product was non-consumable or subscription.

Sub BillingManager_PurchaseCompleted(Success As Boolean, Product As Purchase)

· ConsumeProduct method For consumable products, you should be sure to call the ConsumeProduct method after purchase until the product can be consumed on the Myket server. To do this is enough:

oBillingManager.ConsumeProduct(Product)

This method takes the product Object that to be consumed in its only input parameter. (This object has been submitted in a completed purchase Callback.) Process Completion of Consuming the product This callback is called after the process completion of ConsumeProduct method, and if the Success parameter is true, the product must be delivered to the user.

Sub BillingManager_ProductConsumed(Success As Boolean, Product As Purchase)

GetOwnedProducts method

 This method is called at the beginning of the application after the initialization of the BillingManager. The purpose of this task is to receive products that are not consumed in Myket server and subscription products. After completing the task, this method returns a list of products in the callback product received by the user. For example, when a user purchases a Premium Account in your application, this product is always returned in Callback. To call this method is enough:

oBillingManager.GetOwnedProducts()

Get user products 

This callback is called after the GetOwnedProducts Call. Purchase lists include user non-consumable products and consumable products that are not consumed on Myket server.

Sub BillingManager_OwnedProducts(Success As Boolean, purchases As Map)

Enable and Disable Log 

To enable or disable Myket In-App Purchase library, just give True / False value to the DebugLogging method: oBillingManager.DebugLogging = True *It is recommended to enable DebugLogging during development, and disable it after publishing your application. Test project This project is the same as the Google Trivial Drive project, which has been changed.

 You can download this project here or visit GitHub. There are three types of products in the project: Consumable product (gas) Non-Consumable product (premium) Subscription product (infinity) 

 *Note that to get started with the products, you must first define them in the developer’s panel. The project includes four buttons for buying gas, using gas, buying a premium account and get infinite gasoline that Described briefly below:

· Buy Gas Gasoline in this example is in the category of consumable product and purchasable product. By pressing this button if the gas tank is not full, the app will be connected to the Myket In-App purchase, and after the payment, 10 units will be added to the gasoline tank. · Use Gas If the amount of gasoline is more than 10 units, then 10 units of gasoline will be consumed. 
 · Buy Premium The premium account in this example is in the category of purchasable product and non-consumable product. By pressing this button, you will be connected to Myket In-App Purchase and will be purchased a premium account. This type of payment is non-consumable. · Get Infinite Gas This account is time-consuming, meaning that the user can buy a monthly, weekly, or yearly account. 

Note that the user can use an unlimited amount of gas when purchasing an infinite gas account. Obviously, during this time, the user should not buy gasoline, or cannot buy another timely account, therefore the Get infinite Gas button will be hidden and the Buy Gas button also disabled.

Was this article helpful?
Dislike 0
Previous: Open Application Page in Myket
Next: Check Version Update service