1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/payments/core/payment_details_validation.h"
#include <set>
#include <vector>
#include "components/payments/core/payment_details.h"
#include "components/payments/core/payments_validators.h"
namespace payments {
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.amount) {
*error_message = "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<PaymentItem>& items,
std::string* error_message) {
for (const auto& item : items) {
if (!ValidateShippingOptionOrPaymentItem(item, error_message)) {
return false;
}
}
return true;
}
bool ValidateShippingOptions(const std::vector<PaymentShippingOption>& 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<PaymentDetailsModifier>& modifiers,
std::string* error_message) {
if (modifiers.empty()) {
*error_message = "Must specify at least one payment details modifier";
return false;
}
for (const auto& modifier : modifiers) {
if (modifier.method_data.supported_method.empty()) {
*error_message = "Must specify payment method identifier";
return false;
}
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
bool ValidatePaymentDetails(const PaymentDetails& details,
std::string* error_message) {
if (details.total) {
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;
}
}
if (!PaymentsValidators::IsValidErrorMsgFormat(details.error, error_message))
return false;
return true;
}
} // namespace payments
|