Search in sources :

Example 1 with PaymentRequestUI

use of org.chromium.chrome.browser.payments.ui.PaymentRequestUI in project AndroidChromium by JackyAndroid.

the class PaymentRequestImpl method init.

/**
     * Called by the merchant website to initialize the payment request data.
     */
@Override
public void init(PaymentRequestClient client, PaymentMethodData[] methodData, PaymentDetails details, PaymentOptions options) {
    if (mClient != null || client == null)
        return;
    mClient = client;
    if (mMethodData != null) {
        disconnectFromClientWithDebugMessage("PaymentRequest.show() called more than once.");
        recordAbortReasonHistogram(PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERER);
        return;
    }
    mMethodData = getValidatedMethodData(methodData, mCardEditor);
    if (mMethodData == null) {
        disconnectFromClientWithDebugMessage("Invalid payment methods or data");
        recordAbortReasonHistogram(PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERER);
        return;
    }
    if (!parseAndValidateDetailsOrDisconnectFromClient(details))
        return;
    getMatchingPaymentInstruments();
    boolean requestShipping = options != null && options.requestShipping;
    boolean requestPayerPhone = options != null && options.requestPayerPhone;
    boolean requestPayerEmail = options != null && options.requestPayerEmail;
    List<AutofillProfile> profiles = null;
    if (requestShipping || requestPayerPhone || requestPayerEmail) {
        profiles = PersonalDataManager.getInstance().getProfilesToSuggest(false);
    }
    if (requestShipping) {
        List<AutofillAddress> addresses = new ArrayList<>();
        for (int i = 0; i < profiles.size(); i++) {
            AutofillProfile profile = profiles.get(i);
            mAddressEditor.addPhoneNumberIfValid(profile.getPhoneNumber());
            // Only suggest addresses that have a street address.
            if (!TextUtils.isEmpty(profile.getStreetAddress())) {
                boolean isComplete = mAddressEditor.isProfileComplete(profile);
                addresses.add(new AutofillAddress(profile, isComplete));
            }
        }
        // Suggest complete addresses first.
        Collections.sort(addresses, COMPLETENESS_COMPARATOR);
        // Limit the number of suggestions.
        addresses = addresses.subList(0, Math.min(addresses.size(), SUGGESTIONS_LIMIT));
        // Load the validation rules for each unique region code.
        Set<String> uniqueCountryCodes = new HashSet<>();
        for (int i = 0; i < addresses.size(); ++i) {
            String countryCode = AutofillAddress.getCountryCode(addresses.get(i).getProfile());
            if (!uniqueCountryCodes.contains(countryCode)) {
                uniqueCountryCodes.add(countryCode);
                PersonalDataManager.getInstance().loadRulesForRegion(countryCode);
            }
        }
        // Log the number of suggested shipping addresses.
        mJourneyLogger.setNumberOfSuggestionsShown(PaymentRequestJourneyLogger.SECTION_SHIPPING_ADDRESS, addresses.size());
        // Automatically select the first address if one is complete and if the merchant does
        // not require a shipping address to calculate shipping costs.
        int firstCompleteAddressIndex = SectionInformation.NO_SELECTION;
        if (mUiShippingOptions.getSelectedItem() != null && !addresses.isEmpty() && addresses.get(0).isComplete()) {
            firstCompleteAddressIndex = 0;
        }
        mShippingAddressesSection = new SectionInformation(PaymentRequestUI.TYPE_SHIPPING_ADDRESSES, firstCompleteAddressIndex, addresses);
    }
    if (requestPayerPhone || requestPayerEmail) {
        Set<String> uniqueContactInfos = new HashSet<>();
        mContactEditor = new ContactEditor(requestPayerPhone, requestPayerEmail);
        List<AutofillContact> contacts = new ArrayList<>();
        for (int i = 0; i < profiles.size(); i++) {
            AutofillProfile profile = profiles.get(i);
            String phone = requestPayerPhone && !TextUtils.isEmpty(profile.getPhoneNumber()) ? profile.getPhoneNumber() : null;
            String email = requestPayerEmail && !TextUtils.isEmpty(profile.getEmailAddress()) ? profile.getEmailAddress() : null;
            mContactEditor.addPhoneNumberIfValid(phone);
            mContactEditor.addEmailAddressIfValid(email);
            if (phone != null || email != null) {
                // Different profiles can have identical contact info. Do not add the same
                // contact info to the list twice.
                String uniqueContactInfo = phone + email;
                if (!uniqueContactInfos.contains(uniqueContactInfo)) {
                    uniqueContactInfos.add(uniqueContactInfo);
                    boolean isComplete = mContactEditor.isContactInformationComplete(phone, email);
                    contacts.add(new AutofillContact(profile, phone, email, isComplete));
                }
            }
        }
        // Suggest complete contact infos first.
        Collections.sort(contacts, COMPLETENESS_COMPARATOR);
        // Limit the number of suggestions.
        contacts = contacts.subList(0, Math.min(contacts.size(), SUGGESTIONS_LIMIT));
        // Log the number of suggested contact infos.
        mJourneyLogger.setNumberOfSuggestionsShown(PaymentRequestJourneyLogger.SECTION_CONTACT_INFO, contacts.size());
        // Automatically select the first address if it is complete.
        int firstCompleteContactIndex = SectionInformation.NO_SELECTION;
        if (!contacts.isEmpty() && contacts.get(0).isComplete()) {
            firstCompleteContactIndex = 0;
        }
        mContactSection = new SectionInformation(PaymentRequestUI.TYPE_CONTACT_DETAILS, firstCompleteContactIndex, contacts);
    }
    mUI = new PaymentRequestUI(mContext, this, requestShipping, requestPayerPhone || requestPayerEmail, mMerchantSupportsAutofillPaymentInstruments, mMerchantName, mOrigin);
    if (mFavicon != null)
        mUI.setTitleBitmap(mFavicon);
    mFavicon = null;
    mAddressEditor.setEditorView(mUI.getEditorView());
    mCardEditor.setEditorView(mUI.getCardEditorView());
    if (mContactEditor != null)
        mContactEditor.setEditorView(mUI.getEditorView());
    PaymentRequestMetrics.recordRequestedInformationHistogram(requestPayerEmail, requestPayerPhone, requestShipping);
}
Also used : SectionInformation(org.chromium.chrome.browser.payments.ui.SectionInformation) ArrayList(java.util.ArrayList) AutofillProfile(org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile) PaymentRequestUI(org.chromium.chrome.browser.payments.ui.PaymentRequestUI) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AutofillProfile (org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile)1 PaymentRequestUI (org.chromium.chrome.browser.payments.ui.PaymentRequestUI)1 SectionInformation (org.chromium.chrome.browser.payments.ui.SectionInformation)1