-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0b5eeb
commit ed3a3ad
Showing
17 changed files
with
923 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* @flow */ | ||
import { FUNDING } from "@paypal/sdk-constants/src"; | ||
|
||
import { APP_SWITCH_RETURN_HASH } from "../constants"; | ||
|
||
export type AppSwitchResumeParams = {| | ||
orderID?: ?string, | ||
buttonSessionID: string, | ||
payerID?: ?string, | ||
billingToken?: ?string, | ||
vaultSetupToken?: ?string, | ||
paymentID?: ?string, | ||
subscriptionID?: ?string, | ||
fundingSource?: ?$Values<typeof FUNDING>, | ||
checkoutState: "onApprove" | "onCancel" | "onError", | ||
|}; | ||
|
||
export function getAppSwitchResumeParams(): AppSwitchResumeParams | null { | ||
const urlHash = String(window.location.hash).replace("#", ""); | ||
const isPostApprovalAction = [ | ||
APP_SWITCH_RETURN_HASH.ONAPPROVE, | ||
APP_SWITCH_RETURN_HASH.ONCANCEL, | ||
APP_SWITCH_RETURN_HASH.ONERROR, | ||
].includes(urlHash); | ||
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; | ||
} | ||
|
||
export function isAppSwitchResumeFlow(): boolean { | ||
const params = getAppSwitchResumeParams(); | ||
return params !== null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* @flow */ | ||
|
||
import { vi, describe, expect } from "vitest"; | ||
|
||
import { | ||
isAppSwitchResumeFlow, | ||
getAppSwitchResumeParams, | ||
} from "./appSwitchResume"; | ||
|
||
describe("app switch resume flow", () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
vi.unstubAllGlobals(); | ||
}); | ||
const buttonSessionID = "uid_button_session_123444"; | ||
const orderID = "EC-1223114"; | ||
const fundingSource = "paypal"; | ||
|
||
test("should test fetching resume params when its non resume flow", () => { | ||
const params = getAppSwitchResumeParams(); | ||
|
||
expect.assertions(2); | ||
expect(params).toEqual(null); | ||
expect(isAppSwitchResumeFlow()).toEqual(false); | ||
}); | ||
|
||
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}`, | ||
}); | ||
|
||
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}`, | ||
}); | ||
|
||
const params = getAppSwitchResumeParams(); | ||
|
||
expect.assertions(2); | ||
expect(params).toEqual(null); | ||
expect(isAppSwitchResumeFlow()).toEqual(false); | ||
}); | ||
|
||
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}`, | ||
}); | ||
|
||
const params = getAppSwitchResumeParams(); | ||
|
||
expect.assertions(2); | ||
expect(params).toEqual(null); | ||
expect(isAppSwitchResumeFlow()).toEqual(false); | ||
}); | ||
|
||
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}&billingToken=BA-124&payerID=PP-122&paymentID=PAY-123&subscriptionID=I-1234&vaultSetupToken=VA-3`, | ||
}); | ||
|
||
const params = getAppSwitchResumeParams(); | ||
|
||
expect.assertions(2); | ||
expect(params).toEqual({ | ||
billingToken: "BA-124", | ||
buttonSessionID, | ||
checkoutState: "onApprove", | ||
fundingSource, | ||
orderID, | ||
payerID: "PP-122", | ||
paymentID: "PAY-123", | ||
subscriptionID: "I-1234", | ||
vaultSetupToken: "VA-3", | ||
}); | ||
expect(isAppSwitchResumeFlow()).toEqual(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.