diff --git a/src/renderer/component/cardVerify/view.jsx b/src/renderer/component/cardVerify/view.jsx index 5d3f3e007..0f3ff4fe0 100644 --- a/src/renderer/component/cardVerify/view.jsx +++ b/src/renderer/component/cardVerify/view.jsx @@ -7,20 +7,6 @@ let scriptLoading = false; let scriptLoaded = false; let scriptDidError = false; -declare class CardVerify { - static stripeHandler: { - open: Function, - close: Function, - }; -} - -declare class StripeCheckout { - static configure({}): { - open: Function, - close: Function, - }; -} - type Props = { disabled: boolean, label: ?string, @@ -41,20 +27,14 @@ type Props = { // token.id can be used to create a charge or customer. // token.email contains the email address entered by the user. token: string, - email: string, }; -type State = { - open: boolean, -}; - -class CardVerifyComponent extends React.Component { - constructor(props: Props) { +class CardVerify extends React.Component { + constructor(props) { super(props); this.state = { open: false, }; - this.onClick = this.onClick.bind(this); } componentDidMount() { @@ -85,14 +65,12 @@ class CardVerifyComponent extends React.Component { scriptDidError = true; scriptLoading = false; reject(event); - this.onScriptError(); + this.onScriptError(event); }; }); const wrappedPromise = new Promise((accept, cancel) => { - promise.then(() => (canceled ? cancel(new Error({ isCanceled: true })) : accept())); - promise.catch( - error => (canceled ? cancel(new Error({ isCanceled: true })) : cancel(error)) - ); + promise.then(() => (canceled ? cancel({ isCanceled: true }) : accept())); + promise.catch(error => (canceled ? cancel({ isCanceled: true }) : cancel(error))); }); return { @@ -105,9 +83,7 @@ class CardVerifyComponent extends React.Component { this.loadPromise.promise.then(this.onScriptLoaded).catch(this.onScriptError); - if (document.body) { - document.body.appendChild(script); - } + document.body.appendChild(script); } componentDidUpdate() { @@ -136,7 +112,7 @@ class CardVerifyComponent extends React.Component { } }; - onScriptError = () => { + onScriptError = (...args) => { throw new Error('Unable to load credit validation script.'); }; @@ -144,23 +120,6 @@ class CardVerifyComponent extends React.Component { this.setState({ open: false }); }; - onClick = () => { - if (scriptDidError) { - throw new Error('Tried to call onClick, but StripeCheckout failed to load'); - } else if (CardVerify.stripeHandler) { - this.showStripeDialog(); - } else { - this.hasPendingClick = true; - } - }; - - loadPromise: { - promise: Promise, - cancel: Function, - }; - - hasPendingClick: boolean; - updateStripeHandler() { if (!CardVerify.stripeHandler) { CardVerify.stripeHandler = StripeCheckout.configure({ @@ -183,6 +142,18 @@ class CardVerifyComponent extends React.Component { }); } + onClick = () => { + if (scriptDidError) { + try { + throw new Error('Tried to call onClick, but StripeCheckout failed to load'); + } catch (x) {} + } else if (CardVerify.stripeHandler) { + this.showStripeDialog(); + } else { + this.hasPendingClick = true; + } + }; + render() { return (