Home > Java > Android Studio Java - In-app purchase errors/issues

Android Studio Java - In-app purchase errors/issues

王林
Release: 2024-02-08 22:45:32
forward
682 people have browsed it

php editor Baicao provides you with methods to solve Android Studio Java in-app purchase errors and problems. In-app purchases are a common feature when developing apps, but many developers may encounter various problems when implementing them. This article will share effective methods to solve in-app purchase errors and issues to help you successfully complete the development and release of your app. Whether you encounter problems such as unavailable products for purchase, interruption of the purchase process, or failed payment verification, we will provide detailed solutions to help you quickly solve the problem and improve the user experience of the application.

Question content

In my application I use this library to add in-app purchases.

This is my activity code:

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");
    }
}
Copy after login

When I'm testing in-app purchases, I use promo codes (because I don't have a card connected to my account).

So when I open this activity nothing is shown. I press upgradebutton, the upgrade prompt pops up, I select the promo code and enter the promo code, click next, it tells me the price is 0, I press confirm. After this, the purchase prompt window will close and nothing will happen. The toast message is not displayed and the upgrade button text still displays: click here to upgrade to pro.

If I now close the app, reopen it and go to the infocontroller (this activity), the toast message isacknowledged is displayed.

So my question is: Why does nothing happen after the purchase is completed, but when I open the activity again after the purchase, it says isacknowledged?

Solution

Check the confirmation status before processing:

@override
public void onpurchasedproductsfetched(@nonnull producttype producttype, @nonnull list<purchaseinfo> purchases) {
    for (purchaseinfo purchaseinfo : purchases) {
        string purchasedproduct = purchaseinfo.getproduct();
        if (!ispro() && purchasedproduct.equalsignorecase("com.xxxx.pro")) {
            if (!billingconnector.ispurchaseditemacknowledged(purchaseinfo)) {
                // here is the code of purchase processing
            }
        }
    }
}
Copy after login

Also move the confirmation logic from the onpurchaseacknowledged method to the onpurchasedproductsfetched method. This might help.

@Override
public void onPurchasedProductsFetched(@NonNull ProductType productType, @NonNull List<PurchaseInfo> purchases) {
    for (PurchaseInfo purchaseInfo : purchases) {
        String purchasedProduct = purchaseInfo.getProduct();
        if (!isPro() && purchasedProduct.equalsIgnoreCase("com.xxxx.pro")) {
            // here is the code of purchase processing
            handlePurchase(purchaseInfo);
        }
    }
}

private void handlePurchase(PurchaseInfo purchaseInfo) {
    String acknowledgedProduct = purchaseInfo.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");
    }
}
Copy after login

The above is the detailed content of Android Studio Java - In-app purchase errors/issues. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template