Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling IllegalStateException in case of mutiple Buy method call #56

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ Failure callbacks return an error as an integer. See the following error table:
| 23 | `INVALID_PAYMENT` | Indicates that one of the payment parameters was not recognized |
| 24 | `UNAUTHORIZED` | Indicates that the user is not allowed to authorise payments (e.g. parental lock) |
| 25 | `RECEIPT_REFRESH_FAILED` | |
| 26 | `CONFLICTED_ASYNC_OPERATION`| Asynchronous calls cannot be run in parallel |

======
Ref Links
Expand Down
62 changes: 52 additions & 10 deletions src/android/WizPurchase.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class WizPurchase extends CordovaPlugin {
private static final int INVALID_PAYMENT = 23;
private static final int UNAUTHORIZED = 24;
private static final int RECEIPT_REFRESH_FAILED = 25;
private static final int CONFLICTED_ASYNC_OPERATION = 26;

// =================================================================================================
// Override Plugin Methods
Expand Down Expand Up @@ -402,10 +403,26 @@ public void onIabSetupFinished(IabResult result) {
// Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
if (skus.size() <= 0){
Log.d(TAG, "Setup successful. Querying inventory.");
mHelper.queryInventoryAsync(mGotInventoryListener);
try {
mHelper.queryInventoryAsync(mGotInventoryListener);
}
catch (IllegalStateException e) {
Log.w(TAG, "IllegalStateException while getting inventory", e);

result = new IabResult(CONFLICTED_ASYNC_OPERATION, "Async operation already in progress");
mGotInventoryListener.onQueryInventoryFinished(result, null);
}
} else {
Log.d(TAG, "Setup successful. Querying inventory w/ SKUs. " + skus.toString());
mHelper.queryInventoryAsync(true, skus, mGotDetailsListener);
try {
mHelper.queryInventoryAsync(true, skus, mGotDetailsListener);
}
catch (IllegalStateException e) {
Log.w(TAG, "IllegalStateException while getting details on list of skus " + skus.toString(), e);

result = new IabResult(CONFLICTED_ASYNC_OPERATION, "Async operation already in progress");
mGotDetailsListener.onQueryInventoryFinished(result, null);
}
}
}
});
Expand All @@ -419,12 +436,21 @@ public void onIabSetupFinished(IabResult result) {
private void buy(final String sku) {
setStartedPurchase(sku, true);
// Process the purchase for the given product id and developerPayload
mHelper.launchPurchaseFlow(
cordova.getActivity(),
sku,
RC_REQUEST,
mPurchaseFinishedListener,
mDevPayload);
try {
mHelper.launchPurchaseFlow(
cordova.getActivity(),
sku,
RC_REQUEST,
mPurchaseFinishedListener,
mDevPayload);
}
catch (IllegalStateException e) {
Log.w(TAG, "IllegalStateException while launching purchase flow for sku " + sku, e);

result = new IabResult(CONFLICTED_ASYNC_OPERATION, "Async operation already in progress");
mPurchaseFinishedListener.onIabPurchaseFinished(result, null);
}

}

/**
Expand Down Expand Up @@ -460,7 +486,15 @@ private void finishPurchase(JSONArray data) throws JSONException {

if (isConsumable) {
// Process the consumption asynchronously
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
try {
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
}
catch (IllegalStateException e) {
Log.w(TAG, "IllegalStateException while consuming purchase " + purchase.toString(), e);

result = new IabResult(CONFLICTED_ASYNC_OPERATION, "Async operation already in progress");
mConsumeFinishedListener.onConsumeFinished(purchase, result);
}
} else {
unsetPurchaseAsPending(purchase);
mFinishPurchaseCbContext.success();
Expand Down Expand Up @@ -589,7 +623,15 @@ JSONObject convertToJSONObject(Purchase purchase) throws JSONException {
**/
private void getSkuDetails(final List<String> skus){
Log.d(TAG, "Querying inventory w/ SKUs.");
mHelper.queryInventoryAsync(true, skus, mGotDetailsListener);
try {
mHelper.queryInventoryAsync(true, skus, mGotDetailsListener);
}
catch (IllegalStateException e) {
Log.w(TAG, "IllegalStateException while getting details on list of skus", e);

result = new IabResult(CONFLICTED_ASYNC_OPERATION, "Async operation already in progress");
mGotDetailsListener.onQueryInventoryFinished(result, null);
}
}

// =================================================================================================
Expand Down
3 changes: 2 additions & 1 deletion src/ios/WizPurchase.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ enum CDVWizPurchaseError {
INVALID_CLIENT = 22,
INVALID_PAYMENT = 23,
UNAUTHORIZED = 24,
RECEIPT_REFRESH_FAILED = 25
RECEIPT_REFRESH_FAILED = 25,
CONFLICTED_ASYNC_OPERATION = 26
};
typedef int CDVWizPurchaseError;

Expand Down
1 change: 1 addition & 0 deletions www/WizPurchaseError.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ WizPurchaseError.INVALID_CLIENT = 22;
WizPurchaseError.INVALID_PAYMENT = 23;
WizPurchaseError.UNAUTHORIZED = 24;
WizPurchaseError.RECEIPT_REFRESH_FAILED = 25;
WizPurchaseError.CONFLICTED_ASYNC_OPERATION = 26;

module.exports = WizPurchaseError;