@adyen/kyc-components
Version:
This guide assumes that you have already an account with Adyen. A legalEntity needs to be created, and you need to have a `legalEntityId` to instatiate a Component.
412 lines (411 loc) • 15.3 kB
JavaScript
try {
let e = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof globalThis ? globalThis : "undefined" != typeof self ? self : {}, n = new e.Error().stack;
n && (e._sentryDebugIds = e._sentryDebugIds || {}, e._sentryDebugIds[n] = "d0a5d1e7-85ea-4bb3-84e3-b06ebf9102d3", e._sentryDebugIdIdentifier = "sentry-dbid-d0a5d1e7-85ea-4bb3-84e3-b06ebf9102d3");
} catch (e) {}
import { o as createLogger } from "./translation-BFxyJ1c5.js";
import { r as httpPost, s as useApiContext } from "./http-D1NDkBxF.js";
import { C as isValidMinLength, D as trimValWithOneSpace, _ as hasText, a as NO_ALPHABET_REGEX, c as SPECIAL_CHARS, f as getFormattingRegEx, g as hasRepeatedCharacters, h as hasMaximumTwoRepeatedCharacters, l as chomeAndBanchiValidator, n as EMOJI_REGEX, o as NO_ALPHABET_UNICODE_REGEX, t as ADDRESS_VALID_CHAR_REGEX, w as matchesRegex, x as isInvalidName } from "./validatorUtils-DRapRJ6z.js";
import { t as CountryCodes } from "./country-code-CX5KqMBr.js";
import { useCallback } from "preact/hooks";
import { useQuery } from "@tanstack/preact-query";
//#region src/api/address/useAddressCleanse.ts
/**
* Utilizing the Cleanse API from Loqate provider, determine if retrieved address is a PO Box
* @param data Object containing country and text fields
* @returns Promise with RetrieveAddressSuccessResponse as a response
*/
var cleanseAddress = async (legalEntityId, baseUrl, data) => {
return httpPost({
baseUrl,
path: `legalEntities/${legalEntityId}/addresses/cleanse`
}, data);
};
var useAddressCleanse = (data, options) => {
const { rootLegalEntityId, baseUrl } = useApiContext();
return useQuery({
queryKey: ["addressCleanse", data],
queryFn: () => cleanseAddress(rootLegalEntityId.value, baseUrl.value, data),
...options
});
};
var useAddressCleanseImperatively = () => {
const { rootLegalEntityId, baseUrl } = useApiContext();
return useCallback((data) => cleanseAddress(rootLegalEntityId.value, baseUrl.value, data), [baseUrl.value, rootLegalEntityId.value]);
};
var formatAddressForCleanse = ({ address, city, country, postalCode }) => {
if (!address || !country) return void 0;
return `${address} ${city ?? ""} ${country} ${postalCode ?? ""}`.trim();
};
//#endregion
//#region src/utils/regex/postalCodePatterns.ts
var createRegExpByDigits = (digits) => new RegExp(`^\\d{${digits}}$`);
var postalCodePatterns = {
[CountryCodes.Australia]: createRegExpByDigits(4),
[CountryCodes.Austria]: createRegExpByDigits(4),
[CountryCodes.Belgium]: /^[1-9]\d{3}$/,
[CountryCodes.Brazil]: createRegExpByDigits(8),
[CountryCodes.Bulgaria]: createRegExpByDigits(4),
[CountryCodes.Canada]: /^[ABCEGHJKLMNPRSTVXY]\d[A-Z][ -]?(?:\d[A-Z]\d)?$/,
[CountryCodes.Croatia]: /^([1-5])\d{4}$/,
[CountryCodes.Cyprus]: createRegExpByDigits(4),
[CountryCodes.CzechRepublic]: /^\d{3} ?\d{2}$/,
[CountryCodes.Denmark]: /^(?:DK-)?\d{4}$/,
[CountryCodes.Estonia]: createRegExpByDigits(5),
[CountryCodes.Finland]: createRegExpByDigits(5),
[CountryCodes.France]: createRegExpByDigits(5),
[CountryCodes.Georgia]: createRegExpByDigits(4),
[CountryCodes.Germany]: createRegExpByDigits(5),
[CountryCodes.Gibraltar]: /^GX11[ -]?1AA$/,
[CountryCodes.Greece]: /^\d{3} ?\d{2}$/,
[CountryCodes.Hungary]: createRegExpByDigits(4),
[CountryCodes.Iceland]: createRegExpByDigits(3),
[CountryCodes.Ireland]: /^(?:[ACDEFHKNPRTVWXY]\d{2}|D6W)[ -]?[0-9ACDEFHKNPRTVWXY]{4}$/,
[CountryCodes.Italy]: createRegExpByDigits(5),
[CountryCodes.Japan]: createRegExpByDigits(7),
[CountryCodes.Latvia]: /^(?:LV-)?\d{4}$/,
[CountryCodes.Liechtenstein]: createRegExpByDigits(4),
[CountryCodes.Lithuania]: /^(?:LT-)?\d{4,5}$/,
[CountryCodes.Luxembourg]: createRegExpByDigits(4),
[CountryCodes.Malaysia]: createRegExpByDigits(5),
[CountryCodes.Malta]: /^[A-Za-z]{2,3} ?\d{2}(?:\d{2})?$/,
[CountryCodes.Monaco]: /^980\d{2}$/,
[CountryCodes.Netherlands]: /^(?:NL-)?[1-9]\d{3} ?(?:[ABCDEFGHIJKLMNOPQRTUVWXYZ][A-Z]|S[BCEFGHIJKLMNOPQRTUVWXYZ])$/,
[CountryCodes.NewZealand]: createRegExpByDigits(4),
[CountryCodes.Norway]: createRegExpByDigits(4),
[CountryCodes.Poland]: /^\d{2}-\d{3}$/,
[CountryCodes.Portugal]: /^[1-9]\d{3}([- ]?\d{3})?$/,
[CountryCodes.PuertoRico]: /^\d{5}(?:[ -]?\d{4})?$/,
[CountryCodes.Romania]: createRegExpByDigits(6),
[CountryCodes.Slovakia]: /^(SK-)?\d{5}$/,
[CountryCodes.Slovenia]: /^(SI-)?\d{4}$/,
[CountryCodes.Singapore]: createRegExpByDigits(6),
[CountryCodes.Spain]: /(?:0[1-9]|[1-4]\d|5[0-2])\d{3}/,
[CountryCodes.Sweden]: createRegExpByDigits(5),
[CountryCodes.Switzerland]: /^[1-9]\d{3}$/,
[CountryCodes.UnitedKingdom]: /^[ABCDEFGHIJKLMNOPRSTUWYZ](?:[ABCDEFGHKLMNOPQRSTUVWXY][0-9][ABEHMNPRVWXY0-9]?|[0-9][ABCDEFGHJKPSTUW0-9]?)(?: ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{1,2})?$/,
[CountryCodes.UnitedStates]: /^\d{5}(?:[ -]?\d{4})?$/
};
//#endregion
//#region src/components/Shared/forms/Address/validate.formats.ts
var createFormatByDigits = (digits) => {
return {
formatter: (val) => val ? val.replace(getFormattingRegEx("^\\d", "g"), "").substring(0, digits) : void 0,
format: new Array(digits).fill("9").join(""),
maxlength: digits
};
};
var addressDisallowedChars = getFormattingRegEx(SPECIAL_CHARS.replace(/[-(\\/)]/g, ""));
var addressFormatters = {
postalCode: { formatter: (val, context) => {
const specificRule = countrySpecificFormatters[context?.state.data.country]?.postalCode?.formatter;
if (specificRule) return specificRule(val);
return val;
} },
address: { formatter: (val) => val ? trimValWithOneSpace(val).replace(addressDisallowedChars, "") : void 0 },
otherAddressInformation: { formatter: (val) => val ? trimValWithOneSpace(val).replace(addressDisallowedChars, "") : void 0 },
city: { formatter: (val) => val ? trimValWithOneSpace(val).replace(addressDisallowedChars, "") : void 0 }
};
var countrySpecificFormatters = {
[CountryCodes.Australia]: { postalCode: createFormatByDigits(4) },
[CountryCodes.Austria]: { postalCode: createFormatByDigits(4) },
[CountryCodes.Belgium]: { postalCode: createFormatByDigits(4) },
[CountryCodes.Brazil]: { postalCode: createFormatByDigits(8) },
[CountryCodes.Bulgaria]: { postalCode: createFormatByDigits(4) },
[CountryCodes.Canada]: { postalCode: {
format: "A9A 9A9 or A9A9A9",
maxlength: 7
} },
[CountryCodes.Croatia]: { postalCode: {
format: "[1-5]9999",
maxlength: 5
} },
[CountryCodes.Cyprus]: { postalCode: createFormatByDigits(4) },
[CountryCodes.CzechRepublic]: { postalCode: {
format: "999 99",
maxlength: 6
} },
[CountryCodes.Denmark]: { postalCode: {
format: "9999",
maxlength: 7
} },
[CountryCodes.Estonia]: { postalCode: createFormatByDigits(5) },
[CountryCodes.Finland]: { postalCode: createFormatByDigits(5) },
[CountryCodes.France]: { postalCode: createFormatByDigits(5) },
[CountryCodes.Germany]: { postalCode: createFormatByDigits(5) },
[CountryCodes.Gibraltar]: { postalCode: {
format: "GX11 1AA",
maxlength: 8
} },
[CountryCodes.Greece]: { postalCode: {
format: "999 99",
maxlength: 6
} },
[CountryCodes.Hungary]: { postalCode: createFormatByDigits(4) },
[CountryCodes.Iceland]: { postalCode: createFormatByDigits(3) },
[CountryCodes.Ireland]: { postalCode: {
format: "A99 A999",
maxlength: 8
} },
[CountryCodes.Italy]: { postalCode: createFormatByDigits(5) },
[CountryCodes.Japan]: { postalCode: createFormatByDigits(7) },
[CountryCodes.Latvia]: { postalCode: {
format: "9999 or LV-9999",
maxlength: 7
} },
[CountryCodes.Liechtenstein]: { postalCode: createFormatByDigits(4) },
[CountryCodes.Lithuania]: { postalCode: {
format: "9999 or 99999 or LT-99999",
maxlength: 8
} },
[CountryCodes.Luxembourg]: { postalCode: createFormatByDigits(4) },
[CountryCodes.Malaysia]: { postalCode: createFormatByDigits(5) },
[CountryCodes.Malta]: { postalCode: {
format: "2 or 3 letters + 2 or 4 digits (with optional space)",
maxlength: 8
} },
[CountryCodes.Monaco]: { postalCode: {
format: "980NN",
maxlength: 5
} },
[CountryCodes.Netherlands]: { postalCode: {
format: "9999AA",
maxlength: 7
} },
[CountryCodes.NewZealand]: { postalCode: createFormatByDigits(4) },
[CountryCodes.Norway]: { postalCode: createFormatByDigits(4) },
[CountryCodes.Poland]: { postalCode: {
formatter: (val) => {
if (!val) return void 0;
const nuVal = val.replace(getFormattingRegEx("^\\d-", "g"), "");
const maxlength = nuVal.includes("-") ? 6 : 5;
return nuVal.substring(0, maxlength);
},
format: "99-999",
maxlength: 6
} },
[CountryCodes.Portugal]: { postalCode: {
formatter: (val) => {
if (!val) return void 0;
return val.replace(getFormattingRegEx("^\\d-", "g"), "").substring(0, 8);
},
format: "9999-999",
maxlength: 8
} },
[CountryCodes.PuertoRico]: { postalCode: createFormatByDigits(5) },
[CountryCodes.Romania]: { postalCode: createFormatByDigits(6) },
[CountryCodes.Singapore]: { postalCode: createFormatByDigits(6) },
[CountryCodes.Slovakia]: { postalCode: {
format: "99999 or SK-99999",
maxlength: 8
} },
[CountryCodes.Slovenia]: { postalCode: {
format: "9999 or SI-9999",
maxlength: 7
} },
[CountryCodes.Spain]: { postalCode: createFormatByDigits(5) },
[CountryCodes.Sweden]: { postalCode: createFormatByDigits(5) },
[CountryCodes.Switzerland]: { postalCode: createFormatByDigits(4) },
[CountryCodes.UnitedKingdom]: { postalCode: {
formatter: (val) => val ? val.replace(getFormattingRegEx(SPECIAL_CHARS), "").substring(0, 8) : void 0,
format: "AA99 9AA or A99 9AA or A9 9AA",
maxlength: 8
} },
[CountryCodes.UnitedStates]: { postalCode: {
format: "12345 or 12345-6789",
maxlength: 10
} }
};
//#endregion
//#region src/components/Shared/forms/Address/validate.ts
var logger = createLogger();
var addressValidationRules = (country) => {
const postalCodeFormat = country ? countrySpecificFormatters[country]?.postalCode?.format : void 0;
return {
address: [{
modes: ["blur"],
validate: (val) => hasText(val),
errorMessage: "fieldIsRequired"
}],
postalCode: {
modes: ["blur"],
validate: (val) => {
const pattern = country ? postalCodePatterns[country] : void 0;
return pattern ? !!val && pattern.test(val) : !!val;
},
errorMessage: postalCodeFormat ? {
key: "invalidFormatExpects",
values: { format: postalCodeFormat }
} : "invalidFormat"
},
city: {
modes: ["blur"],
validate: hasText,
errorMessage: "fieldIsRequired"
},
stateOrProvince: {
modes: ["blur"],
validate: (val) => !!val,
errorMessage: "fieldIsRequired"
},
country: {
modes: ["blur"],
validate: (val) => !!val,
errorMessage: "fieldIsRequired"
}
};
};
var countrySpecificOtherAddressInformationValidators = (country) => {
switch (country) {
case CountryCodes.Japan: return [{
modes: ["blur"],
validate: (input) => {
return hasText(input);
},
errorMessage: "checkForTypos"
}, {
modes: ["blur"],
validate: (input) => {
return chomeAndBanchiValidator(input);
},
errorMessage: "pleaseUseOnlyNumbersAndDashes"
}];
default: return;
}
};
var addressValidationRulesV4 = (country, t, isFeatureEnabled = () => false) => {
const postalCodeFormat = country ? countrySpecificFormatters[country]?.postalCode?.format : void 0;
const MIN_LENGTH_3 = 3;
const MIN_LENGTH_2 = 2;
const otherAddressInformation = countrySpecificOtherAddressInformationValidators(country);
return {
address: [
{
modes: ["blur"],
validate: (val) => hasText(val),
errorMessage: "fieldIsRequired"
},
{
modes: ["blur"],
validate: (input) => !matchesRegex(NO_ALPHABET_UNICODE_REGEX, input),
errorMessage: "nameMustContainLetters"
},
{
modes: ["blur"],
validate: (val) => !matchesRegex(EMOJI_REGEX, val) && !hasRepeatedCharacters(val) && !isInvalidName(val) && matchesRegex(ADDRESS_VALID_CHAR_REGEX, val),
errorMessage: "checkForTypos"
},
{
modes: ["blur"],
validate: (val) => isValidMinLength(val, MIN_LENGTH_3),
errorMessage: {
key: "fieldMustContainAtLeast",
values: {
fieldName: t(($) => $["address"]),
minLength: String(MIN_LENGTH_3)
}
}
}
],
postalCode: {
modes: ["blur"],
validate: (val) => {
const pattern = country ? postalCodePatterns[country] : void 0;
return pattern ? !!val && pattern.test(val) : !!val;
},
errorMessage: postalCodeFormat ? {
key: "invalidFormatExpects",
values: { format: postalCodeFormat }
} : "invalidFormat"
},
city: [
{
modes: ["blur"],
validate: hasText,
errorMessage: "fieldIsRequired"
},
{
modes: ["blur"],
validate: (input) => !matchesRegex(NO_ALPHABET_UNICODE_REGEX, input),
errorMessage: "nameMustContainLetters"
},
{
modes: ["blur"],
validate: (val) => !matchesRegex(EMOJI_REGEX, val) && (isFeatureEnabled("EnableAllowTwoRepeatedCharactersInName") ? !hasMaximumTwoRepeatedCharacters(val) : !hasRepeatedCharacters(val)) && !isInvalidName(val),
errorMessage: "checkForTypos"
},
{
modes: ["blur"],
validate: (val) => isValidMinLength(val, MIN_LENGTH_2),
errorMessage: {
key: "fieldMustContainAtLeast",
values: {
fieldName: t(($) => $["city"]),
minLength: String(MIN_LENGTH_2)
}
}
}
],
stateOrProvince: [
{
modes: ["blur"],
validate: (val) => !!val,
errorMessage: "fieldIsRequired"
},
{
modes: ["blur"],
validate: (input) => !matchesRegex(NO_ALPHABET_UNICODE_REGEX, input),
errorMessage: "nameMustContainLetters"
},
{
modes: ["blur"],
validate: (val) => (isFeatureEnabled("EnableAllowTwoRepeatedCharactersInName") ? !hasMaximumTwoRepeatedCharacters(val) : !hasRepeatedCharacters(val)) && !isInvalidName(val),
errorMessage: "checkForTypos"
}
],
country: [
{
modes: ["blur"],
validate: (val) => !!val,
errorMessage: "fieldIsRequired"
},
{
modes: ["blur"],
validate: (input) => !matchesRegex(NO_ALPHABET_REGEX, input),
errorMessage: "nameMustContainLetters"
},
{
modes: ["blur"],
validate: (val) => !matchesRegex(EMOJI_REGEX, val) && !isInvalidName(val) && matchesRegex(ADDRESS_VALID_CHAR_REGEX, val),
errorMessage: "checkForTypos"
}
],
otherAddressInformation
};
};
var addressValidators = ({ isFeatureEnabled = () => false, country, t }) => ({ ...isFeatureEnabled?.("StrictNameAndAddressValidationV4") ? addressValidationRulesV4(country, t, isFeatureEnabled) : addressValidationRules(country) });
var preventPoBoxValidation = (cleanseAddress) => ({ address: {
modes: ["blur"],
errorMessage: "addressPoBoxError",
asyncValidate: async (address, context) => {
const formData = context?.state?.data ?? {};
const { country } = formData;
if (!address || !country) return true;
const text = formatAddressForCleanse({
...formData,
address
});
if (!text) return true;
try {
const { poBox } = await cleanseAddress({
text,
country
});
return !poBox;
} catch (error) {
logger.error("Error while checking if address is PO box", error);
return true;
}
}
} });
//#endregion
export { addressFormatters as a, useAddressCleanse as c, preventPoBoxValidation as i, useAddressCleanseImperatively as l, addressValidationRulesV4 as n, countrySpecificFormatters as o, addressValidators as r, formatAddressForCleanse as s, addressValidationRules as t };