导航菜单

页面标题

页面副标题

fieldd v4.2.4 - PurchasePlugin.java 源代码

正在查看: fieldd v4.2.4 应用的 PurchasePlugin.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package cc.fovea;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import androidx.core.os.EnvironmentCompat;
import ch.qos.logback.core.joran.action.Action;
import com.android.billingclient.api.AcknowledgePurchaseParams;
import com.android.billingclient.api.AcknowledgePurchaseResponseListener;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.ConsumeParams;
import com.android.billingclient.api.ConsumeResponseListener;
import com.android.billingclient.api.ProductDetails;
import com.android.billingclient.api.ProductDetailsResponseListener;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesResponseListener;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.QueryProductDetailsParams;
import com.android.billingclient.api.QueryPurchasesParams;
import com.google.android.gms.analytics.ecommerce.ProductAction;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class PurchasePlugin extends CordovaPlugin implements PurchasesUpdatedListener, ConsumeResponseListener, AcknowledgePurchaseResponseListener {
    public static final int BILLING_CLIENT_NOT_CONNECTED = -1;
    private BillingClient mBillingClient;
    private CallbackContext mCallbackContext;
    BillingResult mInAppResult;
    private boolean mIsServiceConnected;
    BillingResult mSubsResult;
    private final String mTag = "CdvPurchase";
    private List<String> mInAppProductIds = new ArrayList();
    private List<String> mSubsProductIds = new ArrayList();
    private final List<Purchase> mPurchases = new ArrayList();
    private BillingResult mBillingClientResult = BillingResult.newBuilder().setResponseCode(-1).build();
    private final HashMap<String, ProductDetails> mProductDetails = new HashMap<>();
    private Set<String> mTokensToBeConsumed = new HashSet();
    private CallbackContext mListenerContext = null;
    private long mLastQueryOnStart = 0;
    private int nProductDetailsQuerySuccessful = 0;

    static int access$708(PurchasePlugin purchasePlugin) {
        int i = purchasePlugin.nProductDetailsQuerySuccessful;
        purchasePlugin.nProductDetailsQuerySuccessful = i + 1;
        return i;
    }

    private void addInAppProductIds(List<String> list) {
        for (int i = 0; i < list.size(); i++) {
            if (!this.mInAppProductIds.contains(list.get(i))) {
                this.mInAppProductIds.add(list.get(i));
            }
        }
    }

    private void addSubsProductIds(List<String> list) {
        for (int i = 0; i < list.size(); i++) {
            if (!this.mSubsProductIds.contains(list.get(i))) {
                this.mSubsProductIds.add(list.get(i));
            }
        }
    }

    public BillingResult getLastResult() {
        return this.mBillingClientResult;
    }

    private int getLastResponseCode() {
        return this.mBillingClientResult.getResponseCode();
    }

    private void resetLastResult(int i) {
        this.mBillingClientResult = BillingResult.newBuilder().setResponseCode(i).setDebugMessage("").build();
    }

    public void initialize(CordovaInterface cordovaInterface, CordovaWebView cordovaWebView) {
        super.initialize(cordovaInterface, cordovaWebView);
    }

    private void sendToListener(String str, JSONObject jSONObject) {
        try {
            Log.d("CdvPurchase", "sendToListener() -> " + str);
            Log.d("CdvPurchase", "            data -> " + jSONObject.toString());
            if (this.mListenerContext == null) {
                return;
            }
            JSONObject put = new JSONObject().put("type", str);
            if (jSONObject != null) {
                put.put("data", jSONObject);
            }
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, put);
            pluginResult.setKeepCallback(true);
            this.mListenerContext.sendPluginResult(pluginResult);
        } catch (JSONException e) {
            Log.d("CdvPurchase", "sendToListener() -> Failed: " + e.getMessage());
        }
    }

    public boolean execute(String str, JSONArray jSONArray, CallbackContext callbackContext) {
        if ("setListener".equals(str)) {
            this.mListenerContext = callbackContext;
            sendToListener("ready", new JSONObject());
            return true;
        }
        this.mCallbackContext = callbackContext;
        Boolean bool = true;
        try {
            if ("init".equals(str)) {
                init();
            } else if ("getAvailableProducts".equals(str)) {
                List<String> parseStringArrayAtIndex = parseStringArrayAtIndex(jSONArray, 0);
                List<String> parseStringArrayAtIndex2 = parseStringArrayAtIndex(jSONArray, 1);
                addInAppProductIds(parseStringArrayAtIndex);
                addSubsProductIds(parseStringArrayAtIndex2);
                getAvailableProducts(this.mInAppProductIds, this.mSubsProductIds);
            } else if ("getPurchases".equals(str)) {
                getPurchases();
            } else if ("consumePurchase".equals(str)) {
                consumePurchase(jSONArray.getString(0));
            } else if ("acknowledgePurchase".equals(str)) {
                acknowledgePurchase(jSONArray.getString(0));
            } else if ("buy".equals(str)) {
                buy(jSONArray);
            } else if ("subscribe".equals(str)) {
                subscribe(jSONArray);
            } else if ("manageSubscriptions".equals(str)) {
                this.cordova.getActivity().startActivity(new Intent("android.intent.action.VIEW", Uri.parse("http://play.google.com/store/account/subscriptions")));
            } else if ("manageBilling".equals(str)) {
                this.cordova.getActivity().startActivity(new Intent("android.intent.action.VIEW", Uri.parse("http://play.google.com/store/paymentmethods")));
            } else if ("launchPriceChangeConfirmationFlow".equals(str)) {
                this.cordova.getActivity().startActivity(new Intent("android.intent.action.VIEW", Uri.parse("http://play.google.com/store/account/subscriptions")));
            } else {
                bool = false;
            }
        } catch (IllegalStateException e) {
            callError(Constants.ERR_UNKNOWN, e.getMessage());
        } catch (JSONException e2) {
            callError(Constants.ERR_UNKNOWN, e2.getMessage());
        }
        return bool.booleanValue();
    }

    private String getPublicKey() {
        int identifier = this.cordova.getActivity().getResources().getIdentifier("billing_key_param", "string", this.cordova.getActivity().getPackageName());
        if (identifier > 0) {
            String string = this.cordova.getActivity().getString(identifier);
            if (string.length() > 0) {
                return string;
            }
        }
        return this.cordova.getActivity().getString(this.cordova.getActivity().getResources().getIdentifier("billing_key", "string", this.cordova.getActivity().getPackageName()));
    }

    private void init() {
        Log.d("CdvPurchase", "init()");
        this.mBillingClient = BillingClient.newBuilder(this.cordova.getActivity()).enablePendingPurchases().setListener(this).build();
        resetLastResult(-1);
        startServiceConnection(new Runnable() {
            @Override
            public final void run() {
                PurchasePlugin.this.m61lambda$init$0$ccfoveaPurchasePlugin();
            }
        }, new Runnable() {
            @Override
            public final void run() {
                PurchasePlugin.this.m62lambda$init$1$ccfoveaPurchasePlugin();
            }
        });
    }

    void m61lambda$init$0$ccfoveaPurchasePlugin() {
        if (getLastResponseCode() == 0) {
            Log.d("CdvPurchase", "init() -> Success");
            callSuccess();
        } else {
            Log.d("CdvPurchase", "init() -> Failed: " + format(getLastResult()));
            callError(Constants.ERR_SETUP, "Setup failed. " + format(getLastResult()));
        }
    }

    void m62lambda$init$1$ccfoveaPurchasePlugin() {
        Log.d("CdvPurchase", "init() -> Failure: " + format(getLastResult()));
        callError(Constants.ERR_SETUP, "Setup failure. " + format(getLastResult()));
    }

    private void getPurchases() {
        Log.d("CdvPurchase", "getPurchases()");
        queryPurchases();
    }

    public void onQueryPurchasesFinished(BillingResult billingResult, List<Purchase> list) {
        try {
            if (billingResult.getResponseCode() == 0) {
                Iterator<Purchase> it = list.iterator();
                while (it.hasNext()) {
                    this.mPurchases.add(0, it.next());
                }
                sendToListener("setPurchases", new JSONObject().put("purchases", toJSON(list)));
                callSuccess(toJSON(list));
                return;
            }
            callError(Constants.ERR_LOAD, "Failed to query purchases: " + billingResult.getResponseCode());
        } catch (Exception e) {
            Log.e("CdvPurchase", "onQueryPurchasesFinished() -> Failed: " + e.getMessage());
            callError(Constants.ERR_LOAD, "Failed to query purchases: " + e.getMessage());
        }
    }

    private JSONArray toJSON(List<Purchase> list) throws JSONException {
        JSONArray jSONArray = new JSONArray();
        Iterator<Purchase> it = list.iterator();
        while (it.hasNext()) {
            jSONArray.put(toJSON(it.next()));
        }
        return jSONArray;
    }

    private JSONObject toJSON(Purchase purchase) throws JSONException {
        return new JSONObject(purchase.getOriginalJson()).put("productIds", new JSONArray((Collection) purchase.getProducts())).put("orderId", purchase.getOrderId()).put("getPurchaseState", purchase.getPurchaseState()).put("developerPayload", purchase.getDeveloperPayload()).put("acknowledged", purchase.isAcknowledged()).put("autoRenewing", purchase.isAutoRenewing()).put(BillingFlowParams.EXTRA_PARAM_KEY_ACCOUNT_ID, purchase.getAccountIdentifiers().getObfuscatedAccountId()).put("profileId", purchase.getAccountIdentifiers().getObfuscatedProfileId()).put("signature", purchase.getSignature()).put("receipt", purchase.getOriginalJson().toString());
    }

    public void queryPurchases() {
        Log.d("CdvPurchase", "queryPurchases()");
        executeServiceRequest(new Runnable() {
            @Override
            public final void run() {
                PurchasePlugin.this.m65lambda$queryPurchases$2$ccfoveaPurchasePlugin();
            }
        });
    }

    void m65lambda$queryPurchases$2$ccfoveaPurchasePlugin() {
        final long currentTimeMillis = System.currentTimeMillis();
        final ArrayList arrayList = new ArrayList();
        this.mInAppResult = null;
        this.mSubsResult = null;
        this.mBillingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder().setProductType("inapp").build(), new PurchasesResponseListener() {
            @Override
            public void onQueryPurchasesResponse(BillingResult billingResult, List<Purchase> list) {
                PurchasePlugin.this.mInAppResult = billingResult;
                Log.i("CdvPurchase", "queryPurchases(INAPP) -> Elapsed time: " + (System.currentTimeMillis() - currentTimeMillis) + "ms");
                if (billingResult.getResponseCode() == 0) {
                    arrayList.addAll(list);
                }
                if (PurchasePlugin.this.mInAppResult != null) {
                    if (PurchasePlugin.this.mSubsResult == null && PurchasePlugin.this.areSubscriptionsSupported()) {
                        return;
                    }
                    PurchasePlugin purchasePlugin = PurchasePlugin.this;
                    purchasePlugin.onQueryPurchasesFinished(purchasePlugin.mInAppResult.getResponseCode() == 0 ? PurchasePlugin.this.mInAppResult : PurchasePlugin.this.mSubsResult, arrayList);
                }
            }
        });
        if (areSubscriptionsSupported()) {
            this.mBillingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder().setProductType("subs").build(), new PurchasesResponseListener() {
                @Override
                public void onQueryPurchasesResponse(BillingResult billingResult, List<Purchase> list) {
                    PurchasePlugin.this.mSubsResult = billingResult;
                    Log.i("CdvPurchase", "queryPurchases(SUBS) -> Elapsed time: " + (System.currentTimeMillis() - currentTimeMillis) + "ms");
                    if (billingResult.getResponseCode() == 0) {
                        arrayList.addAll(list);
                    }
                    if (PurchasePlugin.this.mInAppResult != null) {
                        if (PurchasePlugin.this.mSubsResult == null && PurchasePlugin.this.areSubscriptionsSupported()) {
                            return;
                        }
                        PurchasePlugin purchasePlugin = PurchasePlugin.this;
                        purchasePlugin.onQueryPurchasesFinished(purchasePlugin.mInAppResult.getResponseCode() == 0 ? PurchasePlugin.this.mInAppResult : PurchasePlugin.this.mSubsResult, arrayList);
                    }
                }
            });
        } else {
            Log.i("CdvPurchase", "queryPurchases() -> Subscriptions are not supported, skipped.");
        }
    }

    public boolean areSubscriptionsSupported() {
        BillingResult isFeatureSupported = this.mBillingClient.isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS);
        if (isFeatureSupported.getResponseCode() == 0) {
            return true;
        }
        Log.w("CdvPurchase", "areSubscriptionsSupported() -> Failed: " + format(isFeatureSupported));
        return false;
    }

    public JSONObject productDetailsToJson(ProductDetails productDetails) throws JSONException {
        JSONObject put = new JSONObject().put("productId", productDetails.getProductId()).put("title", productDetails.getTitle()).put(Action.NAME_ATTRIBUTE, productDetails.getName()).put("description", productDetails.getDescription());
        if (productDetails.getProductType().equals("inapp")) {
            ProductDetails.OneTimePurchaseOfferDetails oneTimePurchaseOfferDetails = productDetails.getOneTimePurchaseOfferDetails();
            put.put("product_type", "inapp").put("product_format", "v11.0").put("formatted_price", oneTimePurchaseOfferDetails.getFormattedPrice()).put("price_amount_micros", oneTimePurchaseOfferDetails.getPriceAmountMicros()).put("price_currency_code", oneTimePurchaseOfferDetails.getPriceCurrencyCode());
        } else if (productDetails.getProductType().equals("subs")) {
            put.put("product_format", "v12.0").put("product_type", "subs");
            JSONArray jSONArray = new JSONArray();
            for (ProductDetails.SubscriptionOfferDetails subscriptionOfferDetails : productDetails.getSubscriptionOfferDetails()) {
                JSONObject put2 = new JSONObject().put("base_plan_id", subscriptionOfferDetails.getBasePlanId()).put("offer_id", subscriptionOfferDetails.getOfferId()).put("token", subscriptionOfferDetails.getOfferToken()).put("tags", new JSONArray((Collection) subscriptionOfferDetails.getOfferTags()));
                JSONArray jSONArray2 = new JSONArray();
                if (subscriptionOfferDetails.getPricingPhases() != null) {
                    for (ProductDetails.PricingPhase pricingPhase : subscriptionOfferDetails.getPricingPhases().getPricingPhaseList()) {
                        JSONObject put3 = new JSONObject().put("billing_cycle_count", pricingPhase.getBillingCycleCount()).put("billing_period", pricingPhase.getBillingPeriod()).put("formatted_price", pricingPhase.getFormattedPrice()).put("price_amount_micros", pricingPhase.getPriceAmountMicros()).put("price_currency_code", pricingPhase.getPriceCurrencyCode());
                        if (pricingPhase.getRecurrenceMode() == 2) {
                            put3.put("recurrence_mode", "FINITE_RECURRING");
                        } else if (pricingPhase.getRecurrenceMode() == 1) {
                            put3.put("recurrence_mode", "INFINITE_RECURRING");
                        } else if (pricingPhase.getRecurrenceMode() == 3) {
                            put3.put("recurrence_mode", "NON_RECURRING");
                        }
                        jSONArray2.put(put3);
                    }
                } else {
                    put.put("product_format", EnvironmentCompat.MEDIA_UNKNOWN);
                }
                put2.put("pricing_phases", jSONArray2);
                jSONArray.put(put2);
            }
            put.put("offers", jSONArray);
        }
        return put;
    }

    private void getAvailableProducts(List<String> list, List<String> list2) {
        Log.d("CdvPurchase", "getAvailableProducts()");
        final CallbackContext callbackContext = this.mCallbackContext;
        queryAllProductDetails(list, list2, new ProductDetailsResponseListener() {
            @Override
            public void onProductDetailsResponse(BillingResult billingResult, List<ProductDetails> list3) {
                if (billingResult.getResponseCode() != 0) {
                    Log.d("CdvPurchase", "getAvailableProducts() -> Failed: " + PurchasePlugin.this.format(billingResult));
                    PurchasePlugin.this.callError(callbackContext, Constants.ERR_LOAD, "Failed to load Products, code: " + billingResult.getResponseCode());
                    return;
                }
                JSONArray jSONArray = new JSONArray();
                try {
                    for (ProductDetails productDetails : list3) {
                        Log.d("CdvPurchase", "getAvailableProducts() -> productDetails: " + productDetails.toString());
                        jSONArray.put(PurchasePlugin.this.productDetailsToJson(productDetails));
                    }
                    Log.d("CdvPurchase", "getAvailableProducts() -> Success");
                    PurchasePlugin.this.callSuccess(callbackContext, jSONArray);
                } catch (JSONException e) {
                    Log.d("CdvPurchase", "getAvailableProducts() -> Failed: " + e.getMessage());
                    PurchasePlugin.this.callError(callbackContext, Constants.ERR_LOAD, e.getMessage());
                }
            }
        });
    }

    private Purchase findPurchaseByToken(String str) {
        for (Purchase purchase : this.mPurchases) {
            if (purchase.getPurchaseToken().equals(str)) {
                return purchase;
            }
        }
        return null;
    }

    private Purchase findPurchaseByProductId(String str) {
        for (Purchase purchase : this.mPurchases) {
            if (purchase.getSkus().contains(str)) {
                return purchase;
            }
        }
        return null;
    }

    @Override
    public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> list) {
        try {
            int responseCode = billingResult.getResponseCode();
            if (responseCode != 0 || list == null || list.size() <= 0) {
                if (responseCode == 1) {
                    Log.w("CdvPurchase", "onPurchasesUpdated() -> Cancelled: " + format(billingResult));
                    callError(Constants.ERR_CANCELLED, codeToString(responseCode));
                    return;
                } else {
                    Log.w("CdvPurchase", "onPurchasesUpdated() -> Failed: " + format(billingResult));
                    callError(Constants.ERR_PURCHASE, codeToString(responseCode));
                    return;
                }
            }
            Log.d("CdvPurchase", "onPurchasesUpdated() -> Success");
            Iterator<Purchase> it = list.iterator();
            while (it.hasNext()) {
                this.mPurchases.add(0, it.next());
            }
            callSuccess();
            sendToListener("purchasesUpdated", new JSONObject().put("purchases", toJSON(list)));
        } catch (JSONException e) {
            Log.w("CdvPurchase", "onPurchasesUpdated() -> JSONException " + e.getMessage());
            callError(Constants.ERR_PURCHASE, e.getMessage());
        }
    }

    private String codeToString(int i) {
        switch (i) {
            case -3:
                return "SERVICE_TIMEOUT";
            case -2:
                return "FEATURE_NOT_SUPPORTED";
            case -1:
                return "SERVICE_DISCONNECTED";
            case 0:
                return "OK";
            case 1:
                return "USER_CANCELED";
            case 2:
                return "SERVICE_UNAVAILABLE";
            case 3:
                return "BILLING_UNAVAILABLE";
            case 4:
                return "ITEM_UNAVAILABLE";
            case 5:
                return "DEVELOPER_ERROR";
            case 6:
                return "ERROR";
            case 7:
                return "ITEM_ALREADY_OWNED";
            case 8:
                return "ITEM_NOT_OWNED";
            default:
                return "CODE_" + i;
        }
    }

    private String codeToMessage(int i) {
        switch (i) {
            case -3:
                return "The request has reached the maximum timeout before Google Play responds";
            case -2:
                return "Requested feature is not supported by Play Store on the current device";
            case -1:
                return "Play Store service is not connected now - potentially transient state";
            case 0:
                return "Success";
            case 1:
                return "User pressed back or canceled a dialog";
            case 2:
                return "Network connection is down";
            case 3:
                return "Billing API version is not supported for the type requested";
            case 4:
                return "Requested product is not available for purchase";
            case 5:
                return "Invalid arguments provided to the API";
            case 6:
                return "Fatal error during the API action";
            case 7:
                return "Failure to purchase since item is already owned";
            case 8:
                return "Failure to consume since item is not owned";
            default:
                return "Unknown error code";
        }
    }

    private BillingFlowParams parseBillingFlowParams(JSONArray jSONArray) throws JSONException {
        String str;
        String string;
        String string2 = jSONArray.getString(0);
        JSONObject jSONObject = jSONArray.getJSONObject(1);
        String[] split = string2.split("@", 2);
        if (split.length == 2) {
            string2 = split[0];
            str = split[1];
        } else {
            str = null;
        }
        if (str == null && jSONObject.has("offerToken")) {
            str = jSONObject.getString("offerToken");
        }
        String string3 = jSONObject.has("oldPurchaseToken") ? jSONObject.getString("oldPurchaseToken") : null;
        String string4 = jSONObject.has(BillingFlowParams.EXTRA_PARAM_KEY_ACCOUNT_ID) ? jSONObject.getString(BillingFlowParams.EXTRA_PARAM_KEY_ACCOUNT_ID) : null;
        String string5 = jSONObject.has("profileId") ? jSONObject.getString("profileId") : null;
        ProductDetails productDetails = this.mProductDetails.get(string2);
        if (productDetails == null) {
            Log.d("CdvPurchase", "buy() -> Failed: Product not registered: " + string2);
            callError(Constants.ERR_PURCHASE, "Product not registered: " + string2);
            return null;
        }
        BillingFlowParams.Builder newBuilder = BillingFlowParams.newBuilder();
        List<ProductDetails.SubscriptionOfferDetails> subscriptionOfferDetails = productDetails.getSubscriptionOfferDetails();
        if (str == null && subscriptionOfferDetails != null) {
            str = subscriptionOfferDetails.get(0).getOfferToken();
        }
        ArrayList arrayList = new ArrayList();
        if (str != null) {
            arrayList.add(BillingFlowParams.ProductDetailsParams.newBuilder().setProductDetails(productDetails).setOfferToken(str).build());
            Log.d("CdvPurchase", "Product details id@token: " + split + " === " + string2 + "@" + str + " ... " + productDetails.toString());
        } else {
            arrayList.add(BillingFlowParams.ProductDetailsParams.newBuilder().setProductDetails(productDetails).build());
        }
        BillingFlowParams.SubscriptionUpdateParams.Builder newBuilder2 = BillingFlowParams.SubscriptionUpdateParams.newBuilder();
        Boolean bool = false;
        Log.d("CdvPurchase", "buy() -> setProductDetailsParamsList");
        newBuilder.setProductDetailsParamsList(arrayList);
        if (string3 != null) {
            Log.d("CdvPurchase", "buy() -> setOldSkuPurchaseToken");
            newBuilder2.setOldPurchaseToken(string3);
            bool = true;
        }
        if (string4 != null) {
            Log.d("CdvPurchase", "buy() -> setObfuscatedAccountId");
            newBuilder.setObfuscatedAccountId(string4);
        }
        if (string5 != null) {
            Log.d("CdvPurchase", "buy() -> setObfuscatedProfileId");
            newBuilder.setObfuscatedProfileId(string5);
        }
        if (jSONObject.has("prorationMode")) {
            string = jSONObject.getString("prorationMode");
        } else {
            string = jSONObject.has("replacementMode") ? jSONObject.getString("replacementMode") : null;
        }
        if (string != null) {
            if ("IMMEDIATE_WITH_TIME_PRORATION".equals(string)) {
                newBuilder2.setSubscriptionReplacementMode(1);
            } else if ("IMMEDIATE_AND_CHARGE_PRORATED_PRICE".equals(string)) {
                newBuilder2.setSubscriptionReplacementMode(2);
            } else if ("IMMEDIATE_WITHOUT_PRORATION".equals(string)) {
                newBuilder2.setSubscriptionReplacementMode(3);
            } else if ("DEFERRED".equals(string)) {
                newBuilder2.setSubscriptionReplacementMode(6);
            } else if ("IMMEDIATE_AND_CHARGE_FULL_PRICE".equals(string)) {
                newBuilder2.setSubscriptionReplacementMode(5);
            }
        }
        if (bool.booleanValue()) {
            newBuilder.setSubscriptionUpdateParams(newBuilder2.build());
        }
        return newBuilder.build();
    }

    private void subscribe(JSONArray jSONArray) throws JSONException {
        Log.d("CdvPurchase", "subscribe()");
        if (!areSubscriptionsSupported()) {
            callError(Constants.ERR_PURCHASE, "FEATURE_NOT_SUPPORTED");
        } else {
            initiatePurchaseFlow(parseBillingFlowParams(jSONArray));
        }
    }

    private void buy(JSONArray jSONArray) throws JSONException {
        Log.d("CdvPurchase", "buy()");
        initiatePurchaseFlow(parseBillingFlowParams(jSONArray));
    }

    public void initiatePurchaseFlow(final BillingFlowParams billingFlowParams) {
        Log.d("CdvPurchase", "initiatePurchaseFlow()");
        if (billingFlowParams == null) {
            return;
        }
        executeServiceRequest(new Runnable() {
            @Override
            public final void run() {
                PurchasePlugin.this.m63lambda$initiatePurchaseFlow$3$ccfoveaPurchasePlugin(billingFlowParams);
            }
        });
    }

    void m63lambda$initiatePurchaseFlow$3$ccfoveaPurchasePlugin(BillingFlowParams billingFlowParams) {
        if (getLastResponseCode() != 0) {
            Log.d("CdvPurchase", "initiatePurchaseFlow() -> Failed: Failed to execute service request. " + format(getLastResult()));
            callError(Constants.ERR_COMMUNICATION, "Failed to execute service request. " + format(getLastResult()));
        } else {
            Log.d("CdvPurchase", "initiatePurchaseFlow() -> launchBillingFlow.");
            this.cordova.setActivityResultCallback(this);
            this.mBillingClient.launchBillingFlow(this.cordova.getActivity(), billingFlowParams);
        }
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        try {
            Log.d("CdvPurchase", "onActivityResult(" + i + "," + i2 + "," + intent + ")");
            Log.d("CdvPurchase", "onActivityResult() -> super.onActivityResult()");
            super.onActivityResult(i, i2, intent);
        } catch (Exception e) {
            Log.e("CdvPurchase", "onActivityResult() -> Failed: " + e.getMessage());
            callError(Constants.ERR_UNKNOWN, e.getMessage());
        }
    }

    private void consumePurchase(final String str) throws JSONException {
        Log.d("CdvPurchase", "consumePurchase(" + str + ")");
        if (this.mTokensToBeConsumed.contains(str)) {
            Log.i("CdvPurchase", "consumePurchase() -> Consume already in progress.");
            callError(Constants.ERR_PURCHASE, "ITEM_ALREADY_CONSUMED");
        } else {
            this.mTokensToBeConsumed.add(str);
            executeServiceRequest(new Runnable() {
                @Override
                public final void run() {
                    PurchasePlugin.this.m59lambda$consumePurchase$4$ccfoveaPurchasePlugin(str);
                }
            });
        }
    }

    void m59lambda$consumePurchase$4$ccfoveaPurchasePlugin(String str) {
        this.mBillingClient.consumeAsync(ConsumeParams.newBuilder().setPurchaseToken(str).build(), this);
    }

    private void acknowledgePurchase(final String str) throws JSONException {
        Log.d("CdvPurchase", "acknowledgePurchase(" + str + ")");
        executeServiceRequest(new Runnable() {
            @Override
            public final void run() {
                PurchasePlugin.this.m58lambda$acknowledgePurchase$5$ccfoveaPurchasePlugin(str);
            }
        });
    }

    void m58lambda$acknowledgePurchase$5$ccfoveaPurchasePlugin(String str) {
        this.mBillingClient.acknowledgePurchase(AcknowledgePurchaseParams.newBuilder().setPurchaseToken(str).build(), this);
    }

    @Override
    public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
        if (billingResult.getResponseCode() == 0) {
            Log.d("CdvPurchase", "onAcknowledgePurchaseResponse() -> Success");
            callSuccess();
        } else {
            Log.d("CdvPurchase", "onAcknowledgePurchaseResponse() -> Failed: " + format(billingResult));
            callError(Constants.ERR_FINISH, format(billingResult));
        }
    }

    public void onNewIntent(Intent intent) {
        Log.d("CdvPurchase", "onNewIntent()");
    }

    public void onStop() {
        Log.d("CdvPurchase", "onStop()");
    }

    public void onStart() {
        Log.d("CdvPurchase", "onStart()");
        if (this.mBillingClient == null || Calendar.getInstance().getTimeInMillis() - this.mLastQueryOnStart <= 86400000) {
            return;
        }
        this.mLastQueryOnStart = Calendar.getInstance().getTimeInMillis();
        queryPurchases();
    }

    @Override
    public void onConsumeResponse(BillingResult billingResult, String str) {
        try {
            Log.d("CdvPurchase", "onConsumeResponse()");
            if (billingResult.getResponseCode() != 0) {
                Log.d("CdvPurchase", billingResult.getDebugMessage());
                callError(Constants.ERR_FINISH, billingResult.getDebugMessage());
            } else {
                this.mTokensToBeConsumed.remove(str);
                Purchase findPurchaseByToken = findPurchaseByToken(str);
                Log.d("CdvPurchase", "onConsumeResponse() -> Success");
                sendToListener("purchaseConsumed", new JSONObject().put(ProductAction.ACTION_PURCHASE, toJSON(findPurchaseByToken)));
                callSuccess();
            }
        } catch (JSONException e) {
            Log.d("CdvPurchase", "onConsumeResponse() -> Failed: " + e.getMessage());
            callError(Constants.ERR_UNKNOWN, e.getMessage());
        }
    }

    public void onDestroy() {
        BillingClient billingClient = this.mBillingClient;
        if (billingClient != null && billingClient.isReady()) {
            this.mBillingClient.endConnection();
        }
        super.onDestroy();
    }

    private void queryAllProductDetails(List<String> list, List<String> list2, final ProductDetailsResponseListener productDetailsResponseListener) {
        Log.d("CdvPurchase", "queryAllProductDetails()");
        final ArrayList arrayList = new ArrayList();
        final int i = (list2.size() > 0 ? 1 : 0) + (list.size() <= 0 ? 0 : 1);
        this.nProductDetailsQuerySuccessful = 0;
        ProductDetailsResponseListener productDetailsResponseListener2 = new ProductDetailsResponseListener() {
            @Override
            public void onProductDetailsResponse(BillingResult billingResult, List<ProductDetails> list3) {
                PurchasePlugin.this.mBillingClientResult = billingResult;
                if (billingResult.getResponseCode() != 0) {
                    Log.w("CdvPurchase", "queryAllProductDetails() -> Failed: Unsuccessful query. " + PurchasePlugin.this.format(billingResult));
                    PurchasePlugin.this.callError(Constants.ERR_LOAD, "Error. " + PurchasePlugin.this.format(billingResult));
                    return;
                }
                if (list3 != null && list3.size() > 0) {
                    for (ProductDetails productDetails : list3) {
                        Log.d("CdvPurchase", "queryAllProductDetails() -> ProductDetails: Title: " + productDetails.getTitle());
                        PurchasePlugin.this.mProductDetails.put(productDetails.getProductId(), productDetails);
                        arrayList.add(productDetails);
                    }
                } else {
                    Log.w("CdvPurchase", "queryAllProductDetails() -> Query returned nothing.");
                }
                PurchasePlugin.access$708(PurchasePlugin.this);
                if (PurchasePlugin.this.nProductDetailsQuerySuccessful != i || productDetailsResponseListener == null) {
                    return;
                }
                Log.d("CdvPurchase", "queryAllProductDetails() -> Calling listener.");
                productDetailsResponseListener.onProductDetailsResponse(billingResult, arrayList);
            }
        };
        ArrayList arrayList2 = new ArrayList();
        for (int i2 = 0; i2 < list2.size(); i2++) {
            arrayList2.add(QueryProductDetailsParams.Product.newBuilder().setProductId(list2.get(i2)).setProductType("subs").build());
        }
        ArrayList arrayList3 = new ArrayList();
        for (int i3 = 0; i3 < list.size(); i3++) {
            arrayList3.add(QueryProductDetailsParams.Product.newBuilder().setProductId(list.get(i3)).setProductType("inapp").build());
        }
        if (arrayList2.size() > 0) {
            Log.d("CdvPurchase", "queryAllProductDetails() -> Query SUBS.");
            queryProductDetailsAsync(arrayList2, productDetailsResponseListener2);
        }
        if (arrayList3.size() > 0) {
            Log.d("CdvPurchase", "queryAllProductDetails() -> Query INAPP.");
            queryProductDetailsAsync(arrayList3, productDetailsResponseListener2);
        }
        if (arrayList3.size() == 0 && arrayList2.size() == 0) {
            Log.d("CdvPurchase", "queryAllProductDetails() -> Calling listener (0 requests).");
            if (productDetailsResponseListener != null) {
                productDetailsResponseListener.onProductDetailsResponse(getLastResult(), arrayList);
            }
        }
    }

    public void queryProductDetailsAsync(final List<QueryProductDetailsParams.Product> list, final ProductDetailsResponseListener productDetailsResponseListener) {
        Log.d("CdvPurchase", "queryProductDetailsAsync()");
        executeServiceRequest(new Runnable() {
            @Override
            public final void run() {
                PurchasePlugin.this.m64lambda$queryProductDetailsAsync$6$ccfoveaPurchasePlugin(productDetailsResponseListener, list);
            }
        });
    }

    void m64lambda$queryProductDetailsAsync$6$ccfoveaPurchasePlugin(ProductDetailsResponseListener productDetailsResponseListener, List list) {
        if (getLastResponseCode() != 0) {
            Log.d("CdvPurchase", "queryProductDetailsAsync() -> Failed: " + format(getLastResult()));
            productDetailsResponseListener.onProductDetailsResponse(getLastResult(), null);
        } else {
            Log.d("CdvPurchase", "queryProductDetailsAsync() -> Success");
            QueryProductDetailsParams.Builder newBuilder = QueryProductDetailsParams.newBuilder();
            newBuilder.setProductList(list);
            this.mBillingClient.queryProductDetailsAsync(newBuilder.build(), productDetailsResponseListener);
        }
    }

    private void callSuccess() {
        CallbackContext callbackContext = this.mCallbackContext;
        if (callbackContext == null) {
            return;
        }
        this.mCallbackContext = null;
        callbackContext.success();
    }

    private void callSuccess(JSONArray jSONArray) {
        CallbackContext callbackContext = this.mCallbackContext;
        if (callbackContext == null) {
            return;
        }
        this.mCallbackContext = null;
        callbackContext.success(jSONArray);
    }

    public void callError(int i, String str) {
        Log.d("CdvPurchase", "callError({code:" + i + ", msg:\"" + str + "\")");
        CallbackContext callbackContext = this.mCallbackContext;
        if (callbackContext == null) {
            return;
        }
        this.mCallbackContext = null;
        callbackContext.error(i + "|" + str);
    }

    public void startServiceConnection(final Runnable runnable, final Runnable runnable2) {
        Log.d("CdvPurchase", "startServiceConnection()");
        this.mBillingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                PurchasePlugin.this.mBillingClientResult = billingResult;
                if (billingResult.getResponseCode() == 0) {
                    onBillingConnectionSuccess();
                    Runnable runnable3 = runnable;
                    if (runnable3 != null) {
                        runnable3.run();
                        return;
                    }
                    return;
                }
                onBillingConnectionFailed();
                Runnable runnable4 = runnable2;
                if (runnable4 != null) {
                    runnable4.run();
                }
            }

            private void onBillingConnectionSuccess() {
                Log.d("CdvPurchase", "startServiceConnection() -> Success");
                PurchasePlugin.this.mIsServiceConnected = true;
            }

            private void onBillingConnectionFailed() {
                StringBuilder sb = new StringBuilder("startServiceConnection() -> Failed: ");
                PurchasePlugin purchasePlugin = PurchasePlugin.this;
                Log.d("CdvPurchase", sb.append(purchasePlugin.format(purchasePlugin.getLastResult())).toString());
                PurchasePlugin.this.mIsServiceConnected = false;
            }

            @Override
            public void onBillingServiceDisconnected() {
                Log.d("CdvPurchase", "startServiceConnection() -> Disconnected");
                PurchasePlugin.this.mIsServiceConnected = false;
            }
        });
    }

    private void executeServiceRequest(Runnable runnable) {
        if (this.mIsServiceConnected) {
            Log.d("CdvPurchase", "executeServiceRequest() -> OK");
            resetLastResult(0);
            runnable.run();
        } else {
            Log.d("CdvPurchase", "executeServiceRequest() -> Failed (try again).");
            startServiceConnection(runnable, new Runnable() {
                @Override
                public final void run() {
                    PurchasePlugin.this.m60lambda$executeServiceRequest$7$ccfoveaPurchasePlugin();
                }
            });
        }
    }

    void m60lambda$executeServiceRequest$7$ccfoveaPurchasePlugin() {
        Log.d("CdvPurchase", "executeServiceRequest() -> Failed to reconnect to billing server...");
    }

    private List<String> parseStringArrayAtIndex(JSONArray jSONArray, int i) throws JSONException {
        ArrayList arrayList = new ArrayList();
        if (jSONArray.length() > i) {
            JSONArray jSONArray2 = new JSONArray(jSONArray.getString(i));
            int length = jSONArray2.length();
            for (int i2 = 0; i2 < length; i2++) {
                arrayList.add(jSONArray2.get(i2).toString());
            }
        }
        return arrayList;
    }

    public String format(BillingResult billingResult) {
        String codeToMessage;
        int responseCode = billingResult.getResponseCode();
        if (billingResult.getDebugMessage() != "") {
            codeToMessage = billingResult.getDebugMessage();
        } else {
            codeToMessage = codeToMessage(responseCode);
        }
        return codeToString(responseCode) + ": " + codeToMessage;
    }

    public void callSuccess(CallbackContext callbackContext, JSONArray jSONArray) {
        if (callbackContext != null) {
            callbackContext.success(jSONArray);
        }
    }

    public void callError(CallbackContext callbackContext, int i, String str) {
        if (callbackContext != null) {
            callbackContext.error(i + "|" + str);
        }
    }
}