| 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 "components/payments/payment_details_validation.h" |
| 6 |
| 7 #include <set> |
| 8 #include <vector> |
| 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( |
| 19 const T& item, |
| 20 const blink::mojom::PaymentItemPtr& total, |
| 21 std::string* error_message) { |
| 22 if (item->label.empty()) { |
| 23 *error_message = "Item label required"; |
| 24 return false; |
| 25 } |
| 26 |
| 27 if (!item->amount) { |
| 28 *error_message = "Currency amount required"; |
| 29 return false; |
| 30 } |
| 31 |
| 32 if (item->amount->currency.empty()) { |
| 33 *error_message = "Currency code required"; |
| 34 return false; |
| 35 } |
| 36 |
| 37 if (item->amount->value.empty()) { |
| 38 *error_message = "Currency value required"; |
| 39 return false; |
| 40 } |
| 41 |
| 42 if (item->amount->currency != total->amount->currency) { |
| 43 *error_message = "Currencies must all be equal"; |
| 44 return false; |
| 45 } |
| 46 |
| 47 if (item->amount->currencySystem.has_value() && |
| 48 item->amount->currencySystem.value().empty()) { |
| 49 *error_message = "Currency system can't be empty"; |
| 50 return false; |
| 51 } |
| 52 |
| 53 if (!payments::PaymentsValidators::isValidCurrencyCodeFormat( |
| 54 item->amount->currency, item->amount->currencySystem.has_value() |
| 55 ? item->amount->currencySystem.value() |
| 56 : "", |
| 57 error_message)) { |
| 58 return false; |
| 59 } |
| 60 |
| 61 if (!payments::PaymentsValidators::isValidAmountFormat(item->amount->value, |
| 62 error_message)) { |
| 63 return false; |
| 64 } |
| 65 return true; |
| 66 } |
| 67 |
| 68 bool validateDisplayItems( |
| 69 const std::vector<blink::mojom::PaymentItemPtr>& items, |
| 70 const blink::mojom::PaymentItemPtr& total, |
| 71 std::string* error_message) { |
| 72 for (const auto& item : items) { |
| 73 if (!validateShippingOptionOrPaymentItem(item, total, error_message)) |
| 74 return false; |
| 75 } |
| 76 return true; |
| 77 } |
| 78 |
| 79 bool validateShippingOptions( |
| 80 const std::vector<blink::mojom::PaymentShippingOptionPtr>& options, |
| 81 const blink::mojom::PaymentItemPtr& total, |
| 82 std::string* error_message) { |
| 83 std::set<std::string> uniqueIds; |
| 84 for (const auto& option : options) { |
| 85 if (option->id.empty()) { |
| 86 *error_message = "ShippingOption id required"; |
| 87 return false; |
| 88 } |
| 89 |
| 90 if (uniqueIds.find(option->id) != uniqueIds.end()) { |
| 91 *error_message = "Duplicate shipping option identifiers are not allowed"; |
| 92 return false; |
| 93 } |
| 94 uniqueIds.insert(option->id); |
| 95 |
| 96 if (!validateShippingOptionOrPaymentItem(option, total, error_message)) |
| 97 return false; |
| 98 } |
| 99 return true; |
| 100 } |
| 101 |
| 102 bool validatePaymentDetailsModifiers( |
| 103 const std::vector<blink::mojom::PaymentDetailsModifierPtr>& modifiers, |
| 104 const blink::mojom::PaymentItemPtr& total, |
| 105 std::string* error_message) { |
| 106 if (modifiers.empty()) { |
| 107 *error_message = "Must specify at least one payment details modifier"; |
| 108 return false; |
| 109 } |
| 110 |
| 111 std::set<mojo::String> uniqueMethods; |
| 112 for (const auto& modifier : modifiers) { |
| 113 if (modifier->supported_methods.empty()) { |
| 114 *error_message = "Must specify at least one payment method identifier"; |
| 115 return false; |
| 116 } |
| 117 |
| 118 for (const auto& method : modifier->supported_methods) { |
| 119 if (uniqueMethods.find(method) != uniqueMethods.end()) { |
| 120 *error_message = "Duplicate payment method identifiers are not allowed"; |
| 121 return false; |
| 122 } |
| 123 uniqueMethods.insert(method); |
| 124 } |
| 125 |
| 126 if (modifier->total) { |
| 127 if (!validateShippingOptionOrPaymentItem(modifier->total, total, |
| 128 error_message)) |
| 129 return false; |
| 130 |
| 131 if (modifier->total->amount->value[0] == '-') { |
| 132 *error_message = "Total amount value should be non-negative"; |
| 133 return false; |
| 134 } |
| 135 } |
| 136 |
| 137 if (modifier->additional_display_items.size()) { |
| 138 if (!validateDisplayItems(modifier->additional_display_items, total, |
| 139 error_message)) { |
| 140 return false; |
| 141 } |
| 142 } |
| 143 } |
| 144 return true; |
| 145 } |
| 146 |
| 147 } // namespace |
| 148 |
| 149 namespace payments { |
| 150 |
| 151 bool validatePaymentDetails(const blink::mojom::PaymentDetailsPtr& details, |
| 152 std::string* error_message) { |
| 153 if (details->total.is_null()) { |
| 154 *error_message = "Must specify total"; |
| 155 return false; |
| 156 } |
| 157 |
| 158 if (!validateShippingOptionOrPaymentItem(details->total, details->total, |
| 159 error_message)) |
| 160 return false; |
| 161 |
| 162 if (details->total->amount->value[0] == '-') { |
| 163 *error_message = "Total amount value should be non-negative"; |
| 164 return false; |
| 165 } |
| 166 |
| 167 if (details->display_items.size()) { |
| 168 if (!validateDisplayItems(details->display_items, details->total, |
| 169 error_message)) |
| 170 return false; |
| 171 } |
| 172 |
| 173 if (details->shipping_options.size()) { |
| 174 if (!validateShippingOptions(details->shipping_options, details->total, |
| 175 error_message)) |
| 176 return false; |
| 177 } |
| 178 |
| 179 if (details->modifiers.size()) { |
| 180 if (!validatePaymentDetailsModifiers(details->modifiers, details->total, |
| 181 error_message)) |
| 182 return false; |
| 183 } |
| 184 if (!PaymentsValidators::isValidErrorMsgFormat(details->error, error_message)) |
| 185 return false; |
| 186 return true; |
| 187 } |
| 188 |
| 189 } // namespace payments |
| OLD | NEW |