2021-02-08 15:28:47 +01:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { useStripe } from '@stripe/react-stripe-js';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
|
|
|
interface StripeConfirmProps {
|
|
|
|
clientSecret: string,
|
|
|
|
onResponse: () => void,
|
|
|
|
}
|
|
|
|
|
2021-04-07 16:21:12 +02:00
|
|
|
/**
|
|
|
|
* This component runs a 3D secure confirmation for the given Stripe payment (identified by clientSecret).
|
|
|
|
* A message is shown, depending on the result of the confirmation.
|
|
|
|
* In case of success, a callback "onResponse" is also run.
|
|
|
|
*/
|
2021-02-08 15:28:47 +01:00
|
|
|
export const StripeConfirm: React.FC<StripeConfirmProps> = ({ clientSecret, onResponse }) => {
|
|
|
|
const stripe = useStripe();
|
|
|
|
const { t } = useTranslation('shared');
|
|
|
|
|
2021-04-07 16:21:12 +02:00
|
|
|
// the message displayed to the user
|
2021-02-08 15:28:47 +01:00
|
|
|
const [message, setMessage] = useState<string>(t('app.shared.stripe_confirm.pending'));
|
2021-04-07 16:21:12 +02:00
|
|
|
// the style class of the message
|
2021-02-08 15:28:47 +01:00
|
|
|
const [type, setType] = useState<string>('info');
|
|
|
|
|
2021-04-07 16:21:12 +02:00
|
|
|
/**
|
|
|
|
* When the component is mounted, run the 3DS confirmation.
|
|
|
|
*/
|
2021-02-08 15:28:47 +01:00
|
|
|
useEffect(() => {
|
|
|
|
stripe.confirmCardPayment(clientSecret).then(function(result) {
|
|
|
|
onResponse();
|
|
|
|
if (result.error) {
|
|
|
|
// Display error.message in your UI.
|
|
|
|
setType('error');
|
|
|
|
setMessage(result.error.message);
|
|
|
|
} else {
|
|
|
|
// The setup has succeeded. Display a success message.
|
|
|
|
setType('success');
|
|
|
|
setMessage(t('app.shared.stripe_confirm.success'));
|
|
|
|
}
|
|
|
|
});
|
2021-04-07 16:21:12 +02:00
|
|
|
}, []);
|
|
|
|
|
2021-02-08 15:28:47 +01:00
|
|
|
return <div className="stripe-confirm">
|
|
|
|
<div className={`message--${type}`}><span className="message-text">{message}</span></div>
|
|
|
|
</div>;
|
|
|
|
}
|