From 609071876c71196243094c717bc39a28988b1e08 Mon Sep 17 00:00:00 2001 From: Nick Leeman Date: Sun, 21 Apr 2024 21:55:20 +0200 Subject: [PATCH] Implemented support for account identifiers for Google Play and StoreKit. --- extension-iap/src/iap_android.cpp | 16 ++++++++++++++++ extension-iap/src/iap_emscripten.cpp | 6 ++++++ extension-iap/src/iap_ios.mm | 18 ++++++++++++++++++ .../src/java/com/defold/iap/IapGooglePlay.java | 12 +++++++++++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/extension-iap/src/iap_android.cpp b/extension-iap/src/iap_android.cpp index 8dcfe5b..1eff807 100644 --- a/extension-iap/src/iap_android.cpp +++ b/extension-iap/src/iap_android.cpp @@ -38,6 +38,21 @@ struct IAP static IAP g_IAP; +static int IAP_SetAccountId(lua_State* L) +{ + DM_LUA_STACK_CHECK(L, 0); + + const char* account_id = luaL_checkstring(L, 1); + + dmAndroid::ThreadAttacher threadAttacher; + JNIEnv* env = threadAttacher.GetEnv(); + jstring account_id_utf = env->NewStringUTF(account_id); + env->CallVoidMethod(g_IAP.m_IAP, g_IAP.m_SetAccountId, account_id_utf); + env->DeleteLocalRef(account_id_utf); + + return 0; +} + static int IAP_ProcessPendingTransactions(lua_State* L) { DM_LUA_STACK_CHECK(L, 0); @@ -236,6 +251,7 @@ static const luaL_reg IAP_methods[] = {"set_listener", IAP_SetListener}, {"get_provider_id", IAP_GetProviderId}, {"process_pending_transactions", IAP_ProcessPendingTransactions}, + {"set_account_id", IAP_SetAccountId}, {0, 0} }; diff --git a/extension-iap/src/iap_emscripten.cpp b/extension-iap/src/iap_emscripten.cpp index fb386db..0a4e626 100644 --- a/extension-iap/src/iap_emscripten.cpp +++ b/extension-iap/src/iap_emscripten.cpp @@ -62,6 +62,12 @@ static void IAPList_Callback(void* luacallback, const char* result_json) dmScript::TeardownCallback(callback); } +static int IAP_SetAccountId(lua_State* L) +{ + dmLogError("iap.set_account_id is not supported on this platform."); + return 0; +} + static int IAP_ProcessPendingTransactions(lua_State* L) { return 0; diff --git a/extension-iap/src/iap_ios.mm b/extension-iap/src/iap_ios.mm index 5209ea4..f3b28fd 100644 --- a/extension-iap/src/iap_ios.mm +++ b/extension-iap/src/iap_ios.mm @@ -31,6 +31,7 @@ struct IAP IAPCommandQueue m_CommandQueue; IAPCommandQueue m_ObservableQueue; SKPaymentTransactionObserver* m_Observer; + char * m_AccountId; }; IAP g_IAP; @@ -383,6 +384,19 @@ static void processTransactions(IAP* iap, NSArray* transactions) { } } +static int IAP_SetAccountId(lua_State* L) +{ + int top = lua_gettop(L); + + const char *account_id = luaL_checkstring(L, 1); + if (g_IAP.m_AccountId) { + free(g_IAP.m_AccountId); + } + + g_IAP.m_AccountId = strdup(account_id); + return 0; +} + static int IAP_ProcessPendingTransactions(lua_State* L) { processTransactions(&g_IAP, [SKPaymentQueue defaultQueue].transactions); @@ -431,6 +445,9 @@ static int IAP_Buy(lua_State* L) SKMutablePayment* payment = [[SKMutablePayment alloc] init]; payment.productIdentifier = [NSString stringWithUTF8String: id]; payment.quantity = 1; + if (g_IAP.m_AccountId) { + payment.applicationUsername = [NSString stringWithUTF8String: g_IAP.m_AccountId]; + } [[SKPaymentQueue defaultQueue] addPayment:payment]; [payment release]; @@ -531,6 +548,7 @@ static const luaL_reg IAP_methods[] = {"set_listener", IAP_SetListener}, {"get_provider_id", IAP_GetProviderId}, {"process_pending_transactions", IAP_ProcessPendingTransactions}, + {"set_account_id", IAP_SetAccountId}, {0, 0} }; diff --git a/extension-iap/src/java/com/defold/iap/IapGooglePlay.java b/extension-iap/src/java/com/defold/iap/IapGooglePlay.java index 1d366b4..7bc992e 100644 --- a/extension-iap/src/java/com/defold/iap/IapGooglePlay.java +++ b/extension-iap/src/java/com/defold/iap/IapGooglePlay.java @@ -51,6 +51,7 @@ public class IapGooglePlay implements PurchasesUpdatedListener { private IPurchaseListener purchaseListener; private boolean autoFinishTransactions; private Activity activity; + private String accountId; public IapGooglePlay(Activity activity, boolean autoFinishTransactions) { this.activity = activity; @@ -257,6 +258,11 @@ public class IapGooglePlay implements PurchasesUpdatedListener { invokeOnPurchaseResultListener(purchaseListener, billingResultToDefoldResponse(billingResult), ""); } + public void setAccountId(String accountId) { + Log.d(TAG, "setAccountId() " + accountId); + this.accountId = accountId; + } + /** * This method is called either explicitly from Lua or from extension code * when "set_listener()" is called from Lua. @@ -405,7 +411,11 @@ public class IapGooglePlay implements PurchasesUpdatedListener { productDetailsParams.add(ProductDetailsParams.newBuilder().setProductDetails(pd).build()); } - final BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder().setProductDetailsParamsList(productDetailsParams).build(); + final BillingFlowParams.Builder billingFlowBuilder = BillingFlowParams.newBuilder().setProductDetailsParamsList(productDetailsParams); + if (this.accountId != null) { + billingFlowBuilder.setObfuscatedAccountId(this.accountId); + } + final BillingFlowParams billingFlowParams = billingFlowBuilder.build(); BillingResult billingResult = billingClient.launchBillingFlow(this.activity, billingFlowParams); if (billingResult.getResponseCode() != BillingResponseCode.OK) {