Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for both new and legacy ID types on product selection page #134

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
id_type: "PASSPORT",
verification_method: "doc_verification",
},
{
country: "ZA",
id_type: "IDENTITY_CARD",
verification_method: "enhanced_document_verification",
},
],
callback_url:
"https://portal.smileidentity.com/api/v2/007/postback/update_status",
Expand Down
21 changes: 21 additions & 0 deletions packages/embed/cypress/tests/verification-method-selection.cy.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
describe("Verification Method Selection", () => {
beforeEach(() => {
cy.loadIDOptions();
});
describe("multiple countries / id_types", () => {
beforeEach(() => {
cy.visit("/verification-method-selection");
Expand Down Expand Up @@ -38,6 +41,24 @@ describe("Verification Method Selection", () => {
"http://localhost:8000/doc-verification.html",
);
});

it("should redirect to the enhanced document verification sequence in a nested iframe", () => {
cy.getIFrameBody().find("#country").select("ZA");

cy.getIFrameBody().find("#id_type").select("IDENTITY_CARD");

cy.getIFrameBody().find("#submitConfig").click();

cy.getIFrameBody()
.find(
'iframe[data-cy="smile-identity-hosted-web-integration-post-product-selection"]',
)
.should(
"have.attr",
"src",
"http://localhost:8000/enhanced-document-verification.html",
);
});
});

describe("enhanced_kyc", () => {
Expand Down
146 changes: 113 additions & 33 deletions packages/embed/src/js/product-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// NOTE: In order to support prior integrations, we have `live` and
// `production` pointing to the same URL
const endpoints = {
sandbox: "https://testapi.smileidentity.com",
live: "https://api.smileidentity.com",
production: "https://api.smileidentity.com",
sandbox: "https://testapi.smileidentity.com/v1",
live: "https://api.smileidentity.com/v1",
production: "https://api.smileidentity.com/v1",
};

const referenceWindow = window.parent;
Expand Down Expand Up @@ -36,32 +36,115 @@
activeScreen = element;
}

function getIdTypeName(country, id_type, services) {
if (
services.hosted_web.doc_verification[country] &&
services.hosted_web.doc_verification[country].id_types[id_type]
) {
return services.hosted_web.doc_verification[country].id_types[id_type]
.label;
async function getProductConstraints() {
const payload = {
token: config.token,
partner_id: config.partner_details.partner_id,
};

const fetchConfig = {
cache: "no-cache",
mode: "cors",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(payload),
};

try {
const response = await fetch(
`${endpoints[config.environment]}/valid_documents`,
fetchConfig,
);
const json = await response.json();

return json.valid_documents;
} catch (e) {
throw new Error("Failed to get supported ID types", { cause: e });
}
}

async function getLegacyProductConstraints() {
const fetchConfig = {
cache: "no-cache",
mode: "cors",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
method: "GET",
};

try {
const response = await fetch(
`${endpoints[config.environment]}/services`,
fetchConfig,
);
const json = await response.json();

return json.hosted_web;
} catch (e) {
throw new Error("Failed to get supported ID types", { cause: e });
}
}

function getIdTypeName(
country,
id_type,
productConstraints,
legacyProductConstraints,
) {
const validDocumentsCountry = productConstraints.find(
(item) => item.country.code === country,
);

if (!validDocumentsCountry) {
throw new Error(`SmileIdentity - ${country} is not supported`);
}
if (
services.hosted_web.enhanced_kyc[country] &&
services.hosted_web.enhanced_kyc[country].id_types[id_type]
) {
return services.hosted_web.enhanced_kyc[country].id_types[id_type].label;

let validIdType = validDocumentsCountry.id_types.find(
(item) => item.code === id_type,
);

if (!validIdType) {
const legacyValidIdType =
legacyProductConstraints.doc_verification[country].id_types[id_type] ||
legacyProductConstraints.enhanced_kyc[country].id_types[id_type];
if (legacyValidIdType) {
validIdType = {
name: legacyValidIdType.label,
};
}
}

if (!validIdType) {
throw new Error(`SmileIdentity - ${country} ${id_type} is not supported`);
}
throw new Error("Cannot find the full name of the id_type");

return validIdType.name;
}

function transformIdTypesToVerificationMethodMap(idTypes, services) {
function transformIdTypesToVerificationMethodMap(
idTypes,
productConstraints,
legacyProductConstraints,
) {
return idTypes.reduce(
(idSelectionMap, { country, id_type, verification_method }) => {
idSelectionMap[country] = idSelectionMap[country] || {
name: services.hosted_web.doc_verification[country].name,
name: productConstraints.find((item) => item.country.code === country)
?.country?.name,
id_types: {},
};
idSelectionMap[country].id_types[id_type] = {
name: getIdTypeName(country, id_type, services),
name: getIdTypeName(
country,
id_type,
productConstraints,
legacyProductConstraints,
),
verification_method,
};

Expand Down Expand Up @@ -246,21 +329,18 @@
config = JSON.parse(event.data);
activeScreen = LoadingScreen;

const servicesResponse = await fetch(
`${endpoints[config.environment]}/v1/services`,
const constraintsPromises = [
getProductConstraints(),
getLegacyProductConstraints(),
];
const [productConstraints, legacyConstraints] =
await Promise.all(constraintsPromises);
verificationMethodMap = transformIdTypesToVerificationMethodMap(
config.id_types,
productConstraints,
legacyConstraints,
);

if (servicesResponse.ok) {
const services = await servicesResponse.json();

verificationMethodMap = transformIdTypesToVerificationMethodMap(
config.id_types,
services,
);
initializeForm(SelectIdType, verificationMethodMap);
} else {
throw Error("Failed to get supported ID types");
}
initializeForm(SelectIdType, verificationMethodMap);
} else if (event.data.includes("SmileIdentity::ChildPageReady")) {
publishMessage(config);
}
Expand Down