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

update resume flow return value parameter names #2461

Merged
merged 1 commit into from
Jan 14, 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
68 changes: 35 additions & 33 deletions src/lib/appSwitchResume.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */
import { FUNDING } from "@paypal/sdk-constants/src";
import { parseQuery } from "@krakenjs/belter/src";

import { APP_SWITCH_RETURN_HASH } from "../constants";

Expand All @@ -16,46 +17,47 @@ export type AppSwitchResumeParams = {|
|};

export function getAppSwitchResumeParams(): AppSwitchResumeParams | null {
const urlHash = String(window.location.hash).replace("#", "");
const hashString = window.location.hash && window.location.hash.slice(1);
const [hash, queryString] = hashString.split("?");

const isPostApprovalAction = [
APP_SWITCH_RETURN_HASH.ONAPPROVE,
APP_SWITCH_RETURN_HASH.ONCANCEL,
APP_SWITCH_RETURN_HASH.ONERROR,
].includes(urlHash);
].includes(hash);
if (!isPostApprovalAction) {
return null;
}
// eslint-disable-next-line compat/compat
const search = new URLSearchParams(window.location.search);
const orderID = search.get("orderID");
const payerID = search.get("payerID");
const buttonSessionID = search.get("buttonSessionID");
const billingToken = search.get("billingToken");
const paymentID = search.get("paymentID");
const subscriptionID = search.get("subscriptionID");
const vaultSetupToken = search.get("vaultSetupToken");
const fundingSource = search.get("fundingSource");
if (buttonSessionID) {
const params: AppSwitchResumeParams = {
orderID,
buttonSessionID,
payerID,
billingToken,
paymentID,
subscriptionID,
// URLSearchParams get returns as string,
// but below code excepts a value from list of string.
// $FlowIgnore[incompatible-type]
fundingSource,
vaultSetupToken,
// the isPostApprovalAction already ensures
// that the function will exit if url hash is not one of supported values.
// $FlowIgnore[incompatible-type]
checkoutState: urlHash,
};
return params;
}
return null;

const {
token,
PayerID,
Copy link
Member

Choose a reason for hiding this comment

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

oh they are doing PayerID and not payerID?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes.

Copy link
Member

Choose a reason for hiding this comment

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

I'll have to do a new PR too

buttonSessionID,
billingToken,
paymentID,
subscriptionID,
vaultSetupToken,
fundingSource,
} = parseQuery(queryString);

const params: AppSwitchResumeParams = {
orderID: token,
buttonSessionID,
payerID: PayerID,
billingToken,
paymentID,
subscriptionID,
// URLSearchParams get returns as string,
// but below code excepts a value from list of string.
// $FlowIgnore[incompatible-type]
fundingSource,
vaultSetupToken,
// the isPostApprovalAction already ensures
// that the function will exit if url hash is not one of supported values.
// $FlowIgnore[incompatible-type]
checkoutState: hash,
};
return params;
}

export function isAppSwitchResumeFlow(): boolean {
Expand Down
20 changes: 6 additions & 14 deletions src/lib/appSwithResume.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,25 @@ describe("app switch resume flow", () => {

test("should test fetching resume params when parameters are correctly passed", () => {
vi.spyOn(window, "location", "get").mockReturnValue({
hash: "#onApprove",
search: `buttonSessionID=${buttonSessionID}&orderID=${orderID}&fundingSource=${fundingSource}`,
hash: `#onApprove?buttonSessionID=${buttonSessionID}&token=${orderID}&fundingSource=${fundingSource}`,
});

const params = getAppSwitchResumeParams();

expect.assertions(2);
expect(params).toEqual({
billingToken: null,
buttonSessionID,
checkoutState: "onApprove",
fundingSource,
orderID,
payerID: null,
paymentID: null,
subscriptionID: null,
vaultSetupToken: null,
});
expect(isAppSwitchResumeFlow()).toEqual(true);
});

test("should test fetching resume params with invalid callback passed", () => {
vi.spyOn(window, "location", "get").mockReturnValue({
hash: "#Unknown",
search: `buttonSessionID=${buttonSessionID}&orderID=${orderID}&fundingSource=${fundingSource}`,
search: `buttonSessionID=${buttonSessionID}&token=${orderID}&fundingSource=${fundingSource}`,
});

const params = getAppSwitchResumeParams();
Expand All @@ -62,8 +56,7 @@ describe("app switch resume flow", () => {

test("should test null fetching resume params with invalid callback passed", () => {
vi.spyOn(window, "location", "get").mockReturnValue({
hash: "#Unknown",
search: `buttonSessionID=${buttonSessionID}&orderID=${orderID}&fundingSource=${fundingSource}`,
hash: `#Unknown?buttonSessionID=${buttonSessionID}&token=${orderID}&fundingSource=${fundingSource}`,
});

const params = getAppSwitchResumeParams();
Expand All @@ -73,10 +66,9 @@ describe("app switch resume flow", () => {
expect(isAppSwitchResumeFlow()).toEqual(false);
});

test("should test fetching resume params when parameters are correctly passed", () => {
test("should test fetching multiple resume params when parameters are correctly passed", () => {
vi.spyOn(window, "location", "get").mockReturnValue({
hash: "#onApprove",
search: `buttonSessionID=${buttonSessionID}&orderID=${orderID}&fundingSource=${fundingSource}&billingToken=BA-124&payerID=PP-122&paymentID=PAY-123&subscriptionID=I-1234&vaultSetupToken=VA-3`,
hash: `#onApprove?buttonSessionID=${buttonSessionID}&token=${orderID}&fundingSource=${fundingSource}&billingToken=BA-124&PayerID=PP-payer-122&paymentID=PAY-123&subscriptionID=I-1234&vaultSetupToken=VA-3`,
});

const params = getAppSwitchResumeParams();
Expand All @@ -88,7 +80,7 @@ describe("app switch resume flow", () => {
checkoutState: "onApprove",
fundingSource,
orderID,
payerID: "PP-122",
payerID: "PP-payer-122",
paymentID: "PAY-123",
subscriptionID: "I-1234",
vaultSetupToken: "VA-3",
Expand Down
Loading