| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <set> |
| 6 #include <vector> |
| 7 |
| 8 #include "components/payments/payment_details_validation.h" |
| 9 |
| 10 #include "components/payments/payment_request.mojom.h" |
| 11 #include "components/payments/payments_validators.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Validates ShippingOption or PaymentItem, which happen to have identical |
| 16 // fields, except for "id", which is present only in ShippingOption. |
| 17 template <typename T> |
| 18 bool validateShippingOptionOrPaymentItem(const T& item, |
| 19 std::string* error_message) { |
| 20 if (item->label.empty()) { |
| 21 *error_message = "Item label required"; |
| 22 return false; |
| 23 } |
| 24 |
| 25 if (!item->amount) { |
| 26 *error_message = "Currency amount required"; |
| 27 return false; |
| 28 } |
| 29 |
| 30 if (item->amount->currency.empty()) { |
| 31 *error_message = "Currency code required"; |
| 32 return false; |
| 33 } |
| 34 |
| 35 if (item->amount->value.empty()) { |
| 36 *error_message = "Currency value required"; |
| 37 return false; |
| 38 } |
| 39 |
| 40 if (!payments::PaymentsValidators::isValidCurrencyCodeFormat( |
| 41 item->amount->currency, error_message)) { |
| 42 return false; |
| 43 } |
| 44 |
| 45 if (!payments::PaymentsValidators::isValidAmountFormat( |
| 46 item->amount->value, error_message)) { |
| 47 return false; |
| 48 } |
| 49 return true; |
| 50 } |
| 51 |
| 52 bool validateDisplayItems( |
| 53 const std::vector<blink::mojom::PaymentItemPtr>& items, |
| 54 std::string* error_message) { |
| 55 for (const auto& item : items) { |
| 56 if (!validateShippingOptionOrPaymentItem(item, error_message)) |
| 57 return false; |
| 58 } |
| 59 return true; |
| 60 } |
| 61 |
| 62 bool validateShippingOptions( |
| 63 const std::vector<blink::mojom::PaymentShippingOptionPtr>& options, |
| 64 std::string* error_message) { |
| 65 std::set<std::string> uniqueIds; |
| 66 for (const auto& option : options) { |
| 67 if (option->id.empty()) { |
| 68 *error_message = "ShippingOption id required"; |
| 69 return false; |
| 70 } |
| 71 |
| 72 if (uniqueIds.find(option->id) != uniqueIds.end()) { |
| 73 *error_message = "Duplicate shipping option identifiers are not allowed"; |
| 74 return false; |
| 75 } |
| 76 uniqueIds.insert(option->id); |
| 77 |
| 78 if (!validateShippingOptionOrPaymentItem(option, error_message)) |
| 79 return false; |
| 80 } |
| 81 return true; |
| 82 } |
| 83 |
| 84 bool validatePaymentDetailsModifiers( |
| 85 const std::vector<blink::mojom::PaymentDetailsModifierPtr>& modifiers, |
| 86 std::string* error_message) { |
| 87 if (modifiers.empty()) { |
| 88 *error_message = |
| 89 "Must specify at least one payment details modifier"; |
| 90 return false; |
| 91 } |
| 92 |
| 93 std::set<mojo::String> uniqueMethods; |
| 94 for (const auto& modifier : modifiers) { |
| 95 if (modifier->supported_methods.empty()) { |
| 96 *error_message = "Must specify at least one payment method identifier"; |
| 97 return false; |
| 98 } |
| 99 |
| 100 for (const auto& method : modifier->supported_methods) { |
| 101 if (uniqueMethods.find(method) != uniqueMethods.end()) { |
| 102 *error_message = |
| 103 "Duplicate payment method identifiers are not allowed"; |
| 104 return false; |
| 105 } |
| 106 uniqueMethods.insert(method); |
| 107 } |
| 108 |
| 109 if (modifier->total) { |
| 110 if (!validateShippingOptionOrPaymentItem(modifier->total, error_message)) |
| 111 return false; |
| 112 |
| 113 if (modifier->total->amount->value[0] == '-') { |
| 114 *error_message = "Total amount value should be non-negative"; |
| 115 return false; |
| 116 } |
| 117 } |
| 118 |
| 119 if (modifier->additional_display_items.size()) { |
| 120 if (!validateDisplayItems( |
| 121 modifier->additional_display_items, error_message)) { |
| 122 return false; |
| 123 } |
| 124 } |
| 125 } |
| 126 return true; |
| 127 } |
| 128 |
| 129 } // namespace |
| 130 |
| 131 namespace payments { |
| 132 |
| 133 bool validatePaymentDetails(const blink::mojom::PaymentDetailsPtr& details, |
| 134 std::string* error_message) { |
| 135 if (details->total.is_null()) { |
| 136 *error_message = "Must specify total"; |
| 137 return false; |
| 138 } |
| 139 |
| 140 if (!validateShippingOptionOrPaymentItem(details->total, error_message)) |
| 141 return false; |
| 142 |
| 143 if (details->total->amount->value[0] == '-') { |
| 144 *error_message = "Total amount value should be non-negative"; |
| 145 return false; |
| 146 } |
| 147 |
| 148 if (details->display_items.size()) { |
| 149 if (!validateDisplayItems(details->display_items, error_message)) |
| 150 return false; |
| 151 } |
| 152 |
| 153 if (details->shipping_options.size()) { |
| 154 if (!validateShippingOptions(details->shipping_options, error_message)) |
| 155 return false; |
| 156 } |
| 157 |
| 158 if (details->modifiers.size()) { |
| 159 if (!validatePaymentDetailsModifiers(details->modifiers, error_message)) |
| 160 return false; |
| 161 } |
| 162 return true; |
| 163 } |
| 164 |
| 165 } // namespace payments |
| OLD | NEW |