| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.payments; | 5 package org.chromium.chrome.browser.payments; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.graphics.Bitmap; | 8 import android.graphics.Bitmap; |
| 9 import android.os.Handler; | 9 import android.os.Handler; |
| 10 import android.text.TextUtils; | 10 import android.text.TextUtils; |
| (...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 /** | 534 /** |
| 535 * Sets the total, display line items, and shipping options based on input a
nd returns the | 535 * Sets the total, display line items, and shipping options based on input a
nd returns the |
| 536 * status boolean. That status is true for valid data, false for invalid dat
a. If the input is | 536 * status boolean. That status is true for valid data, false for invalid dat
a. If the input is |
| 537 * invalid, disconnects from the client. Both raw and UI versions of data ar
e updated. | 537 * invalid, disconnects from the client. Both raw and UI versions of data ar
e updated. |
| 538 * | 538 * |
| 539 * @param details The total, line items, and shipping options to parse, vali
date, and save in | 539 * @param details The total, line items, and shipping options to parse, vali
date, and save in |
| 540 * member variables. | 540 * member variables. |
| 541 * @return True if the data is valid. False if the data is invalid. | 541 * @return True if the data is valid. False if the data is invalid. |
| 542 */ | 542 */ |
| 543 private boolean parseAndValidateDetailsOrDisconnectFromClient(PaymentDetails
details) { | 543 private boolean parseAndValidateDetailsOrDisconnectFromClient(PaymentDetails
details) { |
| 544 if (details == null) { | 544 if (!PaymentValidator.validatePaymentDetails(details)) { |
| 545 disconnectFromClientWithDebugMessage("Payment details required"); | 545 disconnectFromClientWithDebugMessage("Invalid payment details"); |
| 546 recordAbortReasonHistogram( | 546 recordAbortReasonHistogram( |
| 547 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE
R); | 547 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE
R); |
| 548 return false; | 548 return false; |
| 549 } | |
| 550 | |
| 551 if (!hasAllPaymentItemFields(details.total)) { | |
| 552 disconnectFromClientWithDebugMessage("Invalid total"); | |
| 553 recordAbortReasonHistogram( | |
| 554 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE
R); | |
| 555 return false; | |
| 556 } | 549 } |
| 557 | 550 |
| 558 String totalCurrency = details.total.amount.currency; | 551 String totalCurrency = details.total.amount.currency; |
| 559 CurrencyStringFormatter formatter = | 552 CurrencyStringFormatter formatter = |
| 560 new CurrencyStringFormatter(totalCurrency, Locale.getDefault()); | 553 new CurrencyStringFormatter(totalCurrency, Locale.getDefault()); |
| 561 | 554 |
| 562 if (!formatter.isValidAmountCurrencyCode(details.total.amount.currency))
{ | |
| 563 disconnectFromClientWithDebugMessage("Invalid total amount currency"
); | |
| 564 recordAbortReasonHistogram( | |
| 565 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE
R); | |
| 566 return false; | |
| 567 } | |
| 568 | |
| 569 if (!formatter.isValidAmountValue(details.total.amount.value) | |
| 570 || details.total.amount.value.startsWith("-")) { | |
| 571 disconnectFromClientWithDebugMessage("Invalid total amount value"); | |
| 572 recordAbortReasonHistogram( | |
| 573 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE
R); | |
| 574 return false; | |
| 575 } | |
| 576 | |
| 577 // Total is never pending. | 555 // Total is never pending. |
| 578 LineItem uiTotal = new LineItem( | 556 LineItem uiTotal = new LineItem( |
| 579 details.total.label, formatter.getFormattedCurrencyCode(), | 557 details.total.label, formatter.getFormattedCurrencyCode(), |
| 580 formatter.format(details.total.amount.value), /* isPending */ fa
lse); | 558 formatter.format(details.total.amount.value), /* isPending */ fa
lse); |
| 581 | 559 |
| 582 List<LineItem> uiLineItems = getValidatedLineItems(details.displayItems,
totalCurrency, | 560 List<LineItem> uiLineItems = getLineItems(details.displayItems, totalCur
rency, formatter); |
| 583 formatter); | |
| 584 if (uiLineItems == null) { | |
| 585 disconnectFromClientWithDebugMessage("Invalid line items"); | |
| 586 recordAbortReasonHistogram( | |
| 587 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE
R); | |
| 588 return false; | |
| 589 } | |
| 590 | 561 |
| 591 mUiShoppingCart = new ShoppingCart(uiTotal, uiLineItems); | 562 mUiShoppingCart = new ShoppingCart(uiTotal, uiLineItems); |
| 592 mRawTotal = details.total; | 563 mRawTotal = details.total; |
| 593 mRawLineItems = Arrays.asList(details.displayItems); | 564 mRawLineItems = Arrays.asList(details.displayItems); |
| 594 | 565 |
| 595 mUiShippingOptions = getValidatedShippingOptions(details.shippingOptions
, totalCurrency, | 566 mUiShippingOptions = getShippingOptions(details.shippingOptions, totalCu
rrency, formatter); |
| 596 formatter); | |
| 597 if (mUiShippingOptions == null) { | |
| 598 disconnectFromClientWithDebugMessage("Invalid shipping options"); | |
| 599 recordAbortReasonHistogram( | |
| 600 PaymentRequestMetrics.ABORT_REASON_INVALID_DATA_FROM_RENDERE
R); | |
| 601 return false; | |
| 602 } | |
| 603 | 567 |
| 604 return true; | 568 return true; |
| 605 } | 569 } |
| 606 | 570 |
| 607 /** | 571 /** |
| 608 * Returns true if all fields in the payment item are non-null and non-empty
. | 572 * Returns true if all fields in the payment item are non-null and non-empty
. |
| 609 * | 573 * |
| 610 * @param item The payment item to examine. | 574 * @param item The payment item to examine. |
| 611 * @return True if all fields are present and non-empty. | 575 * @return True if all fields are present and non-empty. |
| 612 */ | 576 */ |
| 613 private static boolean hasAllPaymentItemFields(PaymentItem item) { | 577 private static boolean hasAllPaymentItemFields(PaymentItem item) { |
| 614 // "label", "currency", and "value" should be non-empty. | 578 // "label", "currency", and "value" should be non-empty. |
| 615 return item != null && !TextUtils.isEmpty(item.label) && item.amount !=
null | 579 return item != null && !TextUtils.isEmpty(item.label) && item.amount !=
null |
| 616 && !TextUtils.isEmpty(item.amount.currency) | 580 && !TextUtils.isEmpty(item.amount.currency) |
| 617 && !TextUtils.isEmpty(item.amount.value); | 581 && !TextUtils.isEmpty(item.amount.value); |
| 618 } | 582 } |
| 619 | 583 |
| 620 /** | 584 /** |
| 621 * Validates a list of payment items and returns their parsed representation
or null if invalid. | 585 * Converts a list of payment items and returns their parsed representation. |
| 622 * | 586 * |
| 623 * @param items The payment items to parse and validate. | 587 * @param items The payment items to parse and validate. |
| 624 * @param totalCurrency The currency code for the total amount of payment. | 588 * @param totalCurrency The currency code for the total amount of payment. |
| 625 * @param formatter A formatter and validator for the currency amount value. | 589 * @param formatter A formatter and validator for the currency amount value. |
| 626 * @return A list of valid line items or null if invalid. | 590 * @return A list of valid line items. |
| 627 */ | 591 */ |
| 628 private static List<LineItem> getValidatedLineItems( | 592 private static List<LineItem> getLineItems( |
| 629 PaymentItem[] items, String totalCurrency, CurrencyStringFormatter f
ormatter) { | 593 PaymentItem[] items, String totalCurrency, CurrencyStringFormatter f
ormatter) { |
| 630 // Line items are optional. | 594 // Line items are optional. |
| 631 if (items == null) return new ArrayList<>(); | 595 if (items == null) return new ArrayList<>(); |
| 632 | 596 |
| 633 List<LineItem> result = new ArrayList<>(items.length); | 597 List<LineItem> result = new ArrayList<>(items.length); |
| 634 for (int i = 0; i < items.length; i++) { | 598 for (int i = 0; i < items.length; i++) { |
| 635 PaymentItem item = items[i]; | 599 PaymentItem item = items[i]; |
| 636 | 600 |
| 637 if (!hasAllPaymentItemFields(item)) return null; | |
| 638 | |
| 639 // All currencies must match. | |
| 640 if (!item.amount.currency.equals(totalCurrency)) return null; | |
| 641 | |
| 642 // Value should be in correct format. | |
| 643 if (!formatter.isValidAmountValue(item.amount.value)) return null; | |
| 644 | |
| 645 result.add(new LineItem( | 601 result.add(new LineItem( |
| 646 item.label, "", formatter.format(item.amount.value), item.pe
nding)); | 602 item.label, "", formatter.format(item.amount.value), item.pe
nding)); |
| 647 } | 603 } |
| 648 | 604 |
| 649 return result; | 605 return result; |
| 650 } | 606 } |
| 651 | 607 |
| 652 /** | 608 /** |
| 653 * Validates a list of shipping options and returns their parsed representat
ion or null if | 609 * Converts a list of shipping options and returns their parsed representati
on. |
| 654 * invalid. | |
| 655 * | 610 * |
| 656 * @param options The raw shipping options to parse and validate. | 611 * @param options The raw shipping options to parse and validate. |
| 657 * @param totalCurrency The currency code for the total amount of payment. | 612 * @param totalCurrency The currency code for the total amount of payment. |
| 658 * @param formatter A formatter and validator for the currency amount value. | 613 * @param formatter A formatter and validator for the currency amount value. |
| 659 * @return The UI representation of the shipping options or null if invalid. | 614 * @return The UI representation of the shipping options. |
| 660 */ | 615 */ |
| 661 private static SectionInformation getValidatedShippingOptions(PaymentShippin
gOption[] options, | 616 private static SectionInformation getShippingOptions(PaymentShippingOption[]
options, |
| 662 String totalCurrency, CurrencyStringFormatter formatter) { | 617 String totalCurrency, CurrencyStringFormatter formatter) { |
| 663 // Shipping options are optional. | 618 // Shipping options are optional. |
| 664 if (options == null || options.length == 0) { | 619 if (options == null || options.length == 0) { |
| 665 return new SectionInformation(PaymentRequestUI.TYPE_SHIPPING_OPTIONS
); | 620 return new SectionInformation(PaymentRequestUI.TYPE_SHIPPING_OPTIONS
); |
| 666 } | 621 } |
| 667 | 622 |
| 668 for (int i = 0; i < options.length; i++) { | |
| 669 PaymentShippingOption option = options[i]; | |
| 670 | |
| 671 // Each "id", "label", "currency", and "value" should be non-empty. | |
| 672 // Each "value" should be a valid amount value. | |
| 673 // Each "currency" should match the total currency. | |
| 674 if (option == null || TextUtils.isEmpty(option.id) || TextUtils.isEm
pty(option.label) | |
| 675 || option.amount == null || TextUtils.isEmpty(option.amount.
currency) | |
| 676 || TextUtils.isEmpty(option.amount.value) | |
| 677 || !totalCurrency.equals(option.amount.currency) | |
| 678 || !formatter.isValidAmountValue(option.amount.value)) { | |
| 679 return null; | |
| 680 } | |
| 681 } | |
| 682 | |
| 683 List<PaymentOption> result = new ArrayList<>(); | 623 List<PaymentOption> result = new ArrayList<>(); |
| 684 int selectedItemIndex = SectionInformation.NO_SELECTION; | 624 int selectedItemIndex = SectionInformation.NO_SELECTION; |
| 685 for (int i = 0; i < options.length; i++) { | 625 for (int i = 0; i < options.length; i++) { |
| 686 PaymentShippingOption option = options[i]; | 626 PaymentShippingOption option = options[i]; |
| 687 result.add(new PaymentOption(option.id, option.label, | 627 result.add(new PaymentOption(option.id, option.label, |
| 688 formatter.format(option.amount.value), null)); | 628 formatter.format(option.amount.value), null)); |
| 689 if (option.selected) selectedItemIndex = i; | 629 if (option.selected) selectedItemIndex = i; |
| 690 } | 630 } |
| 691 | 631 |
| 692 return new SectionInformation(PaymentRequestUI.TYPE_SHIPPING_OPTIONS, se
lectedItemIndex, | 632 return new SectionInformation(PaymentRequestUI.TYPE_SHIPPING_OPTIONS, se
lectedItemIndex, |
| (...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1233 "PaymentRequest.CheckoutFunnel.Aborted", abortReason, | 1173 "PaymentRequest.CheckoutFunnel.Aborted", abortReason, |
| 1234 PaymentRequestMetrics.ABORT_REASON_MAX); | 1174 PaymentRequestMetrics.ABORT_REASON_MAX); |
| 1235 | 1175 |
| 1236 if (abortReason == PaymentRequestMetrics.ABORT_REASON_ABORTED_BY_USER) { | 1176 if (abortReason == PaymentRequestMetrics.ABORT_REASON_ABORTED_BY_USER) { |
| 1237 mJourneyLogger.recordJourneyStatsHistograms("UserAborted"); | 1177 mJourneyLogger.recordJourneyStatsHistograms("UserAborted"); |
| 1238 } else { | 1178 } else { |
| 1239 mJourneyLogger.recordJourneyStatsHistograms("OtherAborted"); | 1179 mJourneyLogger.recordJourneyStatsHistograms("OtherAborted"); |
| 1240 } | 1180 } |
| 1241 } | 1181 } |
| 1242 } | 1182 } |
| OLD | NEW |