Skip to content

Commit df7a515

Browse files
authored
update resume flow contract (#2461)
1 parent a1618be commit df7a515

File tree

2 files changed

+41
-47
lines changed

2 files changed

+41
-47
lines changed

Diff for: src/lib/appSwitchResume.js

+35-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow */
22
import { FUNDING } from "@paypal/sdk-constants/src";
3+
import { parseQuery } from "@krakenjs/belter/src";
34

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

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

1819
export function getAppSwitchResumeParams(): AppSwitchResumeParams | null {
19-
const urlHash = String(window.location.hash).replace("#", "");
20+
const hashString = window.location.hash && window.location.hash.slice(1);
21+
const [hash, queryString] = hashString.split("?");
22+
2023
const isPostApprovalAction = [
2124
APP_SWITCH_RETURN_HASH.ONAPPROVE,
2225
APP_SWITCH_RETURN_HASH.ONCANCEL,
2326
APP_SWITCH_RETURN_HASH.ONERROR,
24-
].includes(urlHash);
27+
].includes(hash);
2528
if (!isPostApprovalAction) {
2629
return null;
2730
}
28-
// eslint-disable-next-line compat/compat
29-
const search = new URLSearchParams(window.location.search);
30-
const orderID = search.get("orderID");
31-
const payerID = search.get("payerID");
32-
const buttonSessionID = search.get("buttonSessionID");
33-
const billingToken = search.get("billingToken");
34-
const paymentID = search.get("paymentID");
35-
const subscriptionID = search.get("subscriptionID");
36-
const vaultSetupToken = search.get("vaultSetupToken");
37-
const fundingSource = search.get("fundingSource");
38-
if (buttonSessionID) {
39-
const params: AppSwitchResumeParams = {
40-
orderID,
41-
buttonSessionID,
42-
payerID,
43-
billingToken,
44-
paymentID,
45-
subscriptionID,
46-
// URLSearchParams get returns as string,
47-
// but below code excepts a value from list of string.
48-
// $FlowIgnore[incompatible-type]
49-
fundingSource,
50-
vaultSetupToken,
51-
// the isPostApprovalAction already ensures
52-
// that the function will exit if url hash is not one of supported values.
53-
// $FlowIgnore[incompatible-type]
54-
checkoutState: urlHash,
55-
};
56-
return params;
57-
}
58-
return null;
31+
32+
const {
33+
token,
34+
PayerID,
35+
buttonSessionID,
36+
billingToken,
37+
paymentID,
38+
subscriptionID,
39+
vaultSetupToken,
40+
fundingSource,
41+
} = parseQuery(queryString);
42+
43+
const params: AppSwitchResumeParams = {
44+
orderID: token,
45+
buttonSessionID,
46+
payerID: PayerID,
47+
billingToken,
48+
paymentID,
49+
subscriptionID,
50+
// URLSearchParams get returns as string,
51+
// but below code excepts a value from list of string.
52+
// $FlowIgnore[incompatible-type]
53+
fundingSource,
54+
vaultSetupToken,
55+
// the isPostApprovalAction already ensures
56+
// that the function will exit if url hash is not one of supported values.
57+
// $FlowIgnore[incompatible-type]
58+
checkoutState: hash,
59+
};
60+
return params;
5961
}
6062

6163
export function isAppSwitchResumeFlow(): boolean {

Diff for: src/lib/appSwithResume.test.js

+6-14
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,25 @@ describe("app switch resume flow", () => {
2626

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

3332
const params = getAppSwitchResumeParams();
3433

3534
expect.assertions(2);
3635
expect(params).toEqual({
37-
billingToken: null,
3836
buttonSessionID,
3937
checkoutState: "onApprove",
4038
fundingSource,
4139
orderID,
42-
payerID: null,
43-
paymentID: null,
44-
subscriptionID: null,
45-
vaultSetupToken: null,
4640
});
4741
expect(isAppSwitchResumeFlow()).toEqual(true);
4842
});
4943

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

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

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

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

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

8274
const params = getAppSwitchResumeParams();
@@ -88,7 +80,7 @@ describe("app switch resume flow", () => {
8880
checkoutState: "onApprove",
8981
fundingSource,
9082
orderID,
91-
payerID: "PP-122",
83+
payerID: "PP-payer-122",
9284
paymentID: "PAY-123",
9385
subscriptionID: "I-1234",
9486
vaultSetupToken: "VA-3",

0 commit comments

Comments
 (0)