Search in sources :

Example 1 with SectionInformation

use of org.chromium.chrome.browser.payments.ui.SectionInformation 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 requestPayerName = options != null && options.requestPayerName;
    boolean requestPayerPhone = options != null && options.requestPayerPhone;
    boolean requestPayerEmail = options != null && options.requestPayerEmail;
    List<AutofillProfile> profiles = null;
    if (requestShipping || requestPayerName || 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())) {
                addresses.add(new AutofillAddress(mContext, profile));
            }
        }
        // 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 (requestPayerName || requestPayerPhone || requestPayerEmail) {
        Set<String> uniqueContactInfos = new HashSet<>();
        mContactEditor = new ContactEditor(requestPayerName, requestPayerPhone, requestPayerEmail);
        List<AutofillContact> contacts = new ArrayList<>();
        for (int i = 0; i < profiles.size(); i++) {
            AutofillProfile profile = profiles.get(i);
            String name = requestPayerName && !TextUtils.isEmpty(profile.getFullName()) ? profile.getFullName() : null;
            String phone = requestPayerPhone && !TextUtils.isEmpty(profile.getPhoneNumber()) ? profile.getPhoneNumber() : null;
            String email = requestPayerEmail && !TextUtils.isEmpty(profile.getEmailAddress()) ? profile.getEmailAddress() : null;
            mContactEditor.addPayerNameIfValid(name);
            mContactEditor.addPhoneNumberIfValid(phone);
            mContactEditor.addEmailAddressIfValid(email);
            if (name != null || phone != null || email != null) {
                // Different profiles can have identical contact info. Do not add the same
                // contact info to the list twice.
                String uniqueContactInfo = name + phone + email;
                if (!uniqueContactInfos.contains(uniqueContactInfo)) {
                    uniqueContactInfos.add(uniqueContactInfo);
                    boolean isComplete = mContactEditor.isContactInformationComplete(name, phone, email);
                    contacts.add(new AutofillContact(profile, name, 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, requestPayerName || requestPayerPhone || requestPayerEmail, mMerchantSupportsAutofillPaymentInstruments, mMerchantName, mOrigin, new ShippingStrings(options == null ? PaymentShippingType.SHIPPING : options.shippingType));
    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, requestPayerName);
}
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)

Example 2 with SectionInformation

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

the class PaymentRequestImpl method getShippingOptions.

/**
 * Converts a list of shipping options and returns their parsed representation.
 *
 * @param options The raw shipping options to parse and validate.
 * @param totalCurrency The currency code for the total amount of payment.
 * @param formatter A formatter and validator for the currency amount value.
 * @return The UI representation of the shipping options.
 */
private static SectionInformation getShippingOptions(PaymentShippingOption[] options, String totalCurrency, CurrencyStringFormatter formatter) {
    // Shipping options are optional.
    if (options == null || options.length == 0) {
        return new SectionInformation(PaymentRequestUI.TYPE_SHIPPING_OPTIONS);
    }
    List<PaymentOption> result = new ArrayList<>();
    int selectedItemIndex = SectionInformation.NO_SELECTION;
    for (int i = 0; i < options.length; i++) {
        PaymentShippingOption option = options[i];
        result.add(new PaymentOption(option.id, option.label, formatter.format(option.amount.value), null));
        if (option.selected)
            selectedItemIndex = i;
    }
    return new SectionInformation(PaymentRequestUI.TYPE_SHIPPING_OPTIONS, selectedItemIndex, result);
}
Also used : SectionInformation(org.chromium.chrome.browser.payments.ui.SectionInformation) ArrayList(java.util.ArrayList) PaymentOption(org.chromium.chrome.browser.payments.ui.PaymentOption) PaymentShippingOption(org.chromium.payments.mojom.PaymentShippingOption)

Example 3 with SectionInformation

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

the class PaymentRequestImpl method getValidatedShippingOptions.

/**
     * Validates a list of shipping options and returns their parsed representation or null if
     * invalid.
     *
     * @param options The raw shipping options to parse and validate.
     * @param totalCurrency The currency code for the total amount of payment.
     * @param formatter A formatter and validator for the currency amount value.
     * @return The UI representation of the shipping options or null if invalid.
     */
private static SectionInformation getValidatedShippingOptions(PaymentShippingOption[] options, String totalCurrency, CurrencyStringFormatter formatter) {
    // Shipping options are optional.
    if (options == null || options.length == 0) {
        return new SectionInformation(PaymentRequestUI.TYPE_SHIPPING_OPTIONS);
    }
    for (int i = 0; i < options.length; i++) {
        PaymentShippingOption option = options[i];
        // Each "currency" should match the total currency.
        if (option == null || TextUtils.isEmpty(option.id) || TextUtils.isEmpty(option.label) || option.amount == null || TextUtils.isEmpty(option.amount.currency) || TextUtils.isEmpty(option.amount.value) || !totalCurrency.equals(option.amount.currency) || !formatter.isValidAmountValue(option.amount.value)) {
            return null;
        }
    }
    List<PaymentOption> result = new ArrayList<>();
    int selectedItemIndex = SectionInformation.NO_SELECTION;
    for (int i = 0; i < options.length; i++) {
        PaymentShippingOption option = options[i];
        result.add(new PaymentOption(option.id, option.label, formatter.format(option.amount.value), PaymentOption.NO_ICON));
        if (option.selected)
            selectedItemIndex = i;
    }
    return new SectionInformation(PaymentRequestUI.TYPE_SHIPPING_OPTIONS, selectedItemIndex, result);
}
Also used : SectionInformation(org.chromium.chrome.browser.payments.ui.SectionInformation) ArrayList(java.util.ArrayList) PaymentOption(org.chromium.chrome.browser.payments.ui.PaymentOption) PaymentShippingOption(org.chromium.payments.mojom.PaymentShippingOption)

Example 4 with SectionInformation

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

the class PaymentRequestImpl method onInstrumentsReady.

/**
 * Called after retrieving the list of payment instruments in an app.
 */
@Override
public void onInstrumentsReady(PaymentApp app, List<PaymentInstrument> instruments) {
    if (mClient == null)
        return;
    mPendingApps.remove(app);
    // all apps have responded.
    if (instruments != null) {
        for (int i = 0; i < instruments.size(); i++) {
            PaymentInstrument instrument = instruments.get(i);
            if (mMethodData.containsKey(instrument.getInstrumentMethodName())) {
                addPendingInstrument(instrument);
            } else {
                instrument.dismissInstrument();
            }
        }
    }
    // Some payment apps still have not responded. Continue waiting for them.
    if (!mPendingApps.isEmpty())
        return;
    if (disconnectIfNoPaymentMethodsSupported())
        return;
    // Load the validation rules for each unique region code in the credit card billing
    // addresses and check for validity.
    Set<String> uniqueCountryCodes = new HashSet<>();
    for (int i = 0; i < mPendingAutofillInstruments.size(); ++i) {
        assert mPendingAutofillInstruments.get(i) instanceof AutofillPaymentInstrument;
        AutofillPaymentInstrument creditCard = (AutofillPaymentInstrument) mPendingAutofillInstruments.get(i);
        String countryCode = AutofillAddress.getCountryCode(creditCard.getBillingAddress());
        if (!uniqueCountryCodes.contains(countryCode)) {
            uniqueCountryCodes.add(countryCode);
            PersonalDataManager.getInstance().loadRulesForRegion(countryCode);
        }
        // If there's a card on file with a valid number and a name, then
        // PaymentRequest.canMakePayment() returns true.
        mCanMakePayment |= creditCard.isValid();
    }
    // List order:
    // > Non-autofill instruments.
    // > Complete autofill instruments.
    // > Incomplete autofill instruments.
    Collections.sort(mPendingAutofillInstruments, COMPLETENESS_COMPARATOR);
    mPendingInstruments.addAll(mPendingAutofillInstruments);
    // Log the number of suggested credit cards.
    mJourneyLogger.setNumberOfSuggestionsShown(PaymentRequestJourneyLogger.SECTION_CREDIT_CARDS, mPendingAutofillInstruments.size());
    mPendingAutofillInstruments.clear();
    // Possibly pre-select the first instrument on the list.
    int selection = SectionInformation.NO_SELECTION;
    if (!mPendingInstruments.isEmpty()) {
        PaymentInstrument first = mPendingInstruments.get(0);
        if (first instanceof AutofillPaymentInstrument) {
            AutofillPaymentInstrument creditCard = (AutofillPaymentInstrument) first;
            if (creditCard.isComplete())
                selection = 0;
        } else {
            // If a payment app is available, then PaymentRequest.canMakePayment() returns true.
            mCanMakePayment = true;
            selection = 0;
        }
    }
    CanMakePaymentQuery query = sCanMakePaymentQueries.get(mOrigin);
    if (query != null)
        query.setResponse(mCanMakePayment);
    // The list of payment instruments is ready to display.
    mPaymentMethodsSection = new SectionInformation(PaymentRequestUI.TYPE_PAYMENT_METHODS, selection, mPendingInstruments);
    mPendingInstruments.clear();
    // UI has requested the full list of payment instruments. Provide it now.
    if (mPaymentInformationCallback != null)
        providePaymentInformation();
}
Also used : SectionInformation(org.chromium.chrome.browser.payments.ui.SectionInformation) HashSet(java.util.HashSet)

Aggregations

SectionInformation (org.chromium.chrome.browser.payments.ui.SectionInformation)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 PaymentOption (org.chromium.chrome.browser.payments.ui.PaymentOption)2 PaymentShippingOption (org.chromium.payments.mojom.PaymentShippingOption)2 AutofillProfile (org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile)1 PaymentRequestUI (org.chromium.chrome.browser.payments.ui.PaymentRequestUI)1