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

No indication that a purchase has been made #67

Open
NewTeslaUser opened this issue Nov 27, 2023 · 1 comment
Open

No indication that a purchase has been made #67

NewTeslaUser opened this issue Nov 27, 2023 · 1 comment

Comments

@NewTeslaUser
Copy link

In my app I am adding In App Purchase using this library.

This is what my activity code looks like:

public class InfoController extends AppCompatActivity {

    private static final String PREFS_NAME = "TEST_MyPrefsFile";

    private static final String FIRST_TIME_KEY = "TEST_isFirstLaunch";

    private static final String IS_PRO_KEY = "TEST_isPro";
    private boolean isPro() {
        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return preferences.getBoolean(IS_PRO_KEY, false);
    }

    private void setProUser(boolean isPro) {
        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(IS_PRO_KEY, isPro);
        editor.apply();
    }

    private Button upgradeButton;

    String base64String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    private BillingConnector billingConnector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);

        upgradeButton = findViewById(R.id.upgradeButton);

        if (!isPro()) {
            upgradeButton.setText("Click here to upgrade to Pro");
            initializeBillingClient();
        } else {
            upgradeButton.setEnabled(false);
            upgradeButton.setText("User is Pro");
        }
    }


    private void initializeBillingClient() {
        List<String> nonConsumableIds = new ArrayList<>();
        nonConsumableIds.add("com.xxxx.pro");  // Replace with your actual non-consumable product ID

        billingConnector = new BillingConnector(this, base64String)
                .setNonConsumableIds(nonConsumableIds)
                .autoAcknowledge()
                .enableLogging()
                .connect();

        billingConnector.setBillingEventListener(new BillingEventListener() {
            @Override
            public void onProductsFetched(@NonNull List<ProductInfo> productDetails) {

            }

            //this IS the listener in which we can restore previous purchases.
            // Code will execute on InfoController.java appear. If pro is already purchased, unlock pro features.
            @Override
            public void onPurchasedProductsFetched(@NonNull ProductType productType, @NonNull List<PurchaseInfo> purchases) {
                String purchasedProduct;
                boolean isAcknowledged;

                for (PurchaseInfo purchaseInfo : purchases) {
                    purchasedProduct = purchaseInfo.getProduct();
                    isAcknowledged = purchaseInfo.isAcknowledged();

                    if (!isPro()) {
                        if (purchasedProduct.equalsIgnoreCase("com.xxxx.pro")) {
                            if (isAcknowledged) {

                                // CustomToast.makeText(InfoController.this, "The previous purchase was successfully restored.", Toast.LENGTH_SHORT).show();

                                // setProUser(true);

                                Toast.makeText(InfoController.this, "isAcknowledged", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }
            }

            //this IS NOT the listener in which we'll give user entitlement for purchases (see ReadMe.md why)
            @Override
            public void onProductsPurchased(@NonNull List<PurchaseInfo> purchases) {

            }

            //this IS the listener in which we'll give user entitlement for purchases (the ReadMe.md explains why)
            @Override
            public void onPurchaseAcknowledged(@NonNull PurchaseInfo purchase) {
                String acknowledgedProduct = purchase.getProduct();

                if (acknowledgedProduct.equalsIgnoreCase("com.xxxx.pro")) {

                    Toast.makeText(InfoController.this, "The purchase was successfully made.", Toast.LENGTH_SHORT).show();

                    setProUser(true);

                    upgradeButton.setEnabled(false);
                    upgradeButton.setText("User is Pro");
                }
            }

            @Override
            public void onPurchaseConsumed(@NonNull PurchaseInfo purchase) {

            }

            @Override
            public void onBillingError(@NonNull BillingConnector billingConnector, @NonNull BillingResponse response) {
                switch (response.getErrorType()) {
                    case ACKNOWLEDGE_WARNING:
                        //this response will be triggered when the purchase is still PENDING
                        CustomToast.makeText(InfoController.this, "The transaction is still pending. Please come back later to receive the purchase!", Toast.LENGTH_SHORT).show();
                        break;
                    case BILLING_UNAVAILABLE:
                    case SERVICE_UNAVAILABLE:
                        CustomToast.makeText(InfoController.this, "Billing is unavailable at the moment. Check your internet connection!", Toast.LENGTH_SHORT).show();
                        break;
                    case ERROR:
                        CustomToast.makeText(InfoController.this, "Something happened, the transaction was canceled!", Toast.LENGTH_SHORT).show();
                        break;
                    case ITEM_ALREADY_OWNED:
                        Toast.makeText(InfoController.this, "The purchase has already been made.", Toast.LENGTH_SHORT).show();
                        setProUser(true);
                        upgradeButton.setEnabled(false);
                        upgradeButton.setText("User is Pro");
                            break;
                    case ITEM_NOT_OWNED:
                        //TODO - failure to consume since item is not owned
                        break;
                }
            }
        });
    }

    public void onUpgradeButtonClick(View view) {
        billingConnector.purchase(InfoController.this, "com.xxxx.pro");
    }
}

When I am testing in app purchase I am using promo codes (since I do not have a card connected to my account).

So when I open up this activity, nothing shows up. I press the upgradeButton, the upgrade prompt pops up, I select promo code and type in the promo code, click next and it tells me that the price is 0 and I press confirm. After this, the purchase prompt window just closes and nothing happens. No Toast message shows up, the upgradeButton text still says: Click here to upgrade to Pro.

If I now close the app, open it back up and go to the InfoController (this activity), the Toast message isAcknowledged shows up.

So my question is: How come nothing happens after the purchase is done?
(When I open up the activity again after the purchase, it shows isAcknowledged?)

@NewTeslaUser
Copy link
Author

@moisoni97 Any ideas?

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