private void buy()

in ClassyTaxiJava/app/src/main/java/com/sample/android/classytaxijava/ui/BillingViewModel.java [190:268]


    private void buy(String sku, @Nullable String oldSku) {
        // First, determine whether the new SKU can be purchased.
        boolean isSkuOnServer = BillingUtilities
                .serverHasSubscription(subscriptions.getValue(), sku);
        boolean isSkuOnDevice = BillingUtilities
                .deviceHasGooglePlaySubscription(purchases.getValue(), sku);
        Log.d("Billing", sku + " - isSkuOnServer: " + isSkuOnServer +
                ", isSkuOnDevice: " + isSkuOnDevice);
        if (isSkuOnDevice && isSkuOnServer) {
            Log.e("Billing", "You cannot buy a SKU that is already owned: " + sku +
                    "This is an error in the application trying to use Google Play Billing.");
        } else if (isSkuOnDevice && !isSkuOnServer) {
            Log.e("Billing", "The Google Play Billing Library APIs indicate that" +
                    "this SKU is already owned, but the purchase token is not registered " +
                    "with the server. There might be an issue registering the purchase token.");
        } else if (!isSkuOnDevice && isSkuOnServer) {
            Log.w("Billing", "WHOA! The server says that the user already owns " +
                    "this item: $sku. This could be from another Google account. " +
                    "You should warn the user that they are trying to buy something " +
                    "from Google Play that they might already have access to from " +
                    "another purchase, possibly from a different Google account " +
                    "on another device.\n" +
                    "You can choose to block this purchase.\n" +
                    "If you are able to cancel the existing subscription on the server, " +
                    "you should allow the user to subscribe with Google Play, and then " +
                    "cancel the subscription after this new subscription is complete. " +
                    "This will allow the user to seamlessly transition their payment " +
                    "method from an existing payment method to this Google Play account.");
        } else {
            // Second, determine whether the old SKU can be replaced.
            // If the old SKU cannot be used, set this value to null and ignore it.

            String oldSkuToBeReplaced = null;
            if (isOldSkuReplaceable(subscriptions.getValue(), purchases.getValue(), oldSku)) {
                oldSkuToBeReplaced = oldSku;
            }

            // Third, create the billing parameters for the purchase.
            if (sku.equals(oldSkuToBeReplaced)) {
                Log.i("Billing", "Re-subscribe.");
            } else if (Constants.PREMIUM_SKU.equals(sku)
                    && Constants.BASIC_SKU.equals(oldSkuToBeReplaced)) {
                Log.i("Billing", "Upgrade!");
            } else if (Constants.BASIC_SKU.equals(sku)
                    && Constants.PREMIUM_SKU.equals(oldSkuToBeReplaced)) {
                Log.i("Billing", "Downgrade...");
            } else {
                Log.i("Billing", "Regular purchase.");
            }

            SkuDetails skuDetails = null;
            // Create the parameters for the purchase.
            if (skusWithSkuDetails.getValue() != null) {
                skuDetails = skusWithSkuDetails.getValue().get(sku);
            }

            if (skuDetails == null) {
                Log.e("Billing", "Could not find SkuDetails to make purchase.");
                return;
            }

            BillingFlowParams.Builder billingBuilder =
                    BillingFlowParams.newBuilder().setSkuDetails(skuDetails);
            // Only set the old SKU parameter if the old SKU is already owned.
            if (oldSkuToBeReplaced != null && !oldSkuToBeReplaced.equals(sku)) {
                Purchase oldPurchase = BillingUtilities
                        .getPurchaseForSku(purchases.getValue(), oldSkuToBeReplaced);
                billingBuilder.setSubscriptionUpdateParams(
                        BillingFlowParams.SubscriptionUpdateParams.newBuilder()
                                .setOldSkuPurchaseToken(oldPurchase.getPurchaseToken())
                                .build());
            }

            BillingFlowParams billingParams = billingBuilder.build();

            // Send the parameters to the Activity in order to launch the billing flow.
            buyEvent.postValue(billingParams);
        }
    }