Skip to content

Commit

Permalink
feat: add HostedButtons component
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl committed Nov 22, 2023
1 parent a5e9424 commit 17b795d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions types/components/hosted-buttons.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface PayPalHostedButtonsComponentOptions {
hostedButtonId: string;
}

export interface PayPalHostedButtonsComponent {
render: (container: HTMLElement | string) => Promise<void>;
}
7 changes: 7 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ import type {
rememberFunding,
} from "./components/funding-eligibility";
import type { PayPalHostedFieldsComponent } from "./components/hosted-fields";
import type {
PayPalHostedButtonsComponentOptions,
PayPalHostedButtonsComponent,
} from "./components/hosted-buttons";

export interface PayPalNamespace {
Buttons?: (
options?: PayPalButtonsComponentOptions,
) => PayPalButtonsComponent;
HostedButtons?: (
options?: PayPalHostedButtonsComponentOptions,
) => PayPalHostedButtonsComponent;
Marks?: (options?: PayPalMarksComponentOptions) => PayPalMarksComponent;
Messages?: (
options?: PayPalMessagesComponentOptions,
Expand Down
28 changes: 28 additions & 0 deletions types/tests/hosted-buttons.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { loadScript } from "../../src/index";
import type { PayPalNamespace } from "../index";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function main() {
let paypal: PayPalNamespace | null;

try {
paypal = await loadScript({
clientId: "test",
components: "buttons",
});
} catch (err) {
throw new Error(`Failed to load the paypal sdk script: ${err}`);
}

if (!paypal?.HostedButtons) {
throw new Error("Invalid paypal object for buttons component");
}

paypal.HostedButtons().render("#container");
paypal.HostedButtons().render(document.createElement("div"));
paypal
.HostedButtons({
hostedButtonId: "B123456789",
})
.render("#container");
}

0 comments on commit 17b795d

Please sign in to comment.