Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wsbrunson committed Jan 23, 2024
1 parent 1172445 commit 469e19a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,16 @@
"vitest": "^0.25.3"
},
"dependencies": {
"@fingerprintjs/fingerprintjs-pro": "^3.8.6",
"@krakenjs/belter": "^2.0.0",
"@krakenjs/cross-domain-utils": "^3.0.0",
"@krakenjs/jsx-pragmatic": "^3",
"@krakenjs/post-robot": "^11.0.0",
"@krakenjs/zalgo-promise": "^2.0.0",
"@krakenjs/zoid": "^10.3.1",
"@paypal/common-components": "^1.0.35",
"@paypal/funding-components": "^1.0.31",
"@paypal/connect-loader-component": "1.1.1",
"@paypal/funding-components": "^1.0.31",
"@paypal/sdk-client": "^4.0.179",
"@paypal/sdk-constants": "^1.0.133",
"@paypal/sdk-logos": "^2.2.6"
Expand Down
12 changes: 9 additions & 3 deletions src/api/shopper-insights/component.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* @flow */
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable no-restricted-globals, promise/no-native */

import {
getUserIDToken,
Expand Down Expand Up @@ -31,6 +33,8 @@ import {
hasEmail,
hasPhoneNumber,
} from "./validation";
import { fingerprint } from "./fingerprint";
import { ShopperSession } from "./shopperSession";

type RecommendedPaymentMethods = {|
isPayPalRecommended: boolean,
Expand Down Expand Up @@ -58,6 +62,7 @@ type getRecommendedPaymentMethodsRequestPayload = {|

export type ShopperInsightsComponent = {|
getRecommendedPaymentMethods: (MerchantPayloadData) => ZalgoPromise<RecommendedPaymentMethods>,
identify: () => Promise<void>,
|};

function createRecommendedPaymentMethodsRequestPayload(
Expand Down Expand Up @@ -125,7 +130,9 @@ export function getShopperInsightsComponent(): ShopperInsightsComponent {
[FPTI_KEY.EVENT_NAME]: FPTI_TRANSITION.SHOPPER_INSIGHTS_API_INIT,
});

const shopperInsights = {
const shopperSession = new ShopperSession({ fingerprint });

return {
getRecommendedPaymentMethods: (merchantPayload) => {
validateMerchantConfig({ sdkToken, pageType, userIDToken, clientToken });
validateMerchantPayload(merchantPayload);
Expand Down Expand Up @@ -190,7 +197,6 @@ export function getShopperInsightsComponent(): ShopperInsightsComponent {
throw err;
});
},
identify: () => shopperSession.identify(),
};

return shopperInsights;
}
52 changes: 52 additions & 0 deletions src/api/shopper-insights/fingerprint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* @flow */
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable no-restricted-globals, promise/no-native */
import { load as loadFingerprintJS } from "@fingerprintjs/fingerprintjs-pro";

const __FINGERPRINT_JS_API_KEY__ = "Eh4QKkI51U0rVUUPeQT8";

type FingerprintResults = {|
requestId: string,
|};

type Fingerprinter = {|
get: () => Promise<FingerprintResults>,
|};

export class Fingerprint {
fingerprinter: ?Fingerprinter;
results: ?FingerprintResults;

async load(): Promise<void> {
if (!this.fingerprinter) {
this.fingerprinter = await loadFingerprintJS({
apiKey: __FINGERPRINT_JS_API_KEY__,
});
}
}

async collect(): Promise<FingerprintResults> {
if (this.results && this.results.requestId) {
return this.results;
}

if (!this.fingerprinter) {
await this.load();
}

if (!this.fingerprinter) {
throw new Error("fingerprint library failed to load");
}

this.results = await this.fingerprinter.get();

return this.results;
}

get(): Promise<FingerprintResults> {
return this.collect();
}
}

// $FlowIssue flow is bad with classes
export const fingerprint = new Fingerprint();
20 changes: 20 additions & 0 deletions src/api/shopper-insights/shopperSession.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* @flow */
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable no-restricted-globals, promise/no-native */

import { Fingerprint } from "./fingerprint";

export class ShopperSession {
requestId: string = "";
fingerprint: Fingerprint;

constructor({ fingerprint }: {| fingerprint: Fingerprint |}) {
this.fingerprint = fingerprint;
}

async identify(): Promise<void> {
const { requestId } = await this.fingerprint.get();

this.requestId = requestId;
}
}

0 comments on commit 469e19a

Please sign in to comment.