<commit_before>package com.braintreepayments.api;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Base64;

import com.braintreepayments.api.annotations.Beta;
import com.braintreepayments.api.data.BraintreeData;
import com.braintreepayments.api.data.BraintreeEnvironment;
import com.braintreepayments.api.exceptions.AppSwitchNotAvailableException;
import com.braintreepayments.api.exceptions.BraintreeException;
import com.braintreepayments.api.exceptions.ConfigurationException;
import com.braintreepayments.api.exceptions.ErrorWithResponse;
import com.braintreepayments.api.exceptions.ServerException;
import com.braintreepayments.api.internal.HttpRequest;
import com.braintreepayments.api.internal.HttpResponse;
import com.braintreepayments.api.models.AnalyticsRequest;
import com.braintreepayments.api.models.AndroidPayCard;
import com.braintreepayments.api.models.ClientToken;
import com.braintreepayments.api.models.Configuration;
import com.braintreepayments.api.models.PayPalAccount;
import com.braintreepayments.api.models.PayPalAccountBuilder;
import com.braintreepayments.api.models.PaymentMethod;
import com.braintreepayments.api.models.ThreeDSecureLookup;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.wallet.Cart;
import com.google.android.gms.wallet.FullWallet;
import com.google.android.gms.wallet.WalletConstants;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Synchronous communication with the Braintree gateway. Consumed by {@link Braintree}.
 * Unless synchronous behavior is needed, we recommend using {@link Braintree}.
 *
 * @see Braintree
 */
public class BraintreeApi {

    private static final String PAYMENT_METHOD_ENDPOINT = "payment_methods";

    private Context mContext;
    private ClientToken mClientToken;
    private Configuration mConfiguration;
    private HttpRequest mHttpRequest;

    private VenmoAppSwitch mVenmoAppSwitch;
    private BraintreeData mBraintreeData;

    /**
     * @deprecated Interactions should be done using {@link com.braintreepayments.api.Braintree}
     * instead.
     *
     * Initialize a BraintreeApi instance to communicate with Braintree.
     *
     * @param context
     * @param clientTokenString A client token obtained from a Braintree server side SDK.
     */
    public BraintreeApi(Context context, String clientTokenString) {
        Pattern pattern = Pattern.compile("([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)");
        if (pattern.matcher(clientTokenString).matches()) {
            clientTokenString = new String(Base64.decode(clientTokenString, Base64.DEFAULT));
        }

        mClientToken = ClientToken.fromString(clientTokenString);

        mContext = context.getApplicationContext();
        mConfiguration = Configuration.fromJson(clientTokenString);
        mHttpRequest = new HttpRequest(mClientToken.getAuthorizationFingerprint());
        mHttpRequest.setBaseUrl(mConfiguration.getClientApiUrl());

        mBraintreeData = null;
        mVenmoAppSwitch = new VenmoAppSwitch(context, mConfiguration);
    }

    protected BraintreeApi(Context context, ClientToken clientToken) {
        mContext = context.getApplicationContext();
        mClientToken = clientToken;
        mHttpRequest = new HttpRequest(mClientToken.getAuthorizationFingerprint());
    }

    protected BraintreeApi(Context context, String clientTokenString, String configurationString) {
        this(context, ClientToken.fromString(clientTokenString),
                Configuration.fromJson(configurationString),
                new HttpRequest(ClientToken.fromString(clientTokenString).getAuthorizationFingerprint()));
    }

    protected BraintreeApi(Context context, ClientToken clientToken, Configuration configuration,
            HttpRequest requestor) {
        mContext = context.getApplicationContext();
        mClientToken = clientToken;
        mConfiguration = configuration;
        mHttpRequest = requestor;

        mBraintreeData = null;
        mVenmoAppSwitch = new VenmoAppSwitch(mContext, mConfiguration);
    }

    protected boolean isSetup() {
        return mConfiguration != null;
    }

    protected void setup() throws ErrorWithResponse, BraintreeException {
        mConfiguration = getConfiguration();
        mHttpRequest.setBaseUrl(mConfiguration.getClientApiUrl());

        mBraintreeData = null;
        mVenmoAppSwitch = new VenmoAppSwitch(mContext, mConfiguration);
    }

    private Configuration getConfiguration() throws ErrorWithResponse, BraintreeException {
        String configUrl = Uri.parse(mClientToken.getConfigUrl())
                .buildUpon()
                .appendQueryParameter("configVersion", "3")
                .build()
                .toString();
        HttpResponse response = mHttpRequest.get(configUrl);
        return Configuration.fromJson(response.getResponseBody());
    }

    protected String getConfigurationString() {
        if (mConfiguration != null) {
            return new Gson().toJson(mConfiguration);
        } else {
            return null;
        }
    }

    /**
     * @return If PayPal is supported and enabled in the current environment.
     */
    public boolean isPayPalEnabled() {
        return mConfiguration.isPayPalEnabled();
    }

    /**
     * @return If Venmo app switch is supported and enabled in the current environment
     */
    public boolean isVenmoEnabled() {
        return mVenmoAppSwitch.isAvailable();
    }

    public boolean isAndroidPayEnabled() {
        return GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS;
    }

    /**
     * @return If 3D Secure is supported and enabled for the current merchant account
     */
    @Beta
    public boolean isThreeDSecureEnabled() {
        return mConfiguration.isThreeDSecureEnabled();
    }

    /**
     * @return If cvv is required to add a card
     */
    public boolean isCvvChallengePresent() {
        return mConfiguration.isCvvChallengePresent();
    }

    /**
     * @return If postal code is required to add a card
     */<commit_msg>Check configuration if Android Pay is enabled<commit_after>package com.braintreepayments.api;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Base64;

import com.braintreepayments.api.annotations.Beta;
import com.braintreepayments.api.data.BraintreeData;
import com.braintreepayments.api.data.BraintreeEnvironment;
import com.braintreepayments.api.exceptions.AppSwitchNotAvailableException;
import com.braintreepayments.api.exceptions.BraintreeException;
import com.braintreepayments.api.exceptions.ConfigurationException;
import com.braintreepayments.api.exceptions.ErrorWithResponse;
import com.braintreepayments.api.exceptions.ServerException;
import com.braintreepayments.api.internal.HttpRequest;
import com.braintreepayments.api.internal.HttpResponse;
import com.braintreepayments.api.models.AnalyticsRequest;
import com.braintreepayments.api.models.AndroidPayCard;
import com.braintreepayments.api.models.ClientToken;
import com.braintreepayments.api.models.Configuration;
import com.braintreepayments.api.models.PayPalAccount;
import com.braintreepayments.api.models.PayPalAccountBuilder;
import com.braintreepayments.api.models.PaymentMethod;
import com.braintreepayments.api.models.ThreeDSecureLookup;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.wallet.Cart;
import com.google.android.gms.wallet.FullWallet;
import com.google.android.gms.wallet.WalletConstants;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Synchronous communication with the Braintree gateway. Consumed by {@link Braintree}.
 * Unless synchronous behavior is needed, we recommend using {@link Braintree}.
 *
 * @see Braintree
 */
public class BraintreeApi {

    private static final String PAYMENT_METHOD_ENDPOINT = "payment_methods";

    private Context mContext;
    private ClientToken mClientToken;
    private Configuration mConfiguration;
    private HttpRequest mHttpRequest;

    private VenmoAppSwitch mVenmoAppSwitch;
    private BraintreeData mBraintreeData;

    /**
     * @deprecated Interactions should be done using {@link com.braintreepayments.api.Braintree}
     * instead.
     *
     * Initialize a BraintreeApi instance to communicate with Braintree.
     *
     * @param context
     * @param clientTokenString A client token obtained from a Braintree server side SDK.
     */
    public BraintreeApi(Context context, String clientTokenString) {
        Pattern pattern = Pattern.compile("([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)");
        if (pattern.matcher(clientTokenString).matches()) {
            clientTokenString = new String(Base64.decode(clientTokenString, Base64.DEFAULT));
        }

        mClientToken = ClientToken.fromString(clientTokenString);

        mContext = context.getApplicationContext();
        mConfiguration = Configuration.fromJson(clientTokenString);
        mHttpRequest = new HttpRequest(mClientToken.getAuthorizationFingerprint());
        mHttpRequest.setBaseUrl(mConfiguration.getClientApiUrl());

        mBraintreeData = null;
        mVenmoAppSwitch = new VenmoAppSwitch(context, mConfiguration);
    }

    protected BraintreeApi(Context context, ClientToken clientToken) {
        mContext = context.getApplicationContext();
        mClientToken = clientToken;
        mHttpRequest = new HttpRequest(mClientToken.getAuthorizationFingerprint());
    }

    protected BraintreeApi(Context context, String clientTokenString, String configurationString) {
        this(context, ClientToken.fromString(clientTokenString),
                Configuration.fromJson(configurationString),
                new HttpRequest(ClientToken.fromString(clientTokenString).getAuthorizationFingerprint()));
    }

    protected BraintreeApi(Context context, ClientToken clientToken, Configuration configuration,
            HttpRequest requestor) {
        mContext = context.getApplicationContext();
        mClientToken = clientToken;
        mConfiguration = configuration;
        mHttpRequest = requestor;

        mBraintreeData = null;
        mVenmoAppSwitch = new VenmoAppSwitch(mContext, mConfiguration);
    }

    protected boolean isSetup() {
        return mConfiguration != null;
    }

    protected void setup() throws ErrorWithResponse, BraintreeException {
        mConfiguration = getConfiguration();
        mHttpRequest.setBaseUrl(mConfiguration.getClientApiUrl());

        mBraintreeData = null;
        mVenmoAppSwitch = new VenmoAppSwitch(mContext, mConfiguration);
    }

    private Configuration getConfiguration() throws ErrorWithResponse, BraintreeException {
        String configUrl = Uri.parse(mClientToken.getConfigUrl())
                .buildUpon()
                .appendQueryParameter("configVersion", "3")
                .build()
                .toString();
        HttpResponse response = mHttpRequest.get(configUrl);
        return Configuration.fromJson(response.getResponseBody());
    }

    protected String getConfigurationString() {
        if (mConfiguration != null) {
            return new Gson().toJson(mConfiguration);
        } else {
            return null;
        }
    }

    /**
     * @return If PayPal is supported and enabled in the current environment.
     */
    public boolean isPayPalEnabled() {
        return mConfiguration.isPayPalEnabled();
    }

    /**
     * @return If Venmo app switch is supported and enabled in the current environment.
     */
    public boolean isVenmoEnabled() {
        return mVenmoAppSwitch.isAvailable();
    }

    /**
     * @return If Android Pay is enabled.
     */
    public boolean isAndroidPayEnabled() {
        return (mConfiguration.getAndroidPay() != null &&
                mConfiguration.getAndroidPay().getEnabled() &&
                GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS);
    }

    /**
     * @return If 3D Secure is supported and enabled for the current merchant account.
     */
    @Beta
    public boolean isThreeDSecureEnabled() {
        return mConfiguration.isThreeDSecureEnabled();
    }

    /**
     * @return If cvv is required to add a card.
     */
    public boolean isCvvChallengePresent() {
        return mConfiguration.isCvvChallengePresent();
    }

    /**
     * @return If postal code is required to add a card.
     */
{'fraction_non_alphanumeric': 0.08991109393119444, 'fraction_numerical': 0.0030923850019327404, 'mean_word_length': 3.6266094420600856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '679913', 'n_tokens_mistral': 3618, 'n_tokens_neox': 3523, 'n_words': 735}