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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTPPCPSDK-2227: Return V2 Variables from getHostedButtonDetails 馃懢 #2385

Merged
merged 6 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@
"@paypal/sdk-logos": "^2.2.6"
},
"lint-staged": {
"**/{src,test,scripts}/**/*.{js,jsx,json,sh}": "prettier --write"
"**/*": "prettier --write --ignore-unknown"
}
}
9 changes: 9 additions & 0 deletions src/hosted-buttons/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export type HostedButtonsInstance = {|
render: (string | HTMLElement) => Promise<void>,
|};

export type HostedButtonPreferences = {|
buttonPreferences: $ReadOnlyArray<string>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Possible to narrow these string types to known preferences and sources? Could do that in follow-up implementation PR too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Donezo funzo

eligibleFundingMethods: $ReadOnlyArray<string>,
|};

export type HostedButtonDetailsParams =
(HostedButtonsComponentProps) => Promise<{|
html: string,
Expand All @@ -43,7 +48,11 @@ export type HostedButtonDetailsParams =
shape: string,
color: string,
label: string,
height: string,
|},
version: ?string,
buttonContainerId: ?string,
preferences?: HostedButtonPreferences,
|}>;

export type ButtonVariables = $ReadOnlyArray<{|
Expand Down
12 changes: 11 additions & 1 deletion src/hosted-buttons/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,26 @@ export const getHostedButtonDetails: HostedButtonDetailsParams = async ({

// $FlowIssue request returns ZalgoPromise
const { body } = response;
const variables = body.button_details.link_variables;
const { link_variables: variables, preferences } = body.button_details;

return {
style: {
layout: getButtonVariable(variables, "layout"),
shape: getButtonVariable(variables, "shape"),
color: getButtonVariable(variables, "color"),
label: getButtonVariable(variables, "button_text"),
height: getButtonVariable(variables, "height"),
},
version: body.version,
buttonContainerId: body.button_container_id,
html: body.html,
htmlScript: body.html_script,
...(preferences && {
preferences: {
buttonPreferences: preferences.button_preferences,
eligibleFundingMethods: preferences.eligible_funding_methods,
},
}),
};
};

Expand Down
133 changes: 88 additions & 45 deletions src/hosted-buttons/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,6 @@ const merchantId = "M1234567890";
const orderID = "EC-1234567890";
const clientId = "C1234567890";

const getHostedButtonDetailsResponse = {
body: {
button_details: {
link_variables: [
{
name: "business",
value: merchantId,
},
{
name: "shape",
value: "rect",
},
{
name: "layout",
value: "vertical",
},
{
name: "color",
value: "gold",
},
{
name: "button_text",
value: "paypal",
},
],
},
},
};

const mockCreateAccessTokenRequest = () =>
// eslint-disable-next-line compat/compat
Promise.resolve({
Expand All @@ -74,24 +45,96 @@ const mockCreateAccessTokenRequest = () =>
},
});

test("getHostedButtonDetails", async () => {
// $FlowIssue
request.mockImplementationOnce(() =>
// eslint-disable-next-line compat/compat
Promise.resolve(getHostedButtonDetailsResponse)
);
await getHostedButtonDetails({
hostedButtonId,
fundingSources: [],
}).then(({ style }) => {
expect(style).toEqual({
layout: "vertical",
shape: "rect",
color: "gold",
label: "paypal",
describe("getHostedButtonDetails", () => {
const getHostedButtonDetailsResponse = {
v1: {
body: {
button_details: {
link_variables: [
{
name: "business",
value: merchantId,
},
{
name: "shape",
value: "rect",
},
{
name: "layout",
value: "vertical",
},
{
name: "color",
value: "gold",
},
{
name: "button_text",
value: "paypal",
},
],
},
},
},

v2: {
body: {
button_details: {
link_variables: [
{
name: "height",
value: 50,
},
],
preferences: {
button_preferences: ["paypal", "paylater"],
eligible_funding_methods: ["paypal", "venmo", "paylater"],
},
},
version: "2",
},
},
};

test("version 1", async () => {
// $FlowIssue
request.mockImplementationOnce(() =>
// eslint-disable-next-line compat/compat
Promise.resolve(getHostedButtonDetailsResponse.v1)
);
await getHostedButtonDetails({
hostedButtonId,
fundingSources: [],
}).then(({ style }) => {
expect(style).toEqual({
layout: "vertical",
shape: "rect",
color: "gold",
label: "paypal",
height: "",
});
});
expect.assertions(1);
});

test("version 2", async () => {
// $FlowIssue
request.mockImplementationOnce(() =>
// eslint-disable-next-line compat/compat
Promise.resolve(getHostedButtonDetailsResponse.v2)
);
await getHostedButtonDetails({
hostedButtonId,
fundingSources: [],
}).then(({ style, preferences, version }) => {
expect(style.height).toEqual(50);
expect(preferences).toEqual({
buttonPreferences: ["paypal", "paylater"],
eligibleFundingMethods: ["paypal", "venmo", "paylater"],
});
expect(version).toEqual("2");
});
expect.assertions(3);
});
expect.assertions(1);
});

describe("createAccessToken", () => {
Expand Down