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

Retrieves listOwnedProducts only when in background #550

Open
jansvanda opened this issue Sep 15, 2024 · 3 comments
Open

Retrieves listOwnedProducts only when in background #550

jansvanda opened this issue Sep 15, 2024 · 3 comments

Comments

@jansvanda
Copy link

jansvanda commented Sep 15, 2024

Hi there,

Recently I upgraded my app from SDK 29 to 34 and something weird happens:

I came along a weird bug. (@serggl I feel like you could be interested).

I had problems for two days to retrieve owned Products from this library. Sometimes it worked, but in 95% cases it did not work and my app told me whole time that I have nothing purchased yet.

You say here, that this gets called when everything is loaded from the history:
@Override public void onPurchaseHistoryRestored() { /* * Called when purchase history was restored and the list of all owned PRODUCT ID's * was loaded from Google Play */ } }
But that is not true, it gets called every time, but when in background, it retrieves the items everytime and when in foreground, then the items are empty.

This is my code at the start of the app inside my SplashActivity.java:

bp = BillingProcessor.newBillingProcessor(this, LICENSE_KEY, MERCHANT_ID, new BillingProcessor.IBillingHandler() {
            @Override
            public void onProductPurchased(@NonNull String productId, @Nullable PurchaseInfo purchaseInfo) {
            }
            @Override
            public void onBillingError(int errorCode, @Nullable Throwable error) {
                showToast("onBillingError: " + errorCode);
                Log.d("FUCK", "onBillingError: " + errorCode);
            }
            @Override
            public void onBillingInitialized() {
                Log.d("FUCK", "Billing init");

            }
            @Override
            public void onPurchaseHistoryRestored() {

                Log.d("FUCK", "called onPurchaseHistoryRestored");
                for(String sku : bp.listOwnedProducts()){
                    Log.d("FUCK", "Owned Managed Product: " + sku);

                    if (sku.equals(PRODUCT_ID)){
                        Log.i("FUCK", "Activating SKU: " + sku);
                        prefs.edit().putBoolean("purchased", true).apply();
                        prefs.edit().putString("key_paid", "paid").apply();
                        binding.imageProSplash.setVisibility(View.VISIBLE);
                    } else {
                        Log.e("billing", "setting unpaid");
                        prefs.edit().putBoolean("purchased", false).apply();
                        prefs.edit().putString("key_paid", "unpaid").apply();
                        binding.imageProSplash.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    bp.initialize();

Now with normally screen on and app deployed the Logs are following:
image

BUT with screen off during installation of the app to the device, i got following Logs:
image

Can you anyone tell me if this is correct? I don't have the feeling this is right. Also I don't wanna push this into release... It doesn't retrieve the purchased products!

Using version: 2.2.0

minSdkVersion 26
targetSdkVersion 34
compileSdk 34
buildToolsVersion '34.0.0'
implementation 'com.android.billingclient:billing:7.0.0'
 implementation 'com.android.billingclient:billing-ktx:7.0.0'
@jansvanda
Copy link
Author

Oh I missed to tell the part how I came to try this solution..
I searched for onPurchaseHistoryRestored in the code and found this line


Which I don't even know why somhow rang my bell and I tried to "background" the app while the process. And voila - it helped...

Does this not happen in production? I mean i got a version of older anjlab billing plugin on google play and there it works great.

@jansvanda
Copy link
Author

Guys, i am not lying - this is some weird stuff here. My purchased products get retrieved only when i close the app or turn the phone's screen off.
I am testing on a OnePlus 6 running Android 11, with signed debug apk, same versioncode and name as the one uploaded on Gplay.
Any help would be much appreciated.

@jansvanda
Copy link
Author

Okay seems like i used it wrong all the time. This is what works now each time:
`private void initBillingClient() {
bp = BillingProcessor.newBillingProcessor(this, LICENSE_KEY, MERCHANT_ID, new BillingProcessor.IBillingHandler() {
@OverRide
public void onProductPurchased(@nonnull String productId, @nullable PurchaseInfo purchaseInfo) {
//showToast("onProductPurchased: " + productId);
Log.d("HansBilling","Purchased "+productId);
}
@OverRide
public void onBillingError(int errorCode, @nullable Throwable error) {
showToast("onBillingError: " + errorCode);
Log.d("HansBilling", "onBillingError: " + errorCode);
}
@OverRide
public void onBillingInitialized() {
//showToast("onBillingInitialized");
Log.d("HansBilling", "Billing init");
}

        @Override
        public void onPurchaseHistoryRestored() {
            Log.d("HansBilling", "called onPurchaseHistoryRestored");

            bp.loadOwnedPurchasesFromGoogleAsync(new IPurchasesResponseListener() {
                @Override
                public void onPurchasesSuccess() {
                    if (bp.isPurchased(PRODUCT_ID)) {
                        showToast("Máte zakoupenou verzi PRO, aktivuji ...");
                        prefs.edit().putBoolean("purchased", true).apply();
                        prefs.edit().putString("key_paid", "paid").apply();
                        binding.imageProSplash.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onPurchasesError() {
                    Log.d("HansBilling", "purchase error" );

                }
            });

            for(String sku : bp.listOwnedProducts()){
                Log.d("HansBilling", "Owned Managed Product: " + sku);

                if (sku.equals(PRODUCT_ID)){
                    showToast("Máte zakoupenou verzi PRO, aktivuji ...");
                    Log.i("HansBilling", "Activating SKU: " + sku);
                    prefs.edit().putBoolean("purchased", true).apply();
                    prefs.edit().putString("key_paid", "paid").apply();
                    binding.imageProSplash.setVisibility(View.VISIBLE);
                } else {
                    Log.e("billing", "setting unpaid");
                    prefs.edit().putBoolean("purchased", false).apply();
                    prefs.edit().putString("key_paid", "unpaid").apply();
                    binding.imageProSplash.setVisibility(View.VISIBLE);
                }
            }
        }
    });

    bp.initialize();

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant