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

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

Issue 2373103002: [Web Payments] Common Payments validation (Closed)
Patch Set: Split off moving Mojo file 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 <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 payments {
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 void validateShippingOptionOrPaymentItem(const T& item,
19 ExceptionState& exceptionState) {
20 if (item->label.empty()) {
21 exceptionState.throwTypeError("Item label required");
22 return;
23 }
24
25 if (!item->amount) {
26 exceptionState.throwTypeError("Currency amount required");
27 return;
28 }
29
30 if (item->amount->currency.empty()) {
31 exceptionState.throwTypeError("Currency code required");
32 return;
33 }
34
35 if (item->amount->value.empty()) {
36 exceptionState.throwTypeError("Currency value required");
37 return;
38 }
39
40 std::string errorMessage;
41 if (!PaymentsValidators::isValidCurrencyCodeFormat(item->amount->currency,
42 &errorMessage)) {
43 exceptionState.throwTypeError(errorMessage);
44 return;
45 }
46
47 if (!PaymentsValidators::isValidAmountFormat(item->amount->value,
48 &errorMessage)) {
49 exceptionState.throwTypeError(errorMessage);
50 return;
51 }
52 }
53
54 void validateDisplayItems(
55 const std::vector<blink::mojom::PaymentItemPtr>& items,
56 ExceptionState& exceptionState) {
57 for (const auto& item : items) {
58 validateShippingOptionOrPaymentItem(item, exceptionState);
59 if (exceptionState.hadException())
60 return;
61 }
62 }
63
64 void validateShippingOptions(
65 const std::vector<blink::mojom::PaymentShippingOptionPtr>& options,
66 ExceptionState& exceptionState) {
67 std::set<std::string> uniqueIds;
68 for (const auto& option : options) {
69 if (option->id.empty()) {
70 exceptionState.throwTypeError("ShippingOption id required");
71 return;
72 }
73
74 if (uniqueIds.find(option->id) != uniqueIds.end()) {
75 exceptionState.throwTypeError(
76 "Duplicate shipping option identifiers are not allowed");
77 return;
78 }
79 uniqueIds.insert(option->id);
80
81 validateShippingOptionOrPaymentItem(option, exceptionState);
82 if (exceptionState.hadException())
83 return;
84 }
85 }
86
87 void validatePaymentDetailsModifiers(
88 const std::vector<blink::mojom::PaymentDetailsModifierPtr>& modifiers,
89 ExceptionState& exceptionState) {
90 if (modifiers.empty()) {
91 exceptionState.throwTypeError(
92 "Must specify at least one payment details modifier");
93 return;
94 }
95
96 std::set<mojo::String> uniqueMethods;
97 for (const auto& modifier : modifiers) {
98 if (modifier->supported_methods.empty()) {
99 exceptionState.throwTypeError(
100 "Must specify at least one payment method identifier");
101 return;
102 }
103
104 for (const auto& method : modifier->supported_methods) {
105 if (uniqueMethods.find(method) != uniqueMethods.end()) {
106 exceptionState.throwTypeError(
107 "Duplicate payment method identifiers are not allowed");
108 return;
109 }
110 uniqueMethods.insert(method);
111 }
112
113 if (modifier->total) {
114 validateShippingOptionOrPaymentItem(modifier->total, exceptionState);
115 if (exceptionState.hadException())
116 return;
117
118 if (modifier->total->amount->value[0] == '-') {
119 exceptionState.throwTypeError(
120 "Total amount value should be non-negative");
121 return;
122 }
123 }
124
125 if (modifier->additional_display_items.size()) {
126 validateDisplayItems(modifier->additional_display_items, exceptionState);
127 if (exceptionState.hadException())
128 return;
129 }
130 }
131 }
132
please use gerrit instead 2016/10/27 17:14:25 The functions above can be in anonymous namespace
Kevin Bailey 2016/10/27 22:17:53 Done.
133 void validatePaymentDetails(const blink::mojom::PaymentDetailsPtr& details,
134 ExceptionState& exceptionState) {
135 if (details->total.is_null()) {
136 exceptionState.throwTypeError("Must specify total");
137 return;
138 }
139
140 validateShippingOptionOrPaymentItem(details->total, exceptionState);
141 if (exceptionState.hadException())
142 return;
143
144 if (details->total->amount->value[0] == '-') {
145 exceptionState.throwTypeError("Total amount value should be non-negative");
146 return;
147 }
148
149 if (details->display_items.size()) {
150 validateDisplayItems(details->display_items, exceptionState);
151 if (exceptionState.hadException())
152 return;
153 }
154
155 if (details->shipping_options.size()) {
156 validateShippingOptions(details->shipping_options, exceptionState);
157 if (exceptionState.hadException())
158 return;
159 }
160
161 if (details->modifiers.size()) {
162 validatePaymentDetailsModifiers(details->modifiers, exceptionState);
163 }
164 }
165
166 bool validatePaymentDetails(const blink::mojom::PaymentDetailsPtr& details) {
167 ExceptionState exceptionState;
168 validatePaymentDetails(details, exceptionState);
169 return exceptionState.hadException();
170 }
171
172 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698