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

Unified Diff: components/payments/payment_details_validation.cc

Issue 2373103002: [Web Payments] Common Payments validation (Closed)
Patch Set: Deps 2 Created 4 years, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: components/payments/payment_details_validation.cc
diff --git a/components/payments/payment_details_validation.cc b/components/payments/payment_details_validation.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e333ea81c254fb33ab89ab4af6013b242330d171
--- /dev/null
+++ b/components/payments/payment_details_validation.cc
@@ -0,0 +1,165 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <set>
+#include <vector>
+
+#include "components/payments/payment_details_validation.h"
+
+#include "components/payments/payment_request.mojom.h"
+#include "components/payments/payments_validators.h"
+
+namespace {
+
+// Validates ShippingOption or PaymentItem, which happen to have identical
+// fields, except for "id", which is present only in ShippingOption.
+template <typename T>
+bool validateShippingOptionOrPaymentItem(const T& item,
+ std::string* error_message) {
+ if (item->label.empty()) {
+ *error_message = "Item label required";
+ return false;
+ }
+
+ if (!item->amount) {
+ *error_message = "Currency amount required";
+ return false;
+ }
+
+ if (item->amount->currency.empty()) {
+ *error_message = "Currency code required";
+ return false;
+ }
+
+ if (item->amount->value.empty()) {
+ *error_message = "Currency value required";
+ return false;
+ }
+
+ if (!payments::PaymentsValidators::isValidCurrencyCodeFormat(
+ item->amount->currency, error_message)) {
+ return false;
+ }
+
+ if (!payments::PaymentsValidators::isValidAmountFormat(
+ item->amount->value, error_message)) {
+ return false;
+ }
+ return true;
+}
+
+bool validateDisplayItems(
+ const std::vector<blink::mojom::PaymentItemPtr>& items,
+ std::string* error_message) {
+ for (const auto& item : items) {
+ if (!validateShippingOptionOrPaymentItem(item, error_message))
+ return false;
+ }
+ return true;
+}
+
+bool validateShippingOptions(
+ const std::vector<blink::mojom::PaymentShippingOptionPtr>& options,
+ std::string* error_message) {
+ std::set<std::string> uniqueIds;
+ for (const auto& option : options) {
+ if (option->id.empty()) {
+ *error_message = "ShippingOption id required";
+ return false;
+ }
+
+ if (uniqueIds.find(option->id) != uniqueIds.end()) {
+ *error_message = "Duplicate shipping option identifiers are not allowed";
+ return false;
+ }
+ uniqueIds.insert(option->id);
+
+ if (!validateShippingOptionOrPaymentItem(option, error_message))
+ return false;
+ }
+ return true;
+}
+
+bool validatePaymentDetailsModifiers(
+ const std::vector<blink::mojom::PaymentDetailsModifierPtr>& modifiers,
+ std::string* error_message) {
+ if (modifiers.empty()) {
+ *error_message =
+ "Must specify at least one payment details modifier";
+ return false;
+ }
+
+ std::set<mojo::String> uniqueMethods;
+ for (const auto& modifier : modifiers) {
+ if (modifier->supported_methods.empty()) {
+ *error_message = "Must specify at least one payment method identifier";
+ return false;
+ }
+
+ for (const auto& method : modifier->supported_methods) {
+ if (uniqueMethods.find(method) != uniqueMethods.end()) {
+ *error_message =
+ "Duplicate payment method identifiers are not allowed";
+ return false;
+ }
+ uniqueMethods.insert(method);
+ }
+
+ if (modifier->total) {
+ if (!validateShippingOptionOrPaymentItem(modifier->total, error_message))
+ return false;
+
+ if (modifier->total->amount->value[0] == '-') {
+ *error_message = "Total amount value should be non-negative";
+ return false;
+ }
+ }
+
+ if (modifier->additional_display_items.size()) {
+ if (!validateDisplayItems(
+ modifier->additional_display_items, error_message)) {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+} // namespace
+
+namespace payments {
+
+bool validatePaymentDetails(const blink::mojom::PaymentDetailsPtr& details,
+ std::string* error_message) {
+ if (details->total.is_null()) {
+ *error_message = "Must specify total";
+ return false;
+ }
+
+ if (!validateShippingOptionOrPaymentItem(details->total, error_message))
+ return false;
+
+ if (details->total->amount->value[0] == '-') {
+ *error_message = "Total amount value should be non-negative";
+ return false;
+ }
+
+ if (details->display_items.size()) {
+ if (!validateDisplayItems(details->display_items, error_message))
+ return false;
+ }
+
+ if (details->shipping_options.size()) {
+ if (!validateShippingOptions(details->shipping_options, error_message))
+ return false;
+ }
+
+ if (details->modifiers.size()) {
+ if (!validatePaymentDetailsModifiers(details->modifiers, error_message))
+ return false;
+ }
+ return true;
+}
+
+} // namespace payments

Powered by Google App Engine
This is Rietveld 408576698