Merge pull request #59 from defold/Issue-56-app-crash-on-slow-purchase

Check that the purchase listener is not null
This commit is contained in:
Björn Ritzl 2023-02-07 15:51:25 +01:00 committed by GitHub
commit 5c09447e37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 8 deletions

View File

@ -243,6 +243,20 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
return billingResponseCodeToDefoldResponse(result.getResponseCode()); return billingResponseCodeToDefoldResponse(result.getResponseCode());
} }
private void invokeOnPurchaseResultListener(IPurchaseListener purchaseListener, int billingResultCode, String purchaseData) {
if (purchaseListener == null) {
Log.w(TAG, "Received billing result but no listener has been set");
return;
}
purchaseListener.onPurchaseResult(billingResultCode, purchaseData);
}
private void invokeOnPurchaseResultListener(IPurchaseListener purchaseListener, BillingResult billingResult, Purchase purchase) {
invokeOnPurchaseResultListener(purchaseListener, billingResultToDefoldResponse(billingResult), convertPurchase(purchase));
}
private void invokeOnPurchaseResultListener(IPurchaseListener purchaseListener, BillingResult billingResult) {
invokeOnPurchaseResultListener(purchaseListener, billingResultToDefoldResponse(billingResult), "");
}
/** /**
* This method is called either explicitly from Lua or from extension code * This method is called either explicitly from Lua or from extension code
* when "set_listener()" is called from Lua. * when "set_listener()" is called from Lua.
@ -306,7 +320,7 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
// note: we only call the purchase listener if an error happens // note: we only call the purchase listener if an error happens
if (billingResult.getResponseCode() != BillingResponseCode.OK) { if (billingResult.getResponseCode() != BillingResponseCode.OK) {
Log.e(TAG, "Unable to consume purchase: " + billingResult.getDebugMessage()); Log.e(TAG, "Unable to consume purchase: " + billingResult.getDebugMessage());
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), ""); invokeOnPurchaseResultListener(purchaseListener, billingResult);
} }
} }
}); });
@ -329,7 +343,7 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
// note: we only call the purchase listener if an error happens // note: we only call the purchase listener if an error happens
if (billingResult.getResponseCode() != BillingResponseCode.OK) { if (billingResult.getResponseCode() != BillingResponseCode.OK) {
Log.e(TAG, "Unable to acknowledge purchase: " + billingResult.getDebugMessage()); Log.e(TAG, "Unable to acknowledge purchase: " + billingResult.getDebugMessage());
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), ""); invokeOnPurchaseResultListener(purchaseListener, billingResult);
} }
} }
}); });
@ -348,12 +362,12 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
@Override @Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) { public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
Log.d(TAG, "handlePurchase() response code " + billingResult.getResponseCode() + " purchaseToken: " + purchaseToken); Log.d(TAG, "handlePurchase() response code " + billingResult.getResponseCode() + " purchaseToken: " + purchaseToken);
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), convertPurchase(purchase)); invokeOnPurchaseResultListener(purchaseListener, billingResult, purchase);
} }
}); });
} }
else { else {
purchaseListener.onPurchaseResult(billingResponseCodeToDefoldResponse(BillingResponseCode.OK), convertPurchase(purchase)); invokeOnPurchaseResultListener(purchaseListener, billingResponseCodeToDefoldResponse(BillingResponseCode.OK), convertPurchase(purchase));
} }
} }
@ -368,7 +382,7 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
} }
} }
else { else {
this.purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), ""); invokeOnPurchaseResultListener(this.purchaseListener, billingResult);
} }
} }
@ -392,7 +406,7 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
BillingResult billingResult = billingClient.launchBillingFlow(this.activity, billingFlowParams); BillingResult billingResult = billingClient.launchBillingFlow(this.activity, billingFlowParams);
if (billingResult.getResponseCode() != BillingResponseCode.OK) { if (billingResult.getResponseCode() != BillingResponseCode.OK) {
Log.e(TAG, "Purchase failed: " + billingResult.getDebugMessage()); Log.e(TAG, "Purchase failed: " + billingResult.getDebugMessage());
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), ""); invokeOnPurchaseResultListener(purchaseListener, billingResult);
} }
} }
@ -417,7 +431,7 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
} }
else { else {
Log.e(TAG, "Unable to get product details before buying: " + billingResult.getDebugMessage()); Log.e(TAG, "Unable to get product details before buying: " + billingResult.getDebugMessage());
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), ""); invokeOnPurchaseResultListener(purchaseListener, billingResult);
} }
} }
}); });

View File

@ -16,7 +16,7 @@ target_sdk_version = 29
[project] [project]
title = extension-iap title = extension-iap
dependencies = https://github.com/andsve/dirtylarry/archive/master.zip dependencies#0 = https://github.com/andsve/dirtylarry/archive/master.zip
[library] [library]
include_dirs = extension-iap include_dirs = extension-iap