diff --git a/src/index.tsx b/src/index.tsx index 2d4a863..6cd2fba 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -203,17 +203,18 @@ class PayPalButton extends React.Component private addPaypalSdk() { const { options, onButtonReady } = this.props; - const queryParams: string[] = []; - // replacing camelCase with dashes - Object.keys(options).forEach(k => { - const name = k.split(/(?=[A-Z])/).join("-").toLowerCase(); - queryParams.push(`${name}=${options[k]}`); - }); + const separator = (key: string): string => key.split(/(?=[A-Z])/).join("-").toLowerCase(); + const createQueryParam = (object: object, modifier: Function): string => Object.entries(object) + .reduce((acc: string [], [key, value]: [string, string]) => { + acc.push(`${modifier(key)}=${value}`); + return acc; + }, []).join("&"); + const queryParam = createQueryParam({...PayPalButton.defaultProps.options, ...options}, separator); const script = document.createElement("script"); script.type = "text/javascript"; - script.src = `https://www.paypal.com/sdk/js?${queryParams.join("&")}`; + script.src = `https://www.paypal.com/sdk/js?${queryParam}`; script.async = true; script.onload = () => { this.setState({ isSdkReady: true }); @@ -225,7 +226,7 @@ class PayPalButton extends React.Component script.onerror = () => { throw new Error("Paypal SDK could not be loaded."); }; - + document.body.appendChild(script); } }