Refactor IAP initialization and purchase handling in iap_android.cpp and IapGooglePlay.java

This commit is contained in:
Nick Leeman 2024-04-26 00:21:30 +02:00
parent d33c1b18ab
commit e1106920b1
2 changed files with 6 additions and 30 deletions

View File

@ -1,4 +1,4 @@
#if defined(DM_PLATFORM_ANDROID) // #if defined(DM_PLATFORM_ANDROID)
#include <dmsdk/sdk.h> #include <dmsdk/sdk.h>
#include <dmsdk/dlib/android.h> #include <dmsdk/dlib/android.h>
@ -15,9 +15,7 @@ struct IAP
IAP() IAP()
{ {
memset(this, 0, sizeof(*this)); memset(this, 0, sizeof(*this));
m_autoFinishTransactions = true;
} }
bool m_autoFinishTransactions;
dmScript::LuaCallbackInfo* m_Listener; dmScript::LuaCallbackInfo* m_Listener;
@ -118,12 +116,6 @@ static int IAP_Finish(lua_State* L)
{ {
DM_LUA_STACK_CHECK(L, 0); DM_LUA_STACK_CHECK(L, 0);
if(g_IAP.m_autoFinishTransactions)
{
dmLogWarning("Calling iap.finish when autofinish transactions is enabled. Ignored.");
return 0;
}
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
lua_getfield(L, -1, "state"); lua_getfield(L, -1, "state");
@ -385,8 +377,6 @@ static dmExtension::Result InitializeIAP(dmExtension::Params* params)
{ {
IAP_Queue_Create(&g_IAP.m_CommandQueue); IAP_Queue_Create(&g_IAP.m_CommandQueue);
g_IAP.m_autoFinishTransactions = dmConfigFile::GetInt(params->m_ConfigFile, "iap.auto_finish_transactions", 1) == 1;
dmAndroid::ThreadAttacher threadAttacher; dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv(); JNIEnv* env = threadAttacher.GetEnv();
@ -406,7 +396,7 @@ static dmExtension::Result InitializeIAP(dmExtension::Params* params)
g_IAP.m_SetAccountId = env->GetMethodID(iap_class, "setAccountId", "(Ljava/lang/String;)V"); g_IAP.m_SetAccountId = env->GetMethodID(iap_class, "setAccountId", "(Ljava/lang/String;)V");
jmethodID jni_constructor = env->GetMethodID(iap_class, "<init>", "(Landroid/app/Activity;Z)V"); jmethodID jni_constructor = env->GetMethodID(iap_class, "<init>", "(Landroid/app/Activity;Z)V");
g_IAP.m_IAP = env->NewGlobalRef(env->NewObject(iap_class, jni_constructor, threadAttacher.GetActivity()->clazz, g_IAP.m_autoFinishTransactions)); g_IAP.m_IAP = env->NewGlobalRef(env->NewObject(iap_class, jni_constructor, threadAttacher.GetActivity()->clazz));
jni_constructor = env->GetMethodID(iap_jni_class, "<init>", "()V"); jni_constructor = env->GetMethodID(iap_jni_class, "<init>", "()V");
g_IAP.m_IAPJNI = env->NewGlobalRef(env->NewObject(iap_jni_class, jni_constructor)); g_IAP.m_IAPJNI = env->NewGlobalRef(env->NewObject(iap_jni_class, jni_constructor));

View File

@ -49,13 +49,11 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
private Map<String, ProductDetails> products = new HashMap<String, ProductDetails>(); private Map<String, ProductDetails> products = new HashMap<String, ProductDetails>();
private BillingClient billingClient; private BillingClient billingClient;
private IPurchaseListener purchaseListener; private IPurchaseListener purchaseListener;
private boolean autoFinishTransactions;
private Activity activity; private Activity activity;
private String accountId; private String accountId;
public IapGooglePlay(Activity activity, boolean autoFinishTransactions) { public IapGooglePlay(Activity activity) {
this.activity = activity; this.activity = activity;
this.autoFinishTransactions = autoFinishTransactions;
billingClient = BillingClient.newBuilder(activity).setListener(this).enablePendingPurchases().build(); billingClient = BillingClient.newBuilder(activity).setListener(this).enablePendingPurchases().build();
billingClient.startConnection(new BillingClientStateListener() { billingClient.startConnection(new BillingClientStateListener() {
@ -356,25 +354,13 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
} }
/** /**
* Handle a purchase. If the extension is configured to automatically * Handle a purchase.
* finish transactions the purchase will be immediately consumed. Otherwise * The product will be returned via the listener without being consumed.
* the product will be returned via the listener without being consumed.
* NOTE: Billing 3.0+ requires purchases to be acknowledged within 3 days of * NOTE: Billing 3.0+ requires purchases to be acknowledged within 3 days of
* purchase unless they are consumed. * purchase unless they are consumed.
*/ */
private void handlePurchase(final Purchase purchase, final IPurchaseListener purchaseListener) { private void handlePurchase(final Purchase purchase, final IPurchaseListener purchaseListener) {
if (this.autoFinishTransactions) { invokeOnPurchaseResultListener(purchaseListener, billingResponseCodeToDefoldResponse(BillingResponseCode.OK), convertPurchase(purchase));
consumePurchase(purchase.getPurchaseToken(), new ConsumeResponseListener() {
@Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
Log.d(TAG, "handlePurchase() response code " + billingResult.getResponseCode() + " purchaseToken: " + purchaseToken);
invokeOnPurchaseResultListener(purchaseListener, billingResult, purchase);
}
});
}
else {
invokeOnPurchaseResultListener(purchaseListener, billingResponseCodeToDefoldResponse(BillingResponseCode.OK), convertPurchase(purchase));
}
} }
/** /**