| 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 payments::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->currency_system.empty()) { | |
| 48 *error_message = "Currency system can't be empty"; | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 if (!payments::PaymentsValidators::isValidCurrencyCodeFormat( | |
| 53 item->amount->currency, item->amount->currency_system, | |
| 54 error_message)) { | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 if (!payments::PaymentsValidators::isValidAmountFormat(item->amount->value, | |
| 59 error_message)) { | |
| 60 return false; | |
| 61 } | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 bool validateDisplayItems( | |
| 66 const std::vector<payments::mojom::PaymentItemPtr>& items, | |
| 67 const payments::mojom::PaymentItemPtr& total, | |
| 68 std::string* error_message) { | |
| 69 for (const auto& item : items) { | |
| 70 if (!validateShippingOptionOrPaymentItem(item, total, error_message)) | |
| 71 return false; | |
| 72 } | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 bool validateShippingOptions( | |
| 77 const std::vector<payments::mojom::PaymentShippingOptionPtr>& options, | |
| 78 const payments::mojom::PaymentItemPtr& total, | |
| 79 std::string* error_message) { | |
| 80 std::set<std::string> uniqueIds; | |
| 81 for (const auto& option : options) { | |
| 82 if (option->id.empty()) { | |
| 83 *error_message = "ShippingOption id required"; | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 if (uniqueIds.find(option->id) != uniqueIds.end()) { | |
| 88 *error_message = "Duplicate shipping option identifiers are not allowed"; | |
| 89 return false; | |
| 90 } | |
| 91 uniqueIds.insert(option->id); | |
| 92 | |
| 93 if (!validateShippingOptionOrPaymentItem(option, total, error_message)) | |
| 94 return false; | |
| 95 } | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 bool validatePaymentDetailsModifiers( | |
| 100 const std::vector<payments::mojom::PaymentDetailsModifierPtr>& modifiers, | |
| 101 const payments::mojom::PaymentItemPtr& total, | |
| 102 std::string* error_message) { | |
| 103 if (modifiers.empty()) { | |
| 104 *error_message = "Must specify at least one payment details modifier"; | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 for (const auto& modifier : modifiers) { | |
| 109 if (!modifier->method_data) { | |
| 110 *error_message = "Method data required"; | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 if (modifier->method_data->supported_methods.empty()) { | |
| 115 *error_message = "Must specify at least one payment method identifier"; | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 if (modifier->total) { | |
| 120 if (!validateShippingOptionOrPaymentItem(modifier->total, total, | |
| 121 error_message)) | |
| 122 return false; | |
| 123 | |
| 124 if (modifier->total->amount->value[0] == '-') { | |
| 125 *error_message = "Total amount value should be non-negative"; | |
| 126 return false; | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 if (modifier->additional_display_items.size()) { | |
| 131 if (!validateDisplayItems(modifier->additional_display_items, total, | |
| 132 error_message)) { | |
| 133 return false; | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 return true; | |
| 138 } | |
| 139 | |
| 140 } // namespace | |
| 141 | |
| 142 namespace payments { | |
| 143 | |
| 144 bool validatePaymentDetails(const mojom::PaymentDetailsPtr& details, | |
| 145 std::string* error_message) { | |
| 146 if (details->total.is_null()) { | |
| 147 *error_message = "Must specify total"; | |
| 148 return false; | |
| 149 } | |
| 150 | |
| 151 if (!validateShippingOptionOrPaymentItem(details->total, details->total, | |
| 152 error_message)) | |
| 153 return false; | |
| 154 | |
| 155 if (details->total->amount->value[0] == '-') { | |
| 156 *error_message = "Total amount value should be non-negative"; | |
| 157 return false; | |
| 158 } | |
| 159 | |
| 160 if (details->display_items.size()) { | |
| 161 if (!validateDisplayItems(details->display_items, details->total, | |
| 162 error_message)) | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 if (details->shipping_options.size()) { | |
| 167 if (!validateShippingOptions(details->shipping_options, details->total, | |
| 168 error_message)) | |
| 169 return false; | |
| 170 } | |
| 171 | |
| 172 if (details->modifiers.size()) { | |
| 173 if (!validatePaymentDetailsModifiers(details->modifiers, details->total, | |
| 174 error_message)) | |
| 175 return false; | |
| 176 } | |
| 177 if (!PaymentsValidators::isValidErrorMsgFormat(details->error, error_message)) | |
| 178 return false; | |
| 179 return true; | |
| 180 } | |
| 181 | |
| 182 } // namespace payments | |
| OLD | NEW |