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

add validations for isEligible merchant payload #2459

Merged
merged 2 commits into from
Jan 3, 2025
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
47 changes: 46 additions & 1 deletion src/three-domain-secure/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { type LoggerType } from "@krakenjs/beaver-logger/src";
import { type ZoidComponent } from "@krakenjs/zoid/src";
import { base64encode } from "@krakenjs/belter/src";
import { ZalgoPromise } from "@krakenjs/zalgo-promise/src";
import { FPTI_KEY } from "@paypal/sdk-constants/src";
import { FPTI_KEY, CURRENCY } from "@paypal/sdk-constants/src";

import { PAYMENT_3DS_VERIFICATION, AUTH } from "../constants/api";
import { ValidationError } from "../lib";
Expand Down Expand Up @@ -94,6 +94,8 @@ export class ThreeDomainSecureComponent {
}

async isEligible(merchantPayload: MerchantPayloadData): Promise<boolean> {
this.validateMerchantPayload(merchantPayload);

const data = parseMerchantPayload({ merchantPayload });
this.fastlaneNonce = merchantPayload.nonce;

Expand Down Expand Up @@ -218,6 +220,49 @@ export class ThreeDomainSecureComponent {
});
}

validateMerchantPayload(merchantPayload: MerchantPayloadData): void {
// TODO we have a ticket to standardize client-side validations
// eslint-disable-next-line flowtype/no-weak-types
const isRequired = (value: any) => Boolean(value);
// eslint-disable-next-line flowtype/no-weak-types
const isString = (value: any) => typeof value === "string";

const validations = {
amount: {
test: [isString, isRequired],
message: (value) =>
`[amount] is required and must be a string. received: ${value}`,
},
currency: {
test: [(value) => value in CURRENCY, isRequired],
message: (value) =>
`[currency] is required and must be a valid currency. received: ${value}`,
},
nonce: {
test: [isString, isRequired],
message: (value) =>
`[nonce] is required and must be a string. received: ${value}`,
},
};

const errors = [];

Object.entries(merchantPayload).forEach(([key, value]) => {
if (key in validations) {
if (!validations[key]?.test?.every((validation) => validation(value))) {
errors.push(validations[key]?.message(value));
}
}
});

if (errors.length) {
const joinedErrors = errors.join("\n");

this.logger.warn(joinedErrors);
throw new ValidationError(joinedErrors);
}
}

updateNonceWith3dsData(threeDSRefID: string): Promise<GqlResponse> {
// $FlowFixMe Zalgopromise not recognized
return this.graphQLClient.request({
Expand Down
51 changes: 51 additions & 0 deletions src/three-domain-secure/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { describe, expect, vi, afterEach } from "vitest";
import { getEnv } from "@paypal/sdk-client/src";
import { FPTI_KEY } from "@paypal/sdk-constants/src";

import { ValidationError } from "../lib";

import { ThreeDomainSecureComponent } from "./component";

const defaultSdkConfig = {
Expand Down Expand Up @@ -150,6 +152,55 @@ describe("three domain secure component - isEligible method", () => {
).rejects.toThrow(new Error("Error with API"));
expect(mockRestClient.request).toHaveBeenCalled();
});

test.each([
[
"undefined nonce",
{ currency: "USD", amount: "12.00", nonce: undefined },
"[nonce] is required and must be a string. received: undefined",
],

[
"undefined currency",
{ currency: undefined, amount: "12.00", nonce: "abc-nonce" },
"[currency] is required and must be a valid currency. received: undefined",
],
[
"invalid currency",
{ currency: "FOO", amount: "12.00", nonce: "abc-nonce" },
"[currency] is required and must be a valid currency. received: FOO",
],
[
"amount as a number",
{ currency: "USD", amount: 12, nonce: "abc-nonce" },
"[amount] is required and must be a string. received: 12",
],
[
"undefined amount",
{ currency: "USD", amount: undefined, nonce: "abc-nonce" },
"[amount] is required and must be a string. received: undefined",
],
[
"multiple errors",
{ currency: undefined, amount: undefined, nonce: undefined },
"[currency] is required and must be a valid currency. received: undefined\n" +
"[amount] is required and must be a string. received: undefined\n" +
"[nonce] is required and must be a string. received: undefined",
],
])(
"should throw validation error on %s",
async (_assertion, params, expected) => {
const threeDomainSecureClient = createThreeDomainSecureComponent();

await expect(() =>
threeDomainSecureClient.isEligible(params)
).rejects.toThrow(new ValidationError(expected));

expect(threeDomainSecureClient.logger.warn).toHaveBeenCalledWith(
expected
);
}
);
});

describe.todo("three domain descure component - show method", () => {
Expand Down
Loading