Skip to content

Commit

Permalink
v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
abbasghasemi committed Mar 11, 2024
1 parent 98c0dc7 commit 6a40aa9
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions billing/src/main/java/com/farasource/billing/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class Payment {
private final ActivityResultRegistry activityResultRegistry;
// The helper object
PaymentHelper mHelper;
private String SKU = null;
private final String RSA;
private boolean globalAutoConsume, autoConsume, disposed, hasLaunch, startedSetup;
private String sku = null;
private String base64PublicKey = null;
private boolean globalAutoConsume, autoConsume, disposed, hasLaunch, startedSetup, hasGotInventory;
private OnPaymentResultListener onPaymentResultListener;
// Called when consumption is complete
PaymentHelper.OnConsumeFinishedListener mConsumeFinishedListener = new PaymentHelper.OnConsumeFinishedListener() {
Expand Down Expand Up @@ -57,7 +57,7 @@ public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
// if we were disposed of in the meantime, quit.
if (mHelper == null) return;

if (result.isFailure() || !purchase.getSku().equalsIgnoreCase(SKU)) {
if (result.isFailure() || !purchase.getSku().equalsIgnoreCase(sku)) {
onBillingStatus(TableCodes.PAYMENT_FAILED);
return;
} else {
Expand All @@ -78,6 +78,8 @@ public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
public void onQueryInventoryFinished(IabResult result, Inventory inv) {
logger.logDebug("Query inventory finished.");

hasGotInventory = false;

// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) return;

Expand All @@ -102,15 +104,19 @@ public void onQueryInventoryFinished(IabResult result, Inventory inv) {
}
};

public Payment(ActivityResultRegistry registry, Context context, String rsa) {
public Payment(ActivityResultRegistry registry, Context context) {
this(registry, context, null);
}

public Payment(ActivityResultRegistry registry, Context context, String base64PublicKey) {
this.activityResultRegistry = registry;
this.context = context;
this.RSA = rsa;
this.base64PublicKey = base64PublicKey;
}

public void setOnPaymentResultListener(OnPaymentResultListener onPaymentResultListener) {
this.onPaymentResultListener = onPaymentResultListener;
mHelper = new PaymentHelper(activityResultRegistry, context, RSA);
mHelper = new PaymentHelper(activityResultRegistry, context, base64PublicKey);
if (isMarketNotInstalled()) {
onBillingStatus(TableCodes.MARKET_NOT_INSTALLED);
return;
Expand All @@ -131,9 +137,17 @@ public void setOnPaymentResultListener(OnPaymentResultListener onPaymentResultLi

// IAB is fully set up. Now, let's get an inventory of stuff we own.
logger.logDebug("Setup successful. Querying inventory.");

if (!hasLaunch) {
try {
hasGotInventory = true;
mHelper.queryInventoryAsync(mGotInventoryListener);
} catch (Exception e) {
hasGotInventory = false;
}
}
startedSetup = true;
onBillingStatus(TableCodes.SETUP_SUCCESS);
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
} catch (Exception e) {
Expand All @@ -148,6 +162,10 @@ public void rebuildActivityResultRegistry(ActivityResultRegistry registry) {
}
}

public void setBase64PublicKey(String base64PublicKey) {
this.base64PublicKey = base64PublicKey;
}

public void launchPayment(String sku) {
launchPayment(sku, PaymentHelper.ITEM_TYPE_INAPP, "", globalAutoConsume);
}
Expand All @@ -172,7 +190,7 @@ public void launchPayment(String sku, String type, String payload, boolean autoC
if (disposed) {
onBillingStatus(TableCodes.PAYMENT_DISPOSED);
return;
} else if (hasLaunch) {
} else if (hasLaunch || hasGotInventory) {
onBillingStatus(TableCodes.PAYMENT_IS_IN_PROGRESS);
return;
} else if (isMarketNotInstalled()) {
Expand All @@ -182,7 +200,7 @@ public void launchPayment(String sku, String type, String payload, boolean autoC
onBillingStatus(TableCodes.NO_NETWORK);
return;
}
this.SKU = sku;
this.sku = sku;
this.autoConsume = autoConsume;
if (PaymentHelper.ITEM_TYPE_SUBS.equals(type) && !mHelper.subscriptionsSupported()) {
logger.logDebug("Subscriptions not supported on your device yet. Sorry!");
Expand Down Expand Up @@ -235,15 +253,20 @@ public void dispose() {
mConsumeFinishedListener = null;
}

private boolean isMarketNotInstalled() {
public static boolean isPackageInstalled(Context context, String packageID) {
try {
context.getPackageManager().getPackageInfo(mHelper.getMarketId(), PackageManager.GET_ACTIVITIES);
return false;
} catch (PackageManager.NameNotFoundException e) {
context.getPackageManager().getPackageInfo(packageID, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}

private boolean isMarketNotInstalled() {
return !isPackageInstalled(context, mHelper.getMarketId());
}


private void onBillingStatus(int code) {
if (onPaymentResultListener != null) onPaymentResultListener.onBillingStatus(code);
}
Expand Down

0 comments on commit 6a40aa9

Please sign in to comment.