Added iap.acknowledge

This commit is contained in:
Björn Ritzl
2020-07-24 00:10:04 +02:00
parent b1233f5912
commit 497d0dee1e
7 changed files with 447 additions and 19 deletions

View File

@@ -43,6 +43,7 @@ struct IAP
jmethodID m_Buy;
jmethodID m_Restore;
jmethodID m_ProcessPendingConsumables;
jmethodID m_AcknowledgeTransaction;
jmethodID m_FinishTransaction;
IAPCommandQueue m_CommandQueue;
@@ -146,6 +147,46 @@ static int IAP_Finish(lua_State* L)
return 0;
}
static int IAP_Acknowledge(lua_State* L)
{
int top = lua_gettop(L);
luaL_checktype(L, 1, LUA_TTABLE);
lua_getfield(L, -1, "state");
if (lua_isnumber(L, -1))
{
if(lua_tointeger(L, -1) != TRANS_STATE_PURCHASED)
{
dmLogError("Invalid transaction state (must be iap.TRANS_STATE_PURCHASED).");
lua_pop(L, 1);
assert(top == lua_gettop(L));
return 0;
}
}
lua_pop(L, 1);
lua_getfield(L, -1, "receipt");
if (!lua_isstring(L, -1)) {
dmLogError("Transaction error. Invalid transaction data, does not contain 'receipt' key.");
lua_pop(L, 1);
}
else
{
const char * receipt = lua_tostring(L, -1);
lua_pop(L, 1);
JNIEnv* env = Attach();
jstring receiptUTF = env->NewStringUTF(receipt);
env->CallVoidMethod(g_IAP.m_IAP, g_IAP.m_AcknowledgeTransaction, receiptUTF, g_IAP.m_IAPJNI);
env->DeleteLocalRef(receiptUTF);
Detach();
}
assert(top == lua_gettop(L));
return 0;
}
static int IAP_Restore(lua_State* L)
{
// TODO: Missing callback here for completion/error
@@ -193,6 +234,7 @@ static const luaL_reg IAP_methods[] =
{"list", IAP_List},
{"buy", IAP_Buy},
{"finish", IAP_Finish},
{"acknowledge", IAP_Acknowledge},
{"restore", IAP_Restore},
{"set_listener", IAP_SetListener},
{"get_provider_id", IAP_GetProviderId},
@@ -226,6 +268,7 @@ JNIEXPORT void JNICALL Java_com_defold_iap_IapJNI_onProductsResult(JNIEnv* env,
JNIEXPORT void JNICALL Java_com_defold_iap_IapJNI_onPurchaseResult__ILjava_lang_String_2(JNIEnv* env, jobject, jint responseCode, jstring purchaseData)
{
dmLogInfo("Java_com_defold_iap_IapJNI_onPurchaseResult__ILjava_lang_String_2 %d", (int)responseCode);
const char* pd = 0;
if (purchaseData)
{
@@ -398,6 +441,7 @@ static dmExtension::Result InitializeIAP(dmExtension::Params* params)
g_IAP.m_Stop = env->GetMethodID(iap_class, "stop", "()V");
g_IAP.m_ProcessPendingConsumables = env->GetMethodID(iap_class, "processPendingConsumables", "(Lcom/defold/iap/IPurchaseListener;)V");
g_IAP.m_FinishTransaction = env->GetMethodID(iap_class, "finishTransaction", "(Ljava/lang/String;Lcom/defold/iap/IPurchaseListener;)V");
g_IAP.m_AcknowledgeTransaction = env->GetMethodID(iap_class, "acknowledgeTransaction", "(Ljava/lang/String;Lcom/defold/iap/IPurchaseListener;)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, dmGraphics::GetNativeAndroidActivity(), g_IAP.m_autoFinishTransactions));

View File

@@ -193,6 +193,11 @@ static int IAP_Finish(lua_State* L)
return 0;
}
static int IAP_Acknowledge(lua_State* L)
{
return 0;
}
static int IAP_Restore(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 1);
@@ -212,6 +217,7 @@ static const luaL_reg IAP_methods[] =
{"list", IAP_List},
{"buy", IAP_Buy},
{"finish", IAP_Finish},
{"acknowledge", IAP_Acknowledge},
{"restore", IAP_Restore},
{"set_listener", IAP_SetListener},
{"get_provider_id", IAP_GetProviderId},

View File

@@ -503,6 +503,11 @@ static int IAP_SetListener(lua_State* L)
return 0;
}
static int IAP_Acknowledge(lua_State* L)
{
return 0;
}
static int IAP_GetProviderId(lua_State* L)
{
lua_pushinteger(L, PROVIDER_ID_APPLE);
@@ -514,6 +519,7 @@ static const luaL_reg IAP_methods[] =
{"list", IAP_List},
{"buy", IAP_Buy},
{"finish", IAP_Finish},
{"acknowledge", IAP_Acknowledge},
{"restore", IAP_Restore},
{"set_listener", IAP_SetListener},
{"get_provider_id", IAP_GetProviderId},

View File

@@ -28,11 +28,12 @@ import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.ConsumeParams;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.AcknowledgePurchaseParams;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.ConsumeResponseListener;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.android.billingclient.api.AcknowledgePurchaseResponseListener;
public class IapGooglePlay implements PurchasesUpdatedListener {
public static final String TAG = "IapGooglePlay";
@@ -70,7 +71,14 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
public void stop() {
Log.d(TAG, "stop()");
billingClient.endConnection();
this.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (billingClient.isReady()) {
billingClient.endConnection();
}
}
});
}
public String toISO8601(final Date date) {
@@ -172,6 +180,7 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
defoldResponse = IapJNI.BILLING_RESPONSE_RESULT_ERROR;
break;
}
Log.d(TAG, "billingResponseCodeToDefoldResponse: " + responseCode + " defoldResponse: " + defoldResponse);
return defoldResponse;
}
@@ -241,6 +250,29 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
});
}
/**
* Called from Lua. This method will try to acknowledge a purchase (but not finish/consume it).
*/
public void acknowledgeTransaction(final String purchaseToken, final IPurchaseListener purchaseListener) {
Log.d(TAG, "acknowledgeTransaction() " + purchaseToken);
AcknowledgePurchaseParams acknowledgeParams = AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchaseToken)
.build();
billingClient.acknowledgePurchase(acknowledgeParams, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
Log.d(TAG, "acknowledgeTransaction() response code " + billingResult.getResponseCode());
// note: we only call the purchase listener if an error happens
if (billingResult.getResponseCode() != BillingResponseCode.OK) {
Log.e(TAG, "Unable to acknowledge purchase: " + billingResult.getDebugMessage());
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), "");
}
}
});
}
/**
* Handle a purchase. If the extension is configured to automatically
* finish transactions the purchase will be immediately consumed. Otherwise
@@ -385,7 +417,11 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
});
}
/**
* Called from Lua.
*/
public void restore(final IPurchaseListener listener) {
Log.d(TAG, "restore()");
processPendingConsumables(listener);
}
}