Packagecom.myflashlab.air.extensions.billing
Classpublic class Billing
InheritanceBilling Inheritance Object

In-app-payments ANE is making the Android and iOS in-app-billing work flows similar to each other so AIR developers won't be confused. While making these two completely-different-APIs identical, we made sure that you will have access to all their powers so you are not missing anything.

To learn more about the usage of this ANE, please read the examples in this documentation.

NOTICE: If you have a permanent product in your app, you MUST have a restore button in your app UI or your app will be rejected in the apple's review process so make sure to add that button and when it's clicked, you should call Billing.getPurchases() method to get the list of previously purchased items

See also

getPurchases()


Public Properties
 PropertyDefined By
  CHILD_DIRECTED : int = 0
[static] (Android) Developers are able to specify whether you would like your app to be treated as child-directed for purposes of the Children’s Online Privacy Protection Act (COPPA) By setting this method, you certify that this notification is accurate and you are authorized to act on behalf of the owner of the app.
Billing
  iOSReceipt : String
[static] [read-only] (iOS), Returns a base64 encoded string which you can send to your server and eventually to app store for verification.
Billing
  isInitialized : Boolean
[static] [read-only] Returns true if the ANE is initialized successfully
Billing
  listener : EventDispatcher
[static] [read-only] (iOS) This property is used for app store promotions for iOS devices 11+.
Billing
  PARENTAL_GATE : Boolean = false
[static] (iOS) If you are targeting your app for kids, apple requires you to show a parental gate before a purchase flow starts.
Billing
  products : Vector.<Product>
[static] [read-only] Returns a list of confirmed products found on iTunesConnect or GooglePlay console.
Billing
  publicKey : String
[static] [write-only] (Android), sets the public Android key for your app found in your GooglePlay console.
Billing
  UNDER_AGE_OF_CONSENT : int = 0
[static] Developers are able to specify whether to mark your ad requests to receive treatment for users in the European Economic Area (EEA) under the age of consent.
Billing
Public Methods
 MethodDefined By
  
acknowledgePurchase($purchaseToken:String, $callback:Function):void
[static] (Android), Acknowledge in-app purchases.
Billing
  
clearCache():void
[static] Deprecated, the ANE will clear the cache when required internally and you no longer have to worry about it.
Billing
  
[static] (iOS), you can set Billing.PARENTAL_GATE = true if your app targets kids and promo purchases will be prevented from automatic continuation.
Billing
  
dispose():void
[static] You normally won't need to dispose this ANE when initialized!
Billing
  
doPayment($type:int, $id:String, $accountId:String, $callback:Function):void
[static] Call this method to let users make payments.
Billing
  
forceConsume($purchaseToken:String, $callback:Function):void
[static] (Android), Force consume a managed purchased product.
Billing
  
getPurchases($callback:Function):void
[static] Call this method to get a Vector list of all products that user has purchased before.
Billing
  
init($androidInAppIDs:Array, $androidSubsIDs:Array, $iOSInAppIDs:Array, $iOSSubsIDs:Array, $callback:Function):void
[static] Call this method to initialize the Billing ANE.
Billing
  
isFeatureSupported($featureType:String):int
[static] (Android), check if the named featureType is supported or not.
Billing
  
priceChangeConfirmation($productId:String, $callback:Function):void
[static] (Android), Initiate a flow to confirm the change of price for an item subscribed by the user.
Billing
  
redeem():void
[static] (Android), You can create promotional codes and your users can redeem those codes to get your in-app-products for free! To do that, call this method and it will open the redeem window in GooglePlay app.
Billing
  
replaceSubscription($oldProductId:String, $newProductId:String, $prorationMode:int, $accountId:String, $callback:Function):void
[static] (Android), Use this method to let users downgrade/upgrade their exciting subscription.
Billing
  
[static] (Android), like the iOS side, for security reasons you are recommended to verify the purchases using your own server.
Billing
Public Constants
 ConstantDefined By
  ALREADY_OWNED_ITEM : String = This item is already purchased, you should let user use this item.
[static]
Billing
  ANDROID_LIBRARY_VERSION : String = 2.0.3
[static]
Billing
  EXTENSION_ID : String = com.myflashlab.air.extensions.billing
[static]
Billing
  NOT_FOUND_ITEM : String = This item not found on Google or Apple servers.
[static]
Billing
  VERSION : String = 4.0.0
[static]
Billing
Property Detail
CHILD_DIRECTEDproperty
public static var CHILD_DIRECTED:int = 0

(Android) Developers are able to specify whether you would like your app to be treated as child-directed for purposes of the Children’s Online Privacy Protection Act (COPPA)

By setting this method, you certify that this notification is accurate and you are authorized to act on behalf of the owner of the app. You understand that abuse of this setting may result in termination of your Google account.

Note: it may take some time for this designation to be fully implemented in applicable Google services.

See also

iOSReceiptproperty 
iOSReceipt:String  [read-only]

(iOS), Returns a base64 encoded string which you can send to your server and eventually to app store for verification. To learn about how to verify the purchases, read here: https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html#//apple_ref/doc/uid/TP40010573-CH104-SW1


Implementation
    public static function get iOSReceipt():String
isInitializedproperty 
isInitialized:Boolean  [read-only]

Returns true if the ANE is initialized successfully


Implementation
    public static function get isInitialized():Boolean
listenerproperty 
listener:EventDispatcher  [read-only]

(iOS) This property is used for app store promotions for iOS devices 11+. You need to listen to PROMO_PURCHASE_FAILED and PROMO_PURCHASE_SUCCESS right after you initialized the ANE with Billing.init


Implementation
    public static function get listener():EventDispatcher

Example
The following example shows how you can listen to promo purchase events that might happen on iOS 11+
        Billing.listener.addEventListener(BillingEvent.PROMO_PURCHASE_FAILED, onIosPromoPurchaseFailed);
        Billing.listener.addEventListener(BillingEvent.PROMO_PURCHASE_SUCCESS, onIosPromoPurchaseSuccess);
         
        function onIosPromoPurchaseFailed(e:BillingEvent):void
        {
         trace("onPromoPurchaseFailed: " + e.msg);
     }
         
        function onIosPromoPurchaseSuccess(e:BillingEvent):void
        {
         trace("onPromoPurchase status: " + e.status);
         trace("onPromoPurchase msg: " + e.msg);
          
         if (e.purchase)
         {
             trace("----------------");
             // we cannot determine the "billingType" on promo purchases!
             // It's your job to name your productId in a way so you will know this.
             trace("$data.orderId = " +                 e.purchase.orderId);
             trace("$data.productId = " +            e.purchase.productId);
             trace("$data.purchaseState = " +        e.purchase.purchaseState);
             trace("$data.purchaseTime = " +            e.purchase.purchaseTime);
             trace("$data.purchaseToken = " +        e.purchase.purchaseToken);
             trace("----------------");
         }
     }
     
PARENTAL_GATEproperty 
public static var PARENTAL_GATE:Boolean = false

(iOS) If you are targeting your app for kids, apple requires you to show a parental gate before a purchase flow starts. The default value is false but set this to true and when automatic payment flow starts, the event BillingEvent.PARENT_PERMISSION_REQUIRED will be dispatched.

When the event is dispatched, you must show some parental-gate in your app. read here for more information: https://developer.apple.com/app-store/parental-gates/

When you are sure that a parent is using the app, you should simply call the method Billing.continueThePreventedPurchaseFlow(); to continue the halted purchase flow.

See also

productsproperty 
products:Vector.<Product>  [read-only]

Returns a list of confirmed products found on iTunesConnect or GooglePlay console.


Implementation
    public static function get products():Vector.<Product>
publicKeyproperty 
publicKey:String  [write-only]

(Android), sets the public Android key for your app found in your GooglePlay console.

Set this property only if you want to verify purchases on device!


Implementation
    public static function set publicKey(value:String):void

See also

UNDER_AGE_OF_CONSENTproperty 
public static var UNDER_AGE_OF_CONSENT:int = 0

Developers are able to specify whether to mark your ad requests to receive treatment for users in the European Economic Area (EEA) under the age of consent.

Method Detail
acknowledgePurchase()method
public static function acknowledgePurchase($purchaseToken:String, $callback:Function):void

(Android), Acknowledge in-app purchases. Developers are required to acknowledge that they have granted entitlement for all in-app purchases for their application.

Warning! All purchases require acknowledgement. Failure to acknowledge a purchase will result in that purchase being refunded. one-time (consumable) products automatically uses the new Android 'consumeAsync' method which acts as an implicit acknowledgement. For subscriptions and permanent purchase use this method.

Please refer to https://developer.android.com/google/play/billing/billing_library_overview#acknowledge for more details.

Parameters

$purchaseToken:String
 
$callback:Function


Example
the $callback function must be constructed like below
     function($error:String):void
     {
         //$error: would be available only if an error has occured. Otherwise the acknowledgement process has been done successfully.
     }
     
clearCache()method 
public static function clearCache():void

Deprecated, the ANE will clear the cache when required internally and you no longer have to worry about it. This method will be removed in future versions of the ANE.

continueThePreventedPurchaseFlow()method 
public static function continueThePreventedPurchaseFlow():void

(iOS), you can set Billing.PARENTAL_GATE = true if your app targets kids and promo purchases will be prevented from automatic continuation. This method will continue the purchase flow after it's being halted.

dispose()method 
public static function dispose():void

You normally won't need to dispose this ANE when initialized!

See also

doPayment()method 
public static function doPayment($type:int, $id:String, $accountId:String, $callback:Function):void

Call this method to let users make payments. You should set a $callback function to know the result of the purchase.

Parameters

$type:int — Specify if this is a consumable, permanent or a subscription. BillingType.PERMANENT , BillingType.CONSUMABLE or BillingType.AUTO_RENEWAL
 
$id:String — Specify the product id which you have already created in GooglePlay or iTunesConnect.
 
$accountId:String — Specify an optional obfuscated string that is uniquely associated with the user's account in your app. If you pass this value, Google Play can use it to detect irregular activity, such as many devices making purchases on the same account in a short period of time. Do not use the developer ID or the user's Google ID for this field. In addition, this field should not contain the user's ID in cleartext. We recommend that you use a one-way hash to generate a string from the user's ID and store the hashed string in this field.
 
$callback:Function — Will be called with the result of the purchase.

See also


Example
the $callback function must be constructed like below
     function($status:int, $purchase:Purchase, $msg:String, $wasConsumed:Boolean):void
     {
       //$status: if 1, it means that the purchase has been made successfully. 0 if otherwise.
       //$purchase: will be non-null if the purchase was successful.
       //$msg: a string representing the purchase result.
       //$wasConsumed: true if the purchase has been consumed already. This is meaningless when dealing with subscriptions
                  IMPORTANT:
             Unlike iOS which you could create consumable products inside iTunesConnect, in Android you
             can only create managed products. This means that native devs will always need to manually consume
             their products. To make your job easier and make sure the Android and iOS sides work similar to each
             other on this ANE, we are consuming the purchases on Android automatically (if you have set
             BillingType.CONSUMABLE in the doPayment method).
                  However, it is possible, that due to internet connection or any other reason, the process fails. So
             your Android consumable item will be purchased but not consumed! When this happens, you must
             forceConsume the item yourself. To know if this problem has happened, all you have to do is to
             check if the $wasConsumed param is true or not. and if it's false, call forceConsume on it.
                  This problem might happen on Android side only. on iOS, it will be fine because the product is marked
             consumable by iTunesConnect and will be consumed without ANE's help.
     }
     
forceConsume()method 
public static function forceConsume($purchaseToken:String, $callback:Function):void

(Android), Force consume a managed purchased product. Unlike iOS, you can only create managed products on the Android side but then, you can consume them using this method. On iOS, you can create consumable products in iTunesConnect and they will be consumed automatically when being purchased.

When a purchase happens successfully or when you are restoring the previous purchases, you must use the returned $purchase.purchaseToken and pass it through the forceConsume method to consume it.

Parameters

$purchaseToken:String — purchaseToken received when a managed item is purchased successfully
 
$callback:Function — callback function to let you know the result of the consume


Example
the $callback function must be constructed like below
     function($result:Boolean):void
     {
         //$result: true if the product has been consumed successfully
     }
     
getPurchases()method 
public static function getPurchases($callback:Function):void

Call this method to get a Vector list of all products that user has purchased before. Please note that only BillingType.PERMANENT and BillingType.AUTO_RENEWAL are returned.

Parameters

$callback:Function

See also


Example
the $callback function must be constructed like below
     function($purchases:Vector.<Purchase>):void
     {
       //$purchases: If null, it means there's been a problem in retriving the list of purchases from server.
       // if not null, but $purchases.length is 0, it means that there are no purchase records for this user on server
     }
     
init()method 
public static function init($androidInAppIDs:Array, $androidSubsIDs:Array, $iOSInAppIDs:Array, $iOSSubsIDs:Array, $callback:Function):void

Call this method to initialize the Billing ANE. If you are building for iOS only, you can pass null for Android parameters. The same thing is true if you are building for Android only.

When you initalize this ANE, you must pass the returning result function to know when the initializing job is finished. You must wait for a successful result before calling any other methods on this extension.

The initialization job checks and verifies the list of product IDs you have passed in, to make sure your product IDs are valid. Please be alerted that when you add your products on Google Play or iTunes Connect, it may take a few hours before they are propagate globally, so make sure you are waiting enough before testing your products availability

Parameters

$androidInAppIDs:Array — An Array of Android managed product IDs you have already registered in your Google Play console
 
$androidSubsIDs:Array — An Array of Android subscription product IDs you have already registered in your Google Play console
 
$iOSInAppIDs:Array — An Array of iOS product IDs you have already registered in your iTunes Connect
 
$iOSSubsIDs:Array — An Array of iOS subscription IDs you have already registered in your iTunes Connect
 
$callback:Function — The result of the ANE initialization. only if the ANE is initialized successfully, you can call other methods.


Example
the $callback function must be constructed like below
     function($status:int, $msg:String):void
     {
         //$status: if 1, means the initialization has been successful. if 0, it means the native code was NOT initialized successfully
         //$msg: a string message describing what is possibly wrong in case $status is 0.
     }
     
isFeatureSupported()method 
public static function isFeatureSupported($featureType:String):int

(Android), check if the named featureType is supported or not.

Parameters

$featureType:String — String value from FeatureType class.

Returns
int — if BillingResponse.OK it means that the feature is supported and corresponding error code otherwise.
priceChangeConfirmation()method 
public static function priceChangeConfirmation($productId:String, $callback:Function):void

(Android), Initiate a flow to confirm the change of price for an item subscribed by the user.

When the price of a user subscribed item has changed, launch this flow to take users to a screen with price change information. User can confirm the new price or cancel the flow.

Parameters

$productId:String — must be of type BillingType.AUTO_RENEWAL
 
$callback:Function


Example
the $callback function must be constructed like below
     function($agreed:Boolean, $productId:String):void
     {
         //$agreed: true if user agreed with the price change
         //$productId: the productId initially passed through the priceChangeConfirmation method
     }
     
redeem()method 
public static function redeem():void

(Android), You can create promotional codes and your users can redeem those codes to get your in-app-products for free! To do that, call this method and it will open the redeem window in GooglePlay app. To learn more about how promo-codes work in Android, read here:

replaceSubscription()method 
public static function replaceSubscription($oldProductId:String, $newProductId:String, $prorationMode:int, $accountId:String, $callback:Function):void

(Android), Use this method to let users downgrade/upgrade their exciting subscription. The returning callback will be similar to the one being used with the doPayment method.

Parameters

$oldProductId:String — The current productId which the user is subscribed to
 
$newProductId:String — The new productId which you wish the user downgrade/upgrade to
 
$prorationMode:int — Specifies the mode of proration during subscription upgrade/downgrade.
 
$accountId:String — Specify an optional obfuscated string that is uniquely associated with the user's account in your app. If you pass this value, Google Play can use it to detect irregular activity, such as many devices making purchases on the same account in a short period of time. Do not use the developer ID or the user's Google ID for this field. In addition, this field should not contain the user's ID in cleartext. We recommend that you use a one-way hash to generate a string from the user's ID and store the hashed string in this field.
 
$callback:Function — Will be called with the result of the purchase.

See also

verifyAndroidPurchaseLocally()method 
public static function verifyAndroidPurchaseLocally($purchase:Purchase):Boolean

(Android), like the iOS side, for security reasons you are recommended to verify the purchases using your own server. But yet, you may use this method to locally verify the purchases on device.

Before calling this method, you must have set the Android public key using the publicKey property.

Read here to learn how a purchase should be verified using your server: https://developer.android.com/google/play/billing/billing_library_overview#Verify-purchase

Parameters

$purchase:Purchase

Returns
Boolean — true if the purchase is verified.
Constant Detail
ALREADY_OWNED_ITEMConstant
public static const ALREADY_OWNED_ITEM:String = This item is already purchased, you should let user use this item.

ANDROID_LIBRARY_VERSIONConstant 
public static const ANDROID_LIBRARY_VERSION:String = 2.0.3

EXTENSION_IDConstant 
public static const EXTENSION_ID:String = com.myflashlab.air.extensions.billing

NOT_FOUND_ITEMConstant 
public static const NOT_FOUND_ITEM:String = This item not found on Google or Apple servers.

VERSIONConstant 
public static const VERSION:String = 4.0.0