1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "BasicCardPayment.h"
8 #include "PaymentAddress.h"
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/ErrorResult.h"
11 #include "nsArrayUtils.h"
13 namespace mozilla::dom
{
15 bool IsValidNetwork(const nsAString
& aNetwork
) {
16 return aNetwork
.Equals(u
"amex"_ns
) || aNetwork
.Equals(u
"cartebancaire"_ns
) ||
17 aNetwork
.Equals(u
"diners"_ns
) || aNetwork
.Equals(u
"discover"_ns
) ||
18 aNetwork
.Equals(u
"jcb"_ns
) || aNetwork
.Equals(u
"mastercard"_ns
) ||
19 aNetwork
.Equals(u
"mir"_ns
) || aNetwork
.Equals(u
"unionpay"_ns
) ||
20 aNetwork
.Equals(u
"visa"_ns
);
24 StaticRefPtr
<BasicCardService
> gBasicCardService
;
26 already_AddRefed
<BasicCardService
> BasicCardService::GetService() {
27 if (!gBasicCardService
) {
28 gBasicCardService
= new BasicCardService();
29 ClearOnShutdown(&gBasicCardService
);
31 RefPtr
<BasicCardService
> service
= gBasicCardService
;
32 return service
.forget();
35 bool BasicCardService::IsBasicCardPayment(const nsAString
& aSupportedMethods
) {
36 return aSupportedMethods
.Equals(u
"basic-card"_ns
);
39 bool BasicCardService::IsValidBasicCardRequest(JSContext
* aCx
, JSObject
* aData
,
40 nsAString
& aErrorMsg
) {
44 JS::RootedValue
data(aCx
, JS::ObjectValue(*aData
));
46 BasicCardRequest request
;
47 if (!request
.Init(aCx
, data
)) {
48 aErrorMsg
.AssignLiteral(
49 "Fail to convert methodData.data to BasicCardRequest.");
53 for (const nsString
& network
: request
.mSupportedNetworks
) {
54 if (!IsValidNetwork(network
)) {
55 aErrorMsg
.Assign(network
+ u
" is not an valid network."_ns
);
62 bool BasicCardService::IsValidExpiryMonth(const nsAString
& aExpiryMonth
) {
63 // ExpiryMonth can only be
66 if (aExpiryMonth
.IsEmpty()) {
69 if (aExpiryMonth
.Length() != 2) {
72 // can only be 00 ~ 09
73 if (aExpiryMonth
.CharAt(0) == '0') {
74 if (aExpiryMonth
.CharAt(1) < '0' || aExpiryMonth
.CharAt(1) > '9') {
79 // can only be 11 or 12
80 if (aExpiryMonth
.CharAt(0) == '1') {
81 if (aExpiryMonth
.CharAt(1) != '1' && aExpiryMonth
.CharAt(1) != '2') {
89 bool BasicCardService::IsValidExpiryYear(const nsAString
& aExpiryYear
) {
90 // ExpiryYear can only be
93 if (!aExpiryYear
.IsEmpty()) {
94 if (aExpiryYear
.Length() != 4) {
97 for (uint32_t index
= 0; index
< 4; ++index
) {
98 if (aExpiryYear
.CharAt(index
) < '0' || aExpiryYear
.CharAt(index
) > '9') {
106 void BasicCardService::CheckForValidBasicCardErrors(JSContext
* aCx
,
109 MOZ_ASSERT(aData
, "Don't pass null data");
110 JS::RootedValue
data(aCx
, JS::ObjectValue(*aData
));
112 // XXXbz Just because aData converts to BasicCardErrors right now doesn't mean
113 // it will if someone tries again! Should we be replacing aData with a
114 // conversion of the BasicCardErrors dictionary to a JS object in a clean
115 // compartment or something?
116 BasicCardErrors bcError
;
117 if (!bcError
.Init(aCx
, data
)) {
118 aRv
.NoteJSContextException(aCx
);
121 } // namespace mozilla::dom