Revert "Improve type checking for card verify" (#2074)
This commit is contained in:
parent
736ab5581b
commit
06c16cdf27
1 changed files with 21 additions and 50 deletions
|
@ -7,20 +7,6 @@ let scriptLoading = false;
|
||||||
let scriptLoaded = false;
|
let scriptLoaded = false;
|
||||||
let scriptDidError = false;
|
let scriptDidError = false;
|
||||||
|
|
||||||
declare class CardVerify {
|
|
||||||
static stripeHandler: {
|
|
||||||
open: Function,
|
|
||||||
close: Function,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
declare class StripeCheckout {
|
|
||||||
static configure({}): {
|
|
||||||
open: Function,
|
|
||||||
close: Function,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
disabled: boolean,
|
disabled: boolean,
|
||||||
label: ?string,
|
label: ?string,
|
||||||
|
@ -41,20 +27,14 @@ type Props = {
|
||||||
// token.id can be used to create a charge or customer.
|
// token.id can be used to create a charge or customer.
|
||||||
// token.email contains the email address entered by the user.
|
// token.email contains the email address entered by the user.
|
||||||
token: string,
|
token: string,
|
||||||
email: string,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
type State = {
|
class CardVerify extends React.Component {
|
||||||
open: boolean,
|
constructor(props) {
|
||||||
};
|
|
||||||
|
|
||||||
class CardVerifyComponent extends React.Component<Props, State> {
|
|
||||||
constructor(props: Props) {
|
|
||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
open: false,
|
open: false,
|
||||||
};
|
};
|
||||||
this.onClick = this.onClick.bind(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
|
@ -85,14 +65,12 @@ class CardVerifyComponent extends React.Component<Props, State> {
|
||||||
scriptDidError = true;
|
scriptDidError = true;
|
||||||
scriptLoading = false;
|
scriptLoading = false;
|
||||||
reject(event);
|
reject(event);
|
||||||
this.onScriptError();
|
this.onScriptError(event);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
const wrappedPromise = new Promise((accept, cancel) => {
|
const wrappedPromise = new Promise((accept, cancel) => {
|
||||||
promise.then(() => (canceled ? cancel(new Error({ isCanceled: true })) : accept()));
|
promise.then(() => (canceled ? cancel({ isCanceled: true }) : accept()));
|
||||||
promise.catch(
|
promise.catch(error => (canceled ? cancel({ isCanceled: true }) : cancel(error)));
|
||||||
error => (canceled ? cancel(new Error({ isCanceled: true })) : cancel(error))
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -105,10 +83,8 @@ class CardVerifyComponent extends React.Component<Props, State> {
|
||||||
|
|
||||||
this.loadPromise.promise.then(this.onScriptLoaded).catch(this.onScriptError);
|
this.loadPromise.promise.then(this.onScriptLoaded).catch(this.onScriptError);
|
||||||
|
|
||||||
if (document.body) {
|
|
||||||
document.body.appendChild(script);
|
document.body.appendChild(script);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
componentDidUpdate() {
|
componentDidUpdate() {
|
||||||
if (!scriptLoading) {
|
if (!scriptLoading) {
|
||||||
|
@ -136,7 +112,7 @@ class CardVerifyComponent extends React.Component<Props, State> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
onScriptError = () => {
|
onScriptError = (...args) => {
|
||||||
throw new Error('Unable to load credit validation script.');
|
throw new Error('Unable to load credit validation script.');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -144,23 +120,6 @@ class CardVerifyComponent extends React.Component<Props, State> {
|
||||||
this.setState({ open: false });
|
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<any>,
|
|
||||||
cancel: Function,
|
|
||||||
};
|
|
||||||
|
|
||||||
hasPendingClick: boolean;
|
|
||||||
|
|
||||||
updateStripeHandler() {
|
updateStripeHandler() {
|
||||||
if (!CardVerify.stripeHandler) {
|
if (!CardVerify.stripeHandler) {
|
||||||
CardVerify.stripeHandler = StripeCheckout.configure({
|
CardVerify.stripeHandler = StripeCheckout.configure({
|
||||||
|
@ -183,6 +142,18 @@ class CardVerifyComponent extends React.Component<Props, State> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
|
@ -190,10 +161,10 @@ class CardVerifyComponent extends React.Component<Props, State> {
|
||||||
label={this.props.label}
|
label={this.props.label}
|
||||||
icon={icons.LOCK}
|
icon={icons.LOCK}
|
||||||
disabled={this.props.disabled || this.state.open || this.hasPendingClick}
|
disabled={this.props.disabled || this.state.open || this.hasPendingClick}
|
||||||
onClick={this.onClick}
|
onClick={this.onClick.bind(this)}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default CardVerifyComponent;
|
export default CardVerify;
|
||||||
|
|
Loading…
Reference in a new issue