Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@paypal/checkout-components

Package Overview
Dependencies
Maintainers
0
Versions
502
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@paypal/checkout-components - npm Package Compare versions

Comparing version 5.0.344 to 5.0.345-alpha-3176b3c.0

src/three-domain-secure/types.js

2

package.json
{
"name": "@paypal/checkout-components",
"version": "5.0.344",
"version": "5.0.345-alpha-3176b3c.0",
"description": "PayPal Checkout components, for integrating checkout products.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -5,3 +5,4 @@ /* @flow */

import { supportsPopups } from "@krakenjs/belter/src";
import { isPayPalDomain } from "@paypal/sdk-client/src";
import { getEnv, isPayPalDomain } from "@paypal/sdk-client/src";
import { ENV } from "@paypal/sdk-constants/src";

@@ -32,1 +33,11 @@ export function allowIframe(): boolean {

/* eslint-enable no-confusing-arrow */
// $FlowIssue
export const localOrStageExport = (unprotectedExport) => {
const env = getEnv();
if (env === ENV.LOCAL || env === ENV.STAGE) {
return unprotectedExport;
} else {
return undefined;
}
};
/* @flow */
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable no-restricted-globals, promise/no-native */
import { type LoggerType } from "@krakenjs/beaver-logger/src";
import { type ZoidComponent } from "@krakenjs/zoid/src";
import { ZalgoPromise } from "@krakenjs/zalgo-promise/src";
import { create, type ZoidComponent } from "@krakenjs/zoid/src";
import { FPTI_KEY } from "@paypal/sdk-constants/src";

@@ -9,8 +11,14 @@

type SdkConfig = {|
sdkToken: ?string,
|};
import type {
requestData,
responseBody,
Request,
MerchantPayloadData,
SdkConfig,
threeDSResponse,
} from "./types";
import { getThreeDomainSecureComponent } from "./utils";
const parseSdkConfig = ({ sdkConfig, logger }): SdkConfig => {
if (!sdkConfig.sdkToken) {
if (!sdkConfig.authenticationToken) {
throw new ValidationError(

@@ -27,30 +35,126 @@ `script data attribute sdk-client-token is required but was not passed`

};
const parseMerchantPayload = ({
merchantPayload,
}: {|
merchantPayload: MerchantPayloadData,
|}): requestData => {
const { threeDSRequested, amount, currency, nonce, transactionContext } =
merchantPayload;
return {
intent: "THREE_DS_VERIFICATION",
payment_source: {
card: {
single_use_token: nonce,
verification_method: threeDSRequested
? "SCA_ALWAYS"
: "SCA_WHEN_REQUIRED",
},
},
amount: {
currency_code: currency,
value: amount,
},
...transactionContext,
};
};
export interface ThreeDomainSecureComponentInterface {
isEligible(): ZalgoPromise<boolean>;
show(): ZoidComponent<void>;
isEligible(): Promise<boolean>;
show(): ZalgoPromise<threeDSResponse>;
}
export class ThreeDomainSecureComponent {
logger: LoggerType;
request: Request;
sdkConfig: SdkConfig;
authenticationURL: string;
threeDSIframe: ZoidComponent<void>;
constructor({
logger,
request,
sdkConfig,
}: {|
logger: LoggerType,
request: Request,
sdkConfig: SdkConfig,
|}) {
this.logger = logger;
this.request = request;
this.sdkConfig = parseSdkConfig({ sdkConfig, logger });
}
isEligible(): ZalgoPromise<boolean> {
return new ZalgoPromise((resolve) => {
resolve(false);
});
async isEligible(merchantPayload: MerchantPayloadData): Promise<boolean> {
const data = parseMerchantPayload({ merchantPayload });
try {
console.log("Entered isEligible");
// $FlowFixMe
const { status, links } = await this.request<requestData, responseBody>({
method: "POST",
url: `https://te-fastlane-3ds.qa.paypal.com:12326/v2/payments/payment`,
data,
accessToken: this.sdkConfig.authenticationToken,
});
let responseStatus = false;
if (status === "PAYER_ACTION_REQUIRED") {
this.authenticationURL = links.find(
(link) => link.rel === "payer-action"
).href;
responseStatus = true;
this.threeDSIframe = getThreeDomainSecureComponent(
this.authenticationURL
);
}
return responseStatus;
// eslint-disable-next-line no-unreachable
} catch (error) {
this.logger.warn(error);
throw error;
}
}
show() {
create({ tag: "", url: "" });
show(): ZalgoPromise<threeDSResponse> {
if (!this.threeDSIframe) {
return new ValidationError(`Ineligible for three domain secure`);
}
const promise = new ZalgoPromise();
const cancelThreeDS = () => {
return ZalgoPromise.try(() => {
// eslint-disable-next-line no-console
console.log("cancelled");
}).then(() => {
// eslint-disable-next-line no-use-before-define
instance.close();
});
};
const instance = this.threeDSIframe({
onSuccess: (data) => {
// const {threeDSRefID, authentication_status, liability_shift } = data;
// let enrichedNonce;
// if(threeDSRefID) {
// enrichedNonce = await updateNonceWith3dsData(threeDSRefID, this.fastlaneNonce)
// }
return promise.resolve(data);
},
onClose: cancelThreeDS,
onError: (err) => {
return promise.reject(
new Error(
`Error with obtaining 3DS contingency, ${JSON.stringify(err)}`
)
);
},
});
const TARGET_ELEMENT = {
BODY: "body",
};
return instance
.renderTo(window.parent, TARGET_ELEMENT.BODY)
.then(() => promise)
.finally(instance.close);
}
}
/* @flow */
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable no-restricted-globals, promise/no-native, compat/compat */
import { describe, expect, vi } from "vitest";

@@ -7,7 +9,23 @@

const defaultSdkConfig = {
sdkToken: "sdk-client-token",
authenticationToken: "sdk-client-token",
};
const defaultEligibilityResponse = {
status: "PAYER_ACTION_REQUIRED",
links: [{ href: "https://testurl.com", rel: "payer-action" }],
};
const defaultMerchantPayload = {
amount: "1.00",
currency: "USD",
nonce: "test-nonce",
};
const mockEligibilityRequest = (body = defaultEligibilityResponse) => {
return vi.fn().mockResolvedValue(body);
};
const createThreeDomainSecureComponent = ({
sdkConfig = defaultSdkConfig,
request = mockEligibilityRequest(),
logger = {

@@ -22,4 +40,7 @@ info: vi.fn().mockReturnThis(),

new ThreeDomainSecureComponent({
// $FlowFixMe
sdkConfig,
// $FlowIssue
request,
// $FlowIssue
logger,

@@ -33,13 +54,83 @@ });

describe("three domain secure component - isEligible method", () => {
test("should return false", async () => {
const threeDomainSecuretClient = createThreeDomainSecureComponent();
const eligibility = await threeDomainSecuretClient.isEligible();
test("should return true if payer action required", async () => {
const threeDomainSecureClient = createThreeDomainSecureComponent();
const eligibility = await threeDomainSecureClient.isEligible(
defaultMerchantPayload
);
expect(eligibility).toEqual(true);
});
test("should return false if payer action is not returned", async () => {
const threeDomainSecureClient = createThreeDomainSecureComponent({
request: () =>
Promise.resolve({ ...defaultEligibilityResponse, status: "SUCCESS" }),
});
const eligibility = await threeDomainSecureClient.isEligible(
defaultMerchantPayload
);
expect(eligibility).toEqual(false);
});
test("should assign correct URL to authenticationURL", async () => {
const threeDomainSecureClient = createThreeDomainSecureComponent({
request: () =>
Promise.resolve({
...defaultEligibilityResponse,
links: [
{ href: "https://not-payer-action.com", rel: "not-payer-action" },
...defaultEligibilityResponse.links,
],
}),
});
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
expect(threeDomainSecureClient.authenticationURL).toEqual(
"https://testurl.com"
);
});
test("create payload with correctly parameters", async () => {
const mockedRequest = mockEligibilityRequest();
const threeDomainSecureClient = createThreeDomainSecureComponent({
request: mockedRequest,
});
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
expect(mockedRequest).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
intent: "THREE_DS_VERIFICATION",
payment_source: expect.objectContaining({
card: expect.objectContaining({
single_use_token: defaultMerchantPayload.nonce,
verification_method: "SCA_WHEN_REQUIRED",
}),
}),
amount: expect.objectContaining({
currency_code: defaultMerchantPayload.currency,
value: defaultMerchantPayload.amount,
}),
}),
})
);
});
test("catch errors from the API", async () => {
const mockRequest = vi.fn().mockRejectedValue(new Error("Error with API"));
const threeDomainSecureClient = createThreeDomainSecureComponent({
request: mockRequest,
});
expect.assertions(2);
await expect(() =>
threeDomainSecureClient.isEligible(defaultMerchantPayload)
).rejects.toThrow(new Error("Error with API"));
expect(mockRequest).toHaveBeenCalled();
});
});
describe("three domain descure component - show method", () => {
test.skip("should return a zoid component", () => {
const threeDomainSecuretClient = createThreeDomainSecureComponent();
threeDomainSecuretClient.show();
test.todo("should return a zoid component", () => {
const threeDomainSecureClient = createThreeDomainSecureComponent();
threeDomainSecureClient.show();
// create test for zoid component

@@ -55,3 +146,3 @@ });

...defaultSdkConfig,
sdkToken: "",
authenticationToken: "",
},

@@ -58,0 +149,0 @@ })

/* @flow */
import { getLogger, getSDKToken } from "@paypal/sdk-client/src";
import {
getLogger,
getPayPalAPIDomain,
getUserIDToken,
} from "@paypal/sdk-client/src";
import { callRestAPI, localOrStageExport } from "../lib";
import type { LazyExport } from "../types";
import { protectedExport } from "../lib";

@@ -17,8 +21,11 @@ import {

logger: getLogger(),
// $FlowIssue ZalgoPromise vs Promise
request: callRestAPI,
sdkConfig: {
sdkToken: getSDKToken(),
authenticationToken: getUserIDToken(),
paypalApiDomain: getPayPalAPIDomain(),
},
});
return protectedExport({
isEligible: () => threeDomainSecureInstance.isEligible(),
return localOrStageExport({
isEligible: (payload) => threeDomainSecureInstance.isEligible(payload),
show: () => threeDomainSecureInstance.show(),

@@ -25,0 +32,0 @@ });

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc