lbry-desktop/ui/component/cardVerify/view.jsx

186 lines
4.5 KiB
React
Raw Normal View History

2019-09-26 18:07:11 +02:00
/* eslint-disable no-undef */
/* eslint-disable react/prop-types */
import React from 'react';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
2017-07-19 01:00:13 +02:00
let scriptLoading = false;
let scriptLoaded = false;
let scriptDidError = false;
2019-09-26 18:07:11 +02:00
// Flow does not like the way this stripe plugin works
// Disabled because it was a huge pain
// type Props = {
// disabled: boolean,
// label: ?string,
// email: string,
// // =====================================================
// // Required by stripe
// // see Stripe docs for more info:
// // https://stripe.com/docs/checkout#integration-custom
// // =====================================================
// // Your publishable key (test or live).
// // can't use "key" as a prop in react, so have to change the keyname
// stripeKey: string,
// // The callback to invoke when the Checkout process is complete.
// // function(token)
// // token is the token object created.
// // token.id can be used to create a charge or customer.
// // token.email contains the email address entered by the user.
// token: string,
// };
class CardVerify extends React.Component {
constructor(props) {
2017-07-19 01:00:13 +02:00
super(props);
this.state = {
open: false,
2020-02-05 05:47:23 +01:00
scriptFailedToLoad: false,
2017-07-19 01:00:13 +02:00
};
}
componentDidMount() {
if (scriptLoaded) {
return;
}
if (scriptLoading) {
return;
}
scriptLoading = true;
const script = document.createElement('script');
script.src = 'https://checkout.stripe.com/checkout.js';
script.async = true;
2017-07-19 01:00:13 +02:00
this.loadPromise = (() => {
let canceled = false;
const promise = new Promise((resolve, reject) => {
script.onload = () => {
scriptLoaded = true;
scriptLoading = false;
resolve();
this.onScriptLoaded();
};
script.onerror = event => {
scriptDidError = true;
scriptLoading = false;
reject(event);
this.onScriptError(event);
2017-07-19 01:00:13 +02:00
};
});
const wrappedPromise = new Promise((resolve, reject) => {
promise.then(() => (canceled ? reject({ isCanceled: true }) : resolve()));
promise.catch(error => (canceled ? reject({ isCanceled: true }) : reject(error)));
2017-07-19 01:00:13 +02:00
});
return {
promise: wrappedPromise,
reject() {
2017-07-19 01:00:13 +02:00
canceled = true;
},
};
})();
this.loadPromise.promise.then(this.onScriptLoaded).catch(this.onScriptError);
2017-07-19 01:00:13 +02:00
2019-09-26 18:07:11 +02:00
// $FlowFixMe
document.body.appendChild(script);
2017-07-19 01:00:13 +02:00
}
componentDidUpdate() {
if (!scriptLoading) {
this.updateStripeHandler();
}
}
componentWillUnmount() {
if (this.loadPromise) {
this.loadPromise.reject();
2017-07-19 01:00:13 +02:00
}
if (CardVerify.stripeHandler && this.state.open) {
CardVerify.stripeHandler.close();
}
}
onScriptLoaded = () => {
if (!CardVerify.stripeHandler) {
CardVerify.stripeHandler = StripeCheckout.configure({
key: this.props.stripeKey,
});
updated code about to test something generate programatically beginning of the frontend stripe integration page seems to be working add user put functionality behind conditional tag connect frontend working well adding environment variables to save success and failure url bugfix bugfix final clean up adding credit card page seems to be coming along calls successfully coming from the frontend fixing up frontend cleaning up frontend coming along client secret working basic frontend in place adding tip page adding more to the tip frontend frontend almost done tabs coming along one last thing to do for frontend adding explainer text as custom function putting finishing touches on tabs support tabs working well disable fiat toggle when card not connected fix frontend gui bug bugfix and pull out label function fix symbol for tip gui modal when card is not yet saved fix fiat disabled bug knowing whether card is added programatically sending tip with frontend tip functionality working show unpaid balance add frontend for card add section update frontend update frontend bugfix change to use react instead of css update how stripe is instantiated fix bug use customer setup coming along working but needs optimization persist if card is saved adding anonymous tip functionality fix nan bug build stripe endpoints programatically show for all users for time being allow the stripe key to automatically switch to live environment bugfix bugfix fix jslint fix channel page support button better docs show customer transactions on frontend basic table in place various page updates per jeremys notes showing card details nicer tip history table add better prompt to add card on file viewer page some linting time put connect account behind fiat enabled no persist fiat mode wallet calls tip stuff
2021-07-03 19:19:23 +02:00
2017-07-19 01:00:13 +02:00
if (this.hasPendingClick) {
this.showStripeDialog();
}
}
};
onScriptError = (...args) => {
2020-02-05 05:47:23 +01:00
this.setState({ scriptFailedToLoad: true });
2017-07-19 01:00:13 +02:00
};
onClosed = () => {
this.setState({ open: false });
};
updateStripeHandler() {
if (!CardVerify.stripeHandler) {
CardVerify.stripeHandler = StripeCheckout.configure({
key: this.props.stripeKey,
});
}
}
showStripeDialog() {
this.setState({ open: true });
2017-07-25 20:59:47 +02:00
CardVerify.stripeHandler.open({
allowRememberMe: false,
closed: this.onClosed,
description: __('Confirm Identity'),
2017-07-25 20:59:47 +02:00
email: this.props.email,
locale: 'auto',
panelLabel: 'Verify',
2017-07-25 20:59:47 +02:00
token: this.props.token,
2017-07-27 20:42:43 +02:00
zipCode: true,
2017-07-25 20:59:47 +02:00
});
2017-07-19 01:00:13 +02:00
}
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;
}
};
2017-07-19 01:00:13 +02:00
render() {
2020-02-05 05:47:23 +01:00
const { scriptFailedToLoad } = this.props;
2017-07-19 01:00:13 +02:00
return (
2020-02-05 05:47:23 +01:00
<div>
{scriptFailedToLoad && (
<div className="error__text">There was an error connecting to Stripe. Please try again later.</div>
2020-02-05 05:47:23 +01:00
)}
<Button
button="primary"
label={this.props.label}
disabled={this.props.disabled || this.state.open || this.hasPendingClick}
onClick={this.onClick.bind(this)}
/>
</div>
2017-07-19 01:00:13 +02:00
);
}
}
export default CardVerify;
2019-09-26 18:07:11 +02:00
/* eslint-enable no-undef */
/* eslint-enable react/prop-types */