Add InAppPurchasesUtil.
This commit is contained in:
@@ -93,6 +93,16 @@ class InitState extends FlxState
|
||||
// Setup Haptic feedback
|
||||
funkin.haptic.Haptic.initialize();
|
||||
|
||||
#if FEATURE_ADMOB_ADS
|
||||
// Setup Admob
|
||||
funkin.mobile.util.AdMobUtil.init();
|
||||
#end
|
||||
|
||||
#if android
|
||||
// Setup In-App purchases
|
||||
funkin.mobile.util.InAppPurchasesUtil.init();
|
||||
#end
|
||||
|
||||
// This ain't a pixel art game! (most of the time)
|
||||
FlxSprite.defaultAntialiasing = true;
|
||||
|
||||
|
||||
@@ -81,9 +81,10 @@ class AdMobUtil
|
||||
|
||||
/**
|
||||
* Initializes the AdMob SDK and sets up event listeners for interstitial and rewarded ads.
|
||||
*
|
||||
* The listeners display ads automatically when they are loaded.
|
||||
*/
|
||||
public static inline function init():Void
|
||||
public static function init():Void
|
||||
{
|
||||
Admob.onStatus.add(function(event:String, message:String):Void {
|
||||
switch (event)
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package funkin.mobile.util;
|
||||
|
||||
#if android
|
||||
import iap.IAP;
|
||||
|
||||
/**
|
||||
* Utility class for managing in-app purchases in a mobile application.
|
||||
* This class provides functions to initialize the in-app purchase system,
|
||||
* handle purchase events, and manage the purchase flow for different types
|
||||
* of in-app products, including consumables and non-consumables.
|
||||
*/
|
||||
class InAppPurchasesUtil
|
||||
{
|
||||
/**
|
||||
* The public key used for verifying in-app purchases, this key is used to ensure the integrity and authenticity of the purchase data.
|
||||
*/
|
||||
static final PUBLIC_KEY:String = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvkjukXVNVifxcwvCdntLoVPlMo4H0y232w+7FJCyDLIE39Sif6peAZzRJx5qppCwKhEy0BPp8DNcPCo+KeuYjOroXgPGdwZ6Zr8Cwlrs+xKba2Yt0qr98VhF4PjjJMaDbrLCxtsXEtcpKfItu+Z7m6+4+R5HLP3lvUvK5JynvQ79mct0amKM5WLtnGGDF/cUHaj3prw745SmOpXawTjb43AAIvwWTvEPnFYsoIXodU+4KAm5QIS4FM55HpwXPs8McJFQouo8LaSzYu8SYwRsHBOZpAGgszDIK40gIUmKTe3Ca2Vd77Ib+YP1+EgHJywfuYfC5NAzQRatEX0uDLZ8fwIDAQAB";
|
||||
|
||||
/**
|
||||
* A static final array containing the product IDs for fetching product details.
|
||||
*/
|
||||
static final FETCH_PRODUCT_DETAILS_IDS:Array<String> = ['test_product_0'];
|
||||
|
||||
/**
|
||||
* A static variable that holds an array of currently loaded product details for in-app purchases.
|
||||
*/
|
||||
public static var currentLoadedProductDetails:Array<iap.android.IAPProductDetails> = [];
|
||||
|
||||
/**
|
||||
* Initializes the in-app purchases utility.
|
||||
*/
|
||||
public static function init():Void
|
||||
{
|
||||
IAP.onSetup.add(function(success:Bool):Void {
|
||||
if (success) IAP.queryProductDetails(FETCH_PRODUCT_DETAILS_IDS);
|
||||
|
||||
trace(success ? 'IAP Successfully initialized!' : 'IAP Initialization Failure!');
|
||||
});
|
||||
|
||||
IAP.onDebugLog.add(function(message:String):Void {
|
||||
trace(message);
|
||||
});
|
||||
|
||||
#if android
|
||||
IAP.onQueryProductDetails.add(function(products:Array<iap.android.IAPProductDetails>):Void {
|
||||
if (products != null && products.length > 0)
|
||||
{
|
||||
currentLoadedProductDetails = products;
|
||||
|
||||
for (product in currentLoadedProductDetails)
|
||||
{
|
||||
trace("Product Details found: " + product.title);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
trace("No products found.");
|
||||
}
|
||||
});
|
||||
#end
|
||||
|
||||
IAP.init(PUBLIC_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates the purchase process for the specified item.
|
||||
*
|
||||
* @param id The identifier of the item to be purchased.
|
||||
*/
|
||||
public static function purchase(id:String):Void
|
||||
{
|
||||
for (product in currentLoadedProductDetails)
|
||||
{
|
||||
if (product.productId == id)
|
||||
{
|
||||
IAP.purchase(product);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
trace("Didn\'t Found Product Details for ID: " + id);
|
||||
}
|
||||
}
|
||||
#end
|
||||
Reference in New Issue
Block a user