Why Android IAP initializing but not working?

Viewed 368

I am using this library to implement in app purchase in my app.

https://github.com/anjlab/android-inapp-billing-v3

I added this code to my gradle.

repositories {
  mavenCentral()
}
dependencies {
  implementation 'com.anjlab.android.iab.v3:library:1.0.44'
}

I added the permission.

 <uses-permission android:name="com.android.vending.BILLING" />

I declared variable

 BillingProcessor bp;

I used(to test it) bp in onCreate method after setContentView

   setContentView(R.layout.activity_main);



   bp = new BillingProcessor(this, "x", null,new BillingProcessor.IBillingHandler() {
            @Override
            public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
                Log.d("purchase2","purchased");
            }
            @Override
            public void onBillingError(int errorCode, @Nullable Throwable error) {
                Log.d("purchase2","error");
            }
            @Override
            public void onBillingInitialized() {
                Log.d("purchase2","initialized");
            }
            @Override
            public void onPurchaseHistoryRestored() {
                Log.d("purchase2","historyrestored");
            }
        });


        bp.purchase(this, "y");

The code does not run onProductPurchased method. It only runs onBillingInitialized method. I looked verbose logcat. There is no line about purchased when I can see initialized line.

As a result, how can I solve my problem? Why does not it work?

X and Y values are correct, I checked them in Google Play Console.

1 Answers

I think you should call

bp.initialize();

in oncreate like this

    setContentView(R.layout.activity_main);
    
    
    
       bp = new BillingProcessor(this, "x", null,new BillingProcessor.IBillingHandler() {
                @Override
                public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
                    Log.d("purchase2","purchased");
                }
                @Override
                public void onBillingError(int errorCode, @Nullable Throwable error) {
                    Log.d("purchase2","error");
                }
                @Override
                public void onBillingInitialized() {
                    Log.d("purchase2","initialized");
                }
                @Override
                public void onPurchaseHistoryRestored() {
                    Log.d("purchase2","historyrestored");
                }
            });

bp.initialize();

and call

bp.purchase(this, "y");

in button click event.

you should also override Activity's onActivityResult method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (!bp.handleActivityResult(requestCode, resultCode, data)) {
    super.onActivityResult(requestCode, resultCode, data);
  }  
}

good luck!

Related