1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-25 14:52:20 +01:00

162 lines
5.7 KiB
TypeScript
Raw Normal View History

2021-03-31 17:58:09 +02:00
import React, { ReactNode, useEffect, useRef, useState } from 'react';
2021-03-10 17:15:36 +01:00
import { useTranslation } from 'react-i18next';
import { HtmlTranslate } from '../../base/html-translate';
import { FabInput } from '../../base/fab-input';
import { Loader } from '../../base/loader';
import { SettingName } from '../../../models/setting';
import StripeAPI from '../../../api/external/stripe';
import SettingAPI from '../../../api/setting';
2021-03-10 17:15:36 +01:00
interface StripeKeysFormProps {
2021-05-18 16:42:30 +02:00
onValidKeys: (stripePublic: string, stripeSecret:string) => void,
onInvalidKeys: () => void,
2021-03-10 17:15:36 +01:00
}
/**
* Form to set the stripe's public and private keys
*/
2021-05-18 16:42:30 +02:00
const StripeKeysFormComponent: React.FC<StripeKeysFormProps> = ({ onValidKeys, onInvalidKeys }) => {
2021-03-10 17:15:36 +01:00
const { t } = useTranslation('admin');
// used to prevent promises from resolving if the component was unmounted
2021-04-02 16:02:50 +02:00
const mounted = useRef(false);
2021-03-31 17:58:09 +02:00
// Stripe's public key
2021-03-10 17:15:36 +01:00
const [publicKey, setPublicKey] = useState<string>('');
// Icon of the input field for the Stripe's public key. Used to display if the key is valid.
2021-03-24 17:31:50 +01:00
const [publicKeyAddOn, setPublicKeyAddOn] = useState<ReactNode>(null);
// Style class for the add-on icon, for the public key
const [publicKeyAddOnClassName, setPublicKeyAddOnClassName] = useState<'key-invalid' | 'key-valid' | ''>('');
// Stripe's secret key
2021-03-10 17:15:36 +01:00
const [secretKey, setSecretKey] = useState<string>('');
// Icon of the input field for the Stripe's secret key. Used to display if the key is valid.
2021-03-24 17:31:50 +01:00
const [secretKeyAddOn, setSecretKeyAddOn] = useState<ReactNode>(null);
// Style class for the add-on icon, for the public key
const [secretKeyAddOnClassName, setSecretKeyAddOnClassName] = useState<'key-invalid' | 'key-valid' | ''>('');
/**
* When the component loads for the first time:
* - mark it as mounted
* - initialize the keys with the values fetched from the API (if any)
*/
2021-03-10 17:15:36 +01:00
useEffect(() => {
2021-04-02 16:02:50 +02:00
mounted.current = true;
const api = new SettingAPI();
api.query([SettingName.StripePublicKey, SettingName.StripeSecretKey]).then(stripeKeys => {
setPublicKey(stripeKeys.get(SettingName.StripePublicKey));
setSecretKey(stripeKeys.get(SettingName.StripeSecretKey));
}).catch(error => console.error(error));
2021-04-02 16:02:50 +02:00
// when the component unmounts, mark it as unmounted
2021-04-02 16:02:50 +02:00
return () => {
mounted.current = false;
};
2021-03-10 17:15:36 +01:00
}, []);
/**
* When the style class for the public and private key are updated, check if they indicate valid keys.
* If both are valid, run the 'onValidKeys' callback
*/
useEffect(() => {
const validClassName = 'key-valid';
if (publicKeyAddOnClassName === validClassName && secretKeyAddOnClassName === validClassName) {
onValidKeys(publicKey, secretKey);
2021-05-18 16:42:30 +02:00
} else {
onInvalidKeys();
}
}, [publicKeyAddOnClassName, secretKeyAddOnClassName]);
2021-03-10 17:15:36 +01:00
/**
* Send a test call to the Stripe API to check if the inputted public key is valid
*/
2021-03-30 15:56:36 +02:00
const testPublicKey = (key: string) => {
if (!key.match(/^pk_/)) {
setPublicKeyAddOn(<i className="fa fa-times" />);
setPublicKeyAddOnClassName('key-invalid');
return;
}
StripeAPI.createPIIToken(key, 'test').then(() => {
2021-03-31 17:58:09 +02:00
if (!mounted.current) return;
setPublicKey(key);
setPublicKeyAddOn(<i className="fa fa-check" />);
setPublicKeyAddOnClassName('key-valid');
}, reason => {
2021-03-31 17:58:09 +02:00
if (!mounted.current) return;
if (reason.response.status === 401) {
setPublicKeyAddOn(<i className="fa fa-times" />);
setPublicKeyAddOnClassName('key-invalid');
}
});
}
2021-03-10 17:15:36 +01:00
/**
* Send a test call to the Stripe API to check if the inputted secret key is valid
*/
2021-03-30 15:56:36 +02:00
const testSecretKey = (key: string) => {
if (!key.match(/^sk_/)) {
setSecretKeyAddOn(<i className="fa fa-times" />);
setSecretKeyAddOnClassName('key-invalid');
return;
}
StripeAPI.listAllCharges(key).then(() => {
2021-03-31 17:58:09 +02:00
if (!mounted.current) return;
setSecretKey(key);
setSecretKeyAddOn(<i className="fa fa-check" />);
setSecretKeyAddOnClassName('key-valid');
}, reason => {
2021-03-31 17:58:09 +02:00
if (!mounted.current) return;
if (reason.response.status === 401) {
setSecretKeyAddOn(<i className="fa fa-times" />);
setSecretKeyAddOnClassName('key-invalid');
}
});
2021-03-24 17:31:50 +01:00
}
2021-03-10 17:15:36 +01:00
return (
2021-03-24 17:31:50 +01:00
<div className="stripe-keys-form">
2021-04-07 16:25:14 +02:00
<div className="stripe-keys-info">
<HtmlTranslate trKey="app.admin.invoices.payment.stripe_keys_info_html" />
</div>
2021-03-10 17:15:36 +01:00
<form name="stripeKeysForm">
2021-03-24 17:31:50 +01:00
<div className="stripe-public-input">
<label htmlFor="stripe_public_key">{ t('app.admin.invoices.payment.public_key') } *</label>
<FabInput id="stripe_public_key"
icon={<i className="fa fa-info" />}
2021-04-02 17:16:27 +02:00
defaultValue={publicKey}
2021-03-24 17:31:50 +01:00
onChange={testPublicKey}
addOn={publicKeyAddOn}
addOnClassName={publicKeyAddOnClassName}
debounce={200}
2021-03-24 17:31:50 +01:00
required />
2021-03-10 17:15:36 +01:00
</div>
2021-03-24 17:31:50 +01:00
<div className="stripe-secret-input">
<label htmlFor="stripe_secret_key">{ t('app.admin.invoices.payment.secret_key') } *</label>
<FabInput id="stripe_secret_key"
icon={<i className="fa fa-key" />}
2021-04-02 17:16:27 +02:00
defaultValue={secretKey}
onChange={testSecretKey}
addOn={secretKeyAddOn}
addOnClassName={secretKeyAddOnClassName}
debounce={200}
required/>
2021-03-10 17:15:36 +01:00
</div>
</form>
</div>
);
}
2021-05-18 16:42:30 +02:00
export const StripeKeysForm: React.FC<StripeKeysFormProps> = ({ onValidKeys, onInvalidKeys }) => {
2021-03-10 17:15:36 +01:00
return (
<Loader>
2021-05-18 16:42:30 +02:00
<StripeKeysFormComponent onValidKeys={onValidKeys} onInvalidKeys={onInvalidKeys} />
2021-03-10 17:15:36 +01:00
</Loader>
);
}