<commit_before>package com.braintreepayments.api;

import android.app.Activity;
import android.content.Intent;

import com.braintreepayments.api.exceptions.BraintreeException;
import com.braintreepayments.api.exceptions.ConfigurationException;
import com.braintreepayments.api.interfaces.ConfigurationListener;
import com.braintreepayments.api.interfaces.PaymentMethodNonceCallback;
import com.braintreepayments.api.models.BraintreeRequestCodes;
import com.braintreepayments.api.models.Configuration;
import com.braintreepayments.api.models.PaymentMethodNonce;
import com.braintreepayments.api.models.VisaCheckoutConfiguration;
import com.braintreepayments.api.models.VisaCheckoutPaymentBuilder;
import com.visa.checkout.VisaLibrary;
import com.visa.checkout.VisaMcomLibrary;
import com.visa.checkout.VisaMerchantInfo;
import com.visa.checkout.VisaMerchantInfo.MerchantDataLevel;
import com.visa.checkout.VisaPaymentInfo;
import com.visa.checkout.VisaPaymentSummary;
import com.visa.checkout.utils.VisaEnvironmentConfig;

public class VisaCheckout {
    public static void createVisaCheckoutLibrary(final BraintreeFragment braintreeFragment) {
        braintreeFragment.waitForConfiguration(new ConfigurationListener() {
            @Override
            public void onConfigurationFetched(Configuration configuration) {
                VisaCheckoutConfiguration visaCheckoutConfiguration = configuration.getVisaCheckout();

                if (!visaCheckoutConfiguration.isEnabled()) {
                    braintreeFragment.postCallback(new ConfigurationException("Visa Checkout is not enabled."));
                    return;
                }

                VisaEnvironmentConfig visaEnvironmentConfig = VisaEnvironmentConfig.SANDBOX;

                if ("production".equals(configuration.getEnvironment())) {
                    visaEnvironmentConfig = VisaEnvironmentConfig.PRODUCTION;
                }

                visaEnvironmentConfig.setMerchantApiKey(configuration.getVisaCheckout().getApiKey());
                visaEnvironmentConfig.setVisaCheckoutRequestCode(BraintreeRequestCodes.VISA_CHECKOUT);

                VisaMcomLibrary visaMcomLibrary = VisaMcomLibrary.getLibrary(braintreeFragment.getActivity(),
                        visaEnvironmentConfig);
                BraintreeVisaCheckoutResultActivity.sVisaEnvironmentConfig = visaEnvironmentConfig;
                braintreeFragment.postVisaCheckoutLibraryCallback(visaMcomLibrary);
            }
        });

    }

    public static void authorize(final BraintreeFragment braintreeFragment, final VisaMcomLibrary visaMcomLibrary, final VisaPaymentInfo visaPaymentInfo) {
        braintreeFragment.waitForConfiguration(new ConfigurationListener() {

            @Override
            public void onConfigurationFetched(Configuration configuration) {
                VisaMerchantInfo visaMerchantInfo = visaPaymentInfo.getVisaMerchantInfo();
                if (visaMerchantInfo == null) {
                    visaMerchantInfo = new VisaMerchantInfo();
                }

                visaMerchantInfo.setMerchantApiKey(configuration.getVisaCheckout().getApiKey());
                visaMerchantInfo.setDataLevel(MerchantDataLevel.FULL);

                visaPaymentInfo.setVisaMerchantInfo(visaMerchantInfo);
                visaPaymentInfo.setExternalClientId(configuration.getVisaCheckout().getExternalClientId());

                BraintreeVisaCheckoutResultActivity.sVisaPaymentInfo = visaPaymentInfo;

                Intent visaCheckoutResultActivity = new Intent(braintreeFragment.getActivity(),
                        BraintreeVisaCheckoutResultActivity.class);
                braintreeFragment.startActivityForResult(visaCheckoutResultActivity,
                        BraintreeRequestCodes.VISA_CHECKOUT);
            }
        });
    }

    static void onActivityResult(BraintreeFragment braintreeFragment, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_CANCELED) {
            braintreeFragment.sendAnalyticsEvent("visacheckout.activityresult.canceled");
        } else if (resultCode == Activity.RESULT_OK && data != null) {
            VisaPaymentSummary visaPaymentSummary = data.getParcelableExtra(VisaLibrary.PAYMENT_SUMMARY);
            tokenize(braintreeFragment, visaPaymentSummary);
            braintreeFragment.sendAnalyticsEvent("visacheckout.activityresult.ok");
        } else {
            braintreeFragment.postCallback(new BraintreeException("Visa Checkout responded with resultCode=" + resultCode));
            braintreeFragment.sendAnalyticsEvent("visacheckout.activityresult.failed");
        }
    }

    static void tokenize(final BraintreeFragment braintreeFragment, final VisaPaymentSummary visaPaymentSummary) {
        braintreeFragment.waitForConfiguration(new ConfigurationListener() {
            @Override
            public void onConfigurationFetched(Configuration configuration) {
                TokenizationClient.tokenize(braintreeFragment, new VisaCheckoutPaymentBuilder(visaPaymentSummary),
                        new PaymentMethodNonceCallback() {
                            @Override
                            public void success(PaymentMethodNonce paymentMethodNonce) {
                                braintreeFragment.postCallback(paymentMethodNonce);
                                braintreeFragment.sendAnalyticsEvent("visacheckout.tokenize.succeeded");
                            }

                            @Override
                            public void failure(Exception exception) {
                                braintreeFragment.postCallback(exception);
                                braintreeFragment.sendAnalyticsEvent("visacheckout.tokenize.failed");
                            }
                        });<commit_msg>Cancel callback when Visa Checkout cancels<commit_after>package com.braintreepayments.api;

import android.app.Activity;
import android.content.Intent;

import com.braintreepayments.api.exceptions.BraintreeException;
import com.braintreepayments.api.exceptions.ConfigurationException;
import com.braintreepayments.api.interfaces.ConfigurationListener;
import com.braintreepayments.api.interfaces.PaymentMethodNonceCallback;
import com.braintreepayments.api.models.BraintreeRequestCodes;
import com.braintreepayments.api.models.Configuration;
import com.braintreepayments.api.models.PaymentMethodNonce;
import com.braintreepayments.api.models.VisaCheckoutConfiguration;
import com.braintreepayments.api.models.VisaCheckoutPaymentBuilder;
import com.visa.checkout.VisaLibrary;
import com.visa.checkout.VisaMcomLibrary;
import com.visa.checkout.VisaMerchantInfo;
import com.visa.checkout.VisaMerchantInfo.MerchantDataLevel;
import com.visa.checkout.VisaPaymentInfo;
import com.visa.checkout.VisaPaymentSummary;
import com.visa.checkout.utils.VisaEnvironmentConfig;

public class VisaCheckout {
    public static void createVisaCheckoutLibrary(final BraintreeFragment braintreeFragment) {
        braintreeFragment.waitForConfiguration(new ConfigurationListener() {
            @Override
            public void onConfigurationFetched(Configuration configuration) {
                VisaCheckoutConfiguration visaCheckoutConfiguration = configuration.getVisaCheckout();

                if (!visaCheckoutConfiguration.isEnabled()) {
                    braintreeFragment.postCallback(new ConfigurationException("Visa Checkout is not enabled."));
                    return;
                }

                VisaEnvironmentConfig visaEnvironmentConfig = VisaEnvironmentConfig.SANDBOX;

                if ("production".equals(configuration.getEnvironment())) {
                    visaEnvironmentConfig = VisaEnvironmentConfig.PRODUCTION;
                }

                visaEnvironmentConfig.setMerchantApiKey(configuration.getVisaCheckout().getApiKey());
                visaEnvironmentConfig.setVisaCheckoutRequestCode(BraintreeRequestCodes.VISA_CHECKOUT);

                VisaMcomLibrary visaMcomLibrary = VisaMcomLibrary.getLibrary(braintreeFragment.getActivity(),
                        visaEnvironmentConfig);
                BraintreeVisaCheckoutResultActivity.sVisaEnvironmentConfig = visaEnvironmentConfig;
                braintreeFragment.postVisaCheckoutLibraryCallback(visaMcomLibrary);
            }
        });

    }

    public static void authorize(final BraintreeFragment braintreeFragment, final VisaMcomLibrary visaMcomLibrary, final VisaPaymentInfo visaPaymentInfo) {
        braintreeFragment.waitForConfiguration(new ConfigurationListener() {

            @Override
            public void onConfigurationFetched(Configuration configuration) {
                VisaMerchantInfo visaMerchantInfo = visaPaymentInfo.getVisaMerchantInfo();
                if (visaMerchantInfo == null) {
                    visaMerchantInfo = new VisaMerchantInfo();
                }

                visaMerchantInfo.setMerchantApiKey(configuration.getVisaCheckout().getApiKey());
                visaMerchantInfo.setDataLevel(MerchantDataLevel.FULL);

                visaPaymentInfo.setVisaMerchantInfo(visaMerchantInfo);
                visaPaymentInfo.setExternalClientId(configuration.getVisaCheckout().getExternalClientId());

                BraintreeVisaCheckoutResultActivity.sVisaPaymentInfo = visaPaymentInfo;

                Intent visaCheckoutResultActivity = new Intent(braintreeFragment.getActivity(),
                        BraintreeVisaCheckoutResultActivity.class);
                braintreeFragment.startActivityForResult(visaCheckoutResultActivity,
                        BraintreeRequestCodes.VISA_CHECKOUT);
            }
        });
    }

    static void onActivityResult(BraintreeFragment braintreeFragment, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_CANCELED) {
            braintreeFragment.postCancelCallback(BraintreeRequestCodes.VISA_CHECKOUT);
            braintreeFragment.sendAnalyticsEvent("visacheckout.activityresult.canceled");
        } else if (resultCode == Activity.RESULT_OK && data != null) {
            VisaPaymentSummary visaPaymentSummary = data.getParcelableExtra(VisaLibrary.PAYMENT_SUMMARY);
            tokenize(braintreeFragment, visaPaymentSummary);
            braintreeFragment.sendAnalyticsEvent("visacheckout.activityresult.ok");
        } else {
            braintreeFragment.postCallback(new BraintreeException("Visa Checkout responded with resultCode=" + resultCode));
            braintreeFragment.sendAnalyticsEvent("visacheckout.activityresult.failed");
        }
    }

    static void tokenize(final BraintreeFragment braintreeFragment, final VisaPaymentSummary visaPaymentSummary) {
        braintreeFragment.waitForConfiguration(new ConfigurationListener() {
            @Override
            public void onConfigurationFetched(Configuration configuration) {
                TokenizationClient.tokenize(braintreeFragment, new VisaCheckoutPaymentBuilder(visaPaymentSummary),
                        new PaymentMethodNonceCallback() {
                            @Override
                            public void success(PaymentMethodNonce paymentMethodNonce) {
                                braintreeFragment.postCallback(paymentMethodNonce);
                                braintreeFragment.sendAnalyticsEvent("visacheckout.tokenize.succeeded");
                            }

                            @Override
                            public void failure(Exception exception) {
                                braintreeFragment.postCallback(exception);
                                braintreeFragment.sendAnalyticsEvent("visacheckout.tokenize.failed");
                            }
                        });
{'fraction_non_alphanumeric': 0.06633174386920981, 'fraction_numerical': 0.0, 'mean_word_length': 2.8929400066291016, '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': '1373165', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2848, 'n_words': 386}