BasicCardPayment.cpp (3828B)
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/. */ 6 7 #include "BasicCardPayment.h" 8 9 #include "PaymentAddress.h" 10 #include "mozilla/ClearOnShutdown.h" 11 #include "mozilla/ErrorResult.h" 12 #include "nsArrayUtils.h" 13 14 namespace mozilla::dom { 15 namespace { 16 bool IsValidNetwork(const nsAString& aNetwork) { 17 return aNetwork.Equals(u"amex"_ns) || aNetwork.Equals(u"cartebancaire"_ns) || 18 aNetwork.Equals(u"diners"_ns) || aNetwork.Equals(u"discover"_ns) || 19 aNetwork.Equals(u"jcb"_ns) || aNetwork.Equals(u"mastercard"_ns) || 20 aNetwork.Equals(u"mir"_ns) || aNetwork.Equals(u"unionpay"_ns) || 21 aNetwork.Equals(u"visa"_ns); 22 } 23 } // end of namespace 24 25 StaticRefPtr<BasicCardService> gBasicCardService; 26 27 already_AddRefed<BasicCardService> BasicCardService::GetService() { 28 if (!gBasicCardService) { 29 gBasicCardService = new BasicCardService(); 30 ClearOnShutdown(&gBasicCardService); 31 } 32 RefPtr<BasicCardService> service = gBasicCardService; 33 return service.forget(); 34 } 35 36 bool BasicCardService::IsBasicCardPayment(const nsAString& aSupportedMethods) { 37 return aSupportedMethods.Equals(u"basic-card"_ns); 38 } 39 40 bool BasicCardService::IsValidBasicCardRequest(JSContext* aCx, JSObject* aData, 41 nsAString& aErrorMsg) { 42 if (!aData) { 43 return true; 44 } 45 JS::Rooted<JS::Value> data(aCx, JS::ObjectValue(*aData)); 46 47 BasicCardRequest request; 48 if (!request.Init(aCx, data)) { 49 aErrorMsg.AssignLiteral( 50 "Fail to convert methodData.data to BasicCardRequest."); 51 return false; 52 } 53 54 for (const nsString& network : request.mSupportedNetworks) { 55 if (!IsValidNetwork(network)) { 56 aErrorMsg.Assign(network + u" is not an valid network."_ns); 57 return false; 58 } 59 } 60 return true; 61 } 62 63 bool BasicCardService::IsValidExpiryMonth(const nsAString& aExpiryMonth) { 64 // ExpiryMonth can only be 65 // 1. empty string 66 // 2. 01 ~ 12 67 if (aExpiryMonth.IsEmpty()) { 68 return true; 69 } 70 if (aExpiryMonth.Length() != 2) { 71 return false; 72 } 73 // can only be 00 ~ 09 74 if (aExpiryMonth.CharAt(0) == '0') { 75 if (aExpiryMonth.CharAt(1) < '0' || aExpiryMonth.CharAt(1) > '9') { 76 return false; 77 } 78 return true; 79 } 80 // can only be 11 or 12 81 if (aExpiryMonth.CharAt(0) == '1') { 82 if (aExpiryMonth.CharAt(1) != '1' && aExpiryMonth.CharAt(1) != '2') { 83 return false; 84 } 85 return true; 86 } 87 return false; 88 } 89 90 bool BasicCardService::IsValidExpiryYear(const nsAString& aExpiryYear) { 91 // ExpiryYear can only be 92 // 1. empty string 93 // 2. 0000 ~ 9999 94 if (!aExpiryYear.IsEmpty()) { 95 if (aExpiryYear.Length() != 4) { 96 return false; 97 } 98 for (uint32_t index = 0; index < 4; ++index) { 99 if (aExpiryYear.CharAt(index) < '0' || aExpiryYear.CharAt(index) > '9') { 100 return false; 101 } 102 } 103 } 104 return true; 105 } 106 107 void BasicCardService::CheckForValidBasicCardErrors(JSContext* aCx, 108 JSObject* aData, 109 ErrorResult& aRv) { 110 MOZ_ASSERT(aData, "Don't pass null data"); 111 JS::Rooted<JS::Value> data(aCx, JS::ObjectValue(*aData)); 112 113 // XXXbz Just because aData converts to BasicCardErrors right now doesn't mean 114 // it will if someone tries again! Should we be replacing aData with a 115 // conversion of the BasicCardErrors dictionary to a JS object in a clean 116 // compartment or something? 117 BasicCardErrors bcError; 118 if (!bcError.Init(aCx, data)) { 119 aRv.NoteJSContextException(aCx); 120 } 121 } 122 } // namespace mozilla::dom