Skip to content

Commit

Permalink
add Inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
softrun committed Jul 18, 2020
1 parent 308767f commit 2b62158
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

Expand All @@ -15,9 +14,8 @@
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 = "test2";
private static final String sku = "test";

private CafebazaarBilling cafebazaarBilling;

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

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

public void queryInventoryAsync(View view) {
cafebazaarBilling.queryInventoryAsync();
}

@Override
Expand All @@ -46,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 Down Expand Up @@ -74,8 +74,8 @@ public void onConnectedToBazaar() {

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

@Override
Expand Down
28 changes: 19 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/text_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@color/colorAccent"
android:clickable="true"
android:focusable="true"
android:onClick="payment"
android:onClick="purchase"
android:padding="20dp"
android:text="Click for Payment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:text="purchase" />

</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/text_view_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:clickable="true"
android:focusable="true"
android:onClick="queryInventoryAsync"
android:padding="20dp"
android:text="Inventory"
android:textColor="@color/android:white" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@


public class CafebazaarBilling {

public interface OnConnectListener {
void onConnect();
}

private static final String TAG = "CafebazaarBilling";
private static final int RC_REQUEST = 1990;

Expand All @@ -20,6 +25,7 @@ public class CafebazaarBilling {
private String rsaKey;
private CafebazaarBillingListener billingListener;
private String sku;
private OnConnectListener onConnectListener;

private CafebazaarBilling(Activity activity, String rsaKey, CafebazaarBillingListener billingListener) {
this.activity = activity;
Expand All @@ -29,8 +35,8 @@ private CafebazaarBilling(Activity activity, String rsaKey, CafebazaarBillingLis

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

lunchPayment();
onConnectListener = this::lunchPayment;
connectToBazaar();
}

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

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

public void queryInventoryAsync() {
mHelper.queryInventoryAsync(mGotInventoryListener);
onConnectListener = () -> mHelper.queryInventoryAsync(mGotInventoryListener);
connectToBazaar();
}

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
Expand Down

0 comments on commit 2b62158

Please sign in to comment.