Skip to content

Commit

Permalink
add queryInventoryAsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
softrun committed Jul 15, 2020
1 parent 82e1440 commit 9358404
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

Expand All @@ -10,11 +11,13 @@

import com.softrunapp.cafebazaarbilling.CafebazaarBilling;
import com.softrunapp.cafebazaarbilling.CafebazaarBillingListener;
import com.softrunapp.cafebazaarbilling.util.Inventory;
import com.softrunapp.cafebazaarbilling.util.Purchase;

public class MainActivity extends AppCompatActivity implements CafebazaarBillingListener {
private static final String TAG = "MainActivity";
private static final String rsaKey = "MIHNMA0GCSqGSIb3DQEBAQUAA4G7ADCBtwKBrwC8SR3KLxmir68Xgkn2G9AxCCXDwmoc78PVB3JeUXsglLE8CV9l9aetr7/hW/MXQl3QGkuK0vRPHUHxb3xxhZjXPIRZYHSvn74dkHXLuUEPi20Fr6FCCDbcNChkDR/Jv1oXEmlteQbqDQY+8ZtIsRFjsaiECB+J81OutOR2+YwmGnBLOt/MryTvpxOqXPFLehRLf2uPmDevMcHtrZKnMf5v0g7HiwkhYOO05Uy3loUCAwEAAQ==";
private static final String sku = "test";
private static final String sku = "test2";

private CafebazaarBilling cafebazaarBilling;

Expand All @@ -26,10 +29,12 @@ protected void onCreate(Bundle savedInstanceState) {
.setRsaKey(rsaKey)
.setBillingListener(this)
.build();
cafebazaarBilling.connectToBazaar();
}

public void payment(View view) {
cafebazaarBilling.purchase(sku);
// cafebazaarBilling.queryInventoryAsync();
}

@Override
Expand All @@ -41,7 +46,7 @@ public void onFailed(String message) {
public void onIabPurchaseFinished(Purchase purchase) {
Toast.makeText(MainActivity.this, "onIabPurchaseFinished", Toast.LENGTH_SHORT)
.show();
cafebazaarBilling.consumePurchase(purchase);
// cafebazaarBilling.consumePurchase(purchase);
}

@Override
Expand All @@ -67,6 +72,12 @@ public void onConnectedToBazaar() {
.show();
}

@Override
public void onQueryInventoryFinished(Inventory inventory) {
Purchase premiumPurchase = inventory.getPurchase("test2");
Log.d(TAG, premiumPurchase == null ? "null" : premiumPurchase.toString());
}

@Override
protected void onDestroy() {
super.onDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.util.Log;

import com.softrunapp.cafebazaarbilling.util.IabHelper;
import com.softrunapp.cafebazaarbilling.util.IabResult;
import com.softrunapp.cafebazaarbilling.util.Inventory;
import com.softrunapp.cafebazaarbilling.util.Purchase;


Expand All @@ -27,7 +29,8 @@ private CafebazaarBilling(Activity activity, String rsaKey, CafebazaarBillingLis

public void purchase(String sku) {
this.sku = sku;
connect();

lunchPayment();
}

private void lunchPayment() {
Expand All @@ -36,7 +39,7 @@ private void lunchPayment() {
mPurchaseFinishedListener, "bGoa+V9g/yQDXvKRqq+JTFn4uQZbPiQJo4Fp9RzJ");
}

private void connect() {
public void connectToBazaar() {
if (Utils.cafebazaarIsInstalled(activity)) {
billingListener.onStartConnectingToBazaar();
new Handler().postDelayed(this::paymentConfig, 1000);
Expand All @@ -58,8 +61,8 @@ private void paymentConfig() {
Log.d(TAG, "Problem setting up In-app Billing: " + result);
billingListener.onFailed("not Connect");
} else {
queryInventoryAsync();
billingListener.onConnectedToBazaar();
lunchPayment();
}
});
}
Expand Down Expand Up @@ -96,6 +99,28 @@ public void consumePurchase(Purchase purchase) {
}
};

public void queryInventoryAsync() {
mHelper.queryInventoryAsync(mGotInventoryListener);
}

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");

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

// Is it a failure?
if (result.isFailure()) {
billingListener.onFailed("Failed to query inventory: " + result);
return;
}

Log.d(TAG, "Query inventory was successful.");
billingListener.onQueryInventoryFinished(inventory);
}
};

public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.softrunapp.cafebazaarbilling;

import com.softrunapp.cafebazaarbilling.util.Inventory;
import com.softrunapp.cafebazaarbilling.util.Purchase;

public interface CafebazaarBillingListener {
Expand All @@ -12,6 +13,8 @@ public interface CafebazaarBillingListener {

void onConsumeFinished();

void onQueryInventoryFinished(Inventory inventory);

void onCancel();

void onFailed(String message);
Expand Down

0 comments on commit 9358404

Please sign in to comment.