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 support for 'payment-flow' query param for the web-sdk bridge #218

Merged
merged 1 commit into from
Oct 21, 2024
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
20 changes: 17 additions & 3 deletions server/meta.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,31 @@ function validateWebSDKUrl({ pathname, query }) {
);
}
// check for extraneous parameters
Object.keys(query).forEach((param) => {
if (param !== "version" && param !== "origin") {
const validWebSDKBridgeParams = ["origin", "version", "payment-flow"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add 'debug' here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point @dtjones404! I'll send a follow up PR for adding support for the optional debug param.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dtjones404 here's the follow up PR to add support for the debug query param: #219

for (const param of Object.keys(query)) {
if (!validWebSDKBridgeParams.includes(param)) {
throw new Error(`Invalid parameter on web-sdk bridge url: ${param}`);
}
});
}

// validate the version parameter
if (query.version === undefined || !semverRegex.test(query.version)) {
throw new Error(
`Invalid version parameter on web-sdk bridge url: ${query.version}`
);
}

// validate the payment-flow parameter
const validPaymentFlows = ["popup", "modal", "payment-handler"];
if (
query["payment-flow"] === undefined ||
!validPaymentFlows.includes(query["payment-flow"])
) {
throw new Error(
`Invalid payment-flow parameter on web-sdk bridge url: ${query["payment-flow"]}`
);
}

// validate the origin parameter
let url = null;
try {
Expand Down
40 changes: 33 additions & 7 deletions server/meta.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ test("should error when invalid characters are found in the subdomain - we allow

test("should construct a valid web-sdk bridge url", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000";
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler";
const sdkUID = "abc123";

const { getSDKLoader } = unpackSDKMeta(
Expand Down Expand Up @@ -1253,7 +1253,7 @@ test("should construct a valid web-sdk bridge url", () => {

test("should error when extra parameters are present", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&name=value";
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler&name=value";

let error = null;
try {
Expand All @@ -1278,7 +1278,7 @@ test("should error when extra parameters are present", () => {

test("should error when the version parameter is missing", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?origin=https%3A%2F%2Fwww.example.com%3A8000";
"https://www.paypal.com/web-sdk/v6/bridge?origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler";

let error = null;
try {
Expand All @@ -1303,7 +1303,7 @@ test("should error when the version parameter is missing", () => {

test("should error when the version parameter is invalid", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?version=^1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000";
"https://www.paypal.com/web-sdk/v6/bridge?version=^1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler";

let error = null;
try {
Expand All @@ -1327,7 +1327,8 @@ test("should error when the version parameter is invalid", () => {
});

test("should error when the origin parameter is missing", () => {
const sdkUrl = "https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3";
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&payment-flow=payment-handler";

let error = null;
try {
Expand All @@ -1352,7 +1353,7 @@ test("should error when the origin parameter is missing", () => {

test("should error when the origin parameter is invalid", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=example";
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=example&payment-flow=payment-handler";

let error = null;
try {
Expand All @@ -1377,7 +1378,32 @@ test("should error when the origin parameter is invalid", () => {

test("should error when the origin parameter is not just the origin", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000%2Fpath";
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000%2Fpath&payment-flow=payment-handler";

let error = null;
try {
unpackSDKMeta(
Buffer.from(
JSON.stringify({
url: sdkUrl,
attrs: {
"data-uid": "abc123",
},
})
).toString("base64")
);
} catch (err) {
error = err;
}

if (!error) {
throw new Error("Expected error to be thrown");
}
});

test("should error when the payment-flow parameter is invalid", () => {
const sdkUrl =
"https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=invalid-payment-flow-value";

let error = null;
try {
Expand Down
Loading