From f640eec3403faf6180ce6a36261bc065f9fe794e Mon Sep 17 00:00:00 2001 From: SirSerje Date: Mon, 23 Dec 2019 16:30:18 +0200 Subject: [PATCH 1/2] #46 Missing default props on sdk request, when option prop --- src/index.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 2d4a863..4124d34 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -202,9 +202,16 @@ class PayPalButton extends React.Component } private addPaypalSdk() { - const { options, onButtonReady } = this.props; + const { onButtonReady } = this.props; + let { options } = this.props; const queryParams: string[] = []; + Object.keys(PayPalButton.defaultProps.options).map(i => { + if (!options.hasOwnProperty(i)) { + options = Object.assign(options, {[i]:PayPalButton.defaultProps.options[i]}); + } + }); + // replacing camelCase with dashes Object.keys(options).forEach(k => { const name = k.split(/(?=[A-Z])/).join("-").toLowerCase(); @@ -225,7 +232,7 @@ class PayPalButton extends React.Component script.onerror = () => { throw new Error("Paypal SDK could not be loaded."); }; - + document.body.appendChild(script); } } From 6d4040033c00afe68ab0fa0f69ddc1f86d8844f6 Mon Sep 17 00:00:00 2001 From: SirSerje Date: Mon, 23 Dec 2019 18:46:09 +0200 Subject: [PATCH 2/2] refactor addPaypalSdk method --- src/index.tsx | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 4124d34..6cd2fba 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -202,25 +202,19 @@ class PayPalButton extends React.Component } private addPaypalSdk() { - const { onButtonReady } = this.props; - let { options } = this.props; - const queryParams: string[] = []; + const { options, onButtonReady } = this.props; - Object.keys(PayPalButton.defaultProps.options).map(i => { - if (!options.hasOwnProperty(i)) { - options = Object.assign(options, {[i]:PayPalButton.defaultProps.options[i]}); - } - }); - - // 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 });