From bed90dc230536b14cd62cc8299c4e06549c896d4 Mon Sep 17 00:00:00 2001 From: MAJigsaw77 <77043862+MAJigsaw77@users.noreply.github.com> Date: Sat, 15 Mar 2025 23:12:06 +0200 Subject: [PATCH] Add `InAppPurchasesUtil`. --- source/funkin/InitState.hx | 10 +++ source/funkin/mobile/util/AdMobUtil.hx | 3 +- .../funkin/mobile/util/InAppPurchasesUtil.hx | 85 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 source/funkin/mobile/util/InAppPurchasesUtil.hx diff --git a/source/funkin/InitState.hx b/source/funkin/InitState.hx index 35a6d71f6..94137b0d1 100644 --- a/source/funkin/InitState.hx +++ b/source/funkin/InitState.hx @@ -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; diff --git a/source/funkin/mobile/util/AdMobUtil.hx b/source/funkin/mobile/util/AdMobUtil.hx index 7c9596f43..6beb307a0 100644 --- a/source/funkin/mobile/util/AdMobUtil.hx +++ b/source/funkin/mobile/util/AdMobUtil.hx @@ -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) diff --git a/source/funkin/mobile/util/InAppPurchasesUtil.hx b/source/funkin/mobile/util/InAppPurchasesUtil.hx new file mode 100644 index 000000000..7cf472d1e --- /dev/null +++ b/source/funkin/mobile/util/InAppPurchasesUtil.hx @@ -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 = ['test_product_0']; + + /** + * A static variable that holds an array of currently loaded product details for in-app purchases. + */ + public static var currentLoadedProductDetails:Array = []; + + /** + * 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):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