Search in sources :

Example 1 with PaymentShippingOption

use of org.chromium.payments.mojom.PaymentShippingOption 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)

Aggregations

ArrayList (java.util.ArrayList)1 PaymentOption (org.chromium.chrome.browser.payments.ui.PaymentOption)1 SectionInformation (org.chromium.chrome.browser.payments.ui.SectionInformation)1 PaymentShippingOption (org.chromium.payments.mojom.PaymentShippingOption)1