Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: components/payments/payment_details_validation.cc

Issue 2373103002: [Web Payments] Common Payments validation (Closed)
Patch Set: Deps and new field Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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(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)) {
please use gerrit instead 2016/10/31 14:31:15 Ideally "" would be replaced with item->amount->cu
Kevin Bailey 2016/10/31 20:32:00 Sorry, missed this one. Also, really named it 'cur
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698