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
154
155
156
157
158
159
|
/*
* Copyright (C) 2015, 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "PaymentRequestValidator.h"
#if ENABLE(APPLE_PAY)
#include "ExceptionCode.h"
#include "PaymentRequest.h"
#include <unicode/ucurr.h>
#include <unicode/uloc.h>
namespace WebCore {
static ExceptionOr<void> validateCountryCode(const String&);
static ExceptionOr<void> validateCurrencyCode(const String&);
static ExceptionOr<void> validateMerchantCapabilities(const PaymentRequest::MerchantCapabilities&);
static ExceptionOr<void> validateSupportedNetworks(const Vector<String>&);
static ExceptionOr<void> validateShippingMethods(const Vector<PaymentRequest::ShippingMethod>&);
static ExceptionOr<void> validateShippingMethod(const PaymentRequest::ShippingMethod&);
ExceptionOr<void> PaymentRequestValidator::validate(const PaymentRequest& paymentRequest)
{
auto validatedCountryCode = validateCountryCode(paymentRequest.countryCode());
if (validatedCountryCode.hasException())
return validatedCountryCode.releaseException();
auto validatedCurrencyCode = validateCurrencyCode(paymentRequest.currencyCode());
if (validatedCurrencyCode.hasException())
return validatedCurrencyCode.releaseException();
auto validatedSupportedNetworks = validateSupportedNetworks(paymentRequest.supportedNetworks());
if (validatedSupportedNetworks.hasException())
return validatedSupportedNetworks.releaseException();
auto validatedMerchantCapabilities = validateMerchantCapabilities(paymentRequest.merchantCapabilities());
if (validatedMerchantCapabilities.hasException())
return validatedMerchantCapabilities.releaseException();
auto validatedTotal = validateTotal(paymentRequest.total());
if (validatedTotal.hasException())
return validatedTotal.releaseException();
auto validatedShippingMethods = validateShippingMethods(paymentRequest.shippingMethods());
if (validatedShippingMethods.hasException())
return validatedShippingMethods.releaseException();
return { };
}
ExceptionOr<void> PaymentRequestValidator::validateTotal(const PaymentRequest::LineItem& total)
{
if (!total.label)
return Exception { TypeError, "Missing total label." };
if (!total.amount)
return Exception { TypeError, "Missing total amount." };
if (*total.amount <= 0)
return Exception { TypeError, "Total amount must be greater than zero." };
if (*total.amount > 10000000000)
return Exception { TypeError, "Total amount is too big." };
return { };
}
static ExceptionOr<void> validateCountryCode(const String& countryCode)
{
if (!countryCode)
return Exception { TypeError, "Missing country code." };
for (auto *countryCodePtr = uloc_getISOCountries(); *countryCodePtr; ++countryCodePtr) {
if (countryCode == *countryCodePtr)
return { };
}
return Exception { TypeError, makeString("\"" + countryCode, "\" is not a valid country code.") };
}
static ExceptionOr<void> validateCurrencyCode(const String& currencyCode)
{
if (!currencyCode)
return Exception { TypeError, "Missing currency code." };
UErrorCode errorCode = U_ZERO_ERROR;
auto currencyCodes = std::unique_ptr<UEnumeration, void (*)(UEnumeration*)>(ucurr_openISOCurrencies(UCURR_ALL, &errorCode), uenum_close);
int32_t length;
while (auto *currencyCodePtr = uenum_next(currencyCodes.get(), &length, &errorCode)) {
if (currencyCodePtr == currencyCode)
return { };
}
return Exception { TypeError, makeString("\"" + currencyCode, "\" is not a valid currency code.") };
}
static ExceptionOr<void> validateMerchantCapabilities(const PaymentRequest::MerchantCapabilities& merchantCapabilities)
{
if (!merchantCapabilities.supports3DS && !merchantCapabilities.supportsEMV && !merchantCapabilities.supportsCredit && !merchantCapabilities.supportsDebit)
return Exception { TypeError, "Missing merchant capabilities." };
return { };
}
static ExceptionOr<void> validateSupportedNetworks(const Vector<String>& supportedNetworks)
{
if (supportedNetworks.isEmpty())
return Exception { TypeError, "Missing supported networks." };
return { };
}
static ExceptionOr<void> validateShippingMethod(const PaymentRequest::ShippingMethod& shippingMethod)
{
if (shippingMethod.amount < 0)
return Exception { TypeError, "Shipping method amount must be greater than or equal to zero." };
return { };
}
static ExceptionOr<void> validateShippingMethods(const Vector<PaymentRequest::ShippingMethod>& shippingMethods)
{
for (const auto& shippingMethod : shippingMethods) {
auto validatedShippingMethod = validateShippingMethod(shippingMethod);
if (validatedShippingMethod.hasException())
return validatedShippingMethod.releaseException();
}
return { };
}
}
#endif
|