2020-11-24 13:26:15 +01:00
|
|
|
/**
|
2020-12-01 17:55:23 +01:00
|
|
|
* This component enables the user to input his card data or process payments.
|
|
|
|
* Supports Strong-Customer Authentication (SCA).
|
2020-11-24 13:26:15 +01:00
|
|
|
*/
|
|
|
|
|
2020-12-02 10:06:18 +01:00
|
|
|
import React, { ReactNode, useEffect, useState } from 'react';
|
2020-11-24 13:26:15 +01:00
|
|
|
import { react2angular } from 'react2angular';
|
|
|
|
import { Loader } from './loader';
|
|
|
|
import { IApplication } from '../models/application';
|
|
|
|
import { StripeElements } from './stripe-elements';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2020-11-30 16:52:55 +01:00
|
|
|
import { FabModal, ModalSize } from './fab-modal';
|
2020-12-08 17:30:33 +01:00
|
|
|
import { SetupIntent } from '@stripe/stripe-js';
|
2020-11-24 13:26:15 +01:00
|
|
|
import { WalletInfo } from './wallet-info';
|
|
|
|
import { User } from '../models/user';
|
2020-11-24 16:26:18 +01:00
|
|
|
import CustomAssetAPI from '../api/custom-asset';
|
|
|
|
import { CustomAssetName } from '../models/custom-asset';
|
|
|
|
import { PaymentSchedule } from '../models/payment-schedule';
|
|
|
|
import { IFablab } from '../models/fablab';
|
2020-11-25 17:13:45 +01:00
|
|
|
import WalletLib from '../lib/wallet';
|
|
|
|
import { StripeForm } from './stripe-form';
|
|
|
|
|
|
|
|
import stripeLogo from '../../../images/powered_by_stripe.png';
|
|
|
|
import mastercardLogo from '../../../images/mastercard.png';
|
|
|
|
import visaLogo from '../../../images/visa.png';
|
2020-12-07 13:49:11 +01:00
|
|
|
import { CartItems, PaymentConfirmation } from '../models/payment';
|
2020-12-01 17:55:23 +01:00
|
|
|
import WalletAPI from '../api/wallet';
|
|
|
|
import PriceAPI from '../api/price';
|
2020-12-09 12:51:06 +01:00
|
|
|
import { HtmlTranslate } from './html-translate';
|
2020-11-24 13:26:15 +01:00
|
|
|
|
|
|
|
declare var Application: IApplication;
|
2020-11-24 16:26:18 +01:00
|
|
|
declare var Fablab: IFablab;
|
2020-11-24 13:26:15 +01:00
|
|
|
|
|
|
|
interface StripeModalProps {
|
|
|
|
isOpen: boolean,
|
|
|
|
toggleModal: () => void,
|
2020-12-08 17:30:33 +01:00
|
|
|
afterSuccess: (result: SetupIntent|PaymentConfirmation) => void,
|
2020-12-01 17:55:23 +01:00
|
|
|
cartItems: CartItems,
|
2020-11-24 13:26:15 +01:00
|
|
|
currentUser: User,
|
2020-12-01 17:55:23 +01:00
|
|
|
schedule: PaymentSchedule,
|
2020-12-08 12:26:03 +01:00
|
|
|
customer: User
|
2020-11-24 13:26:15 +01:00
|
|
|
}
|
|
|
|
|
2020-11-24 16:26:18 +01:00
|
|
|
const cgvFile = CustomAssetAPI.get(CustomAssetName.CgvFile);
|
|
|
|
|
2020-12-08 12:26:03 +01:00
|
|
|
const StripeModal: React.FC<StripeModalProps> = ({ isOpen, toggleModal, afterSuccess, cartItems, currentUser, schedule, customer }) => {
|
2020-12-02 10:06:18 +01:00
|
|
|
// customer's wallet
|
|
|
|
const [wallet, setWallet] = useState(null);
|
|
|
|
// server-computed price with all details
|
|
|
|
const [price, setPrice] = useState(null);
|
|
|
|
// remaining price = total price - wallet amount
|
2020-11-25 17:13:45 +01:00
|
|
|
const [remainingPrice, setRemainingPrice] = useState(0);
|
2020-12-02 10:06:18 +01:00
|
|
|
// is the component ready to display?
|
|
|
|
const [ready, setReady] = useState(false);
|
|
|
|
// errors to display in the UI (stripe errors mainly)
|
|
|
|
const [errors, setErrors] = useState(null);
|
|
|
|
// are we currently processing the payment (ie. the form was submit, but the process is still running)?
|
|
|
|
const [submitState, setSubmitState] = useState(false);
|
|
|
|
// did the user accepts the terms of services (CGV)?
|
|
|
|
const [tos, setTos] = useState(false);
|
2020-11-24 13:26:15 +01:00
|
|
|
|
|
|
|
const { t } = useTranslation('shared');
|
2020-11-24 16:26:18 +01:00
|
|
|
const cgv = cgvFile.read();
|
|
|
|
|
2020-11-24 13:26:15 +01:00
|
|
|
|
2020-12-01 17:55:23 +01:00
|
|
|
/**
|
2020-12-02 10:06:18 +01:00
|
|
|
* On each display:
|
|
|
|
* - Refresh the wallet
|
|
|
|
* - Refresh the price
|
|
|
|
* - Refresh the remaining price
|
2020-12-01 17:55:23 +01:00
|
|
|
*/
|
|
|
|
useEffect(() => {
|
2020-12-02 10:06:18 +01:00
|
|
|
if (!cartItems) return;
|
|
|
|
WalletAPI.getByUser(cartItems.reservation?.user_id || cartItems.subscription?.user_id).then((wallet) => {
|
|
|
|
setWallet(wallet);
|
|
|
|
PriceAPI.compute(cartItems).then((res) => {
|
|
|
|
setPrice(res);
|
|
|
|
const wLib = new WalletLib(wallet);
|
|
|
|
setRemainingPrice(wLib.computeRemainingPrice(res.price));
|
|
|
|
setReady(true);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}, [cartItems]);
|
2020-12-01 17:55:23 +01:00
|
|
|
|
2020-11-24 13:26:15 +01:00
|
|
|
/**
|
|
|
|
* Check if there is currently an error to display
|
|
|
|
*/
|
|
|
|
const hasErrors = (): boolean => {
|
|
|
|
return errors !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-01 17:55:23 +01:00
|
|
|
* Check if the user accepts the Terms of Sales document
|
2020-11-24 16:26:18 +01:00
|
|
|
*/
|
|
|
|
const hasCgv = (): boolean => {
|
|
|
|
return cgv != null;
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:55:23 +01:00
|
|
|
/**
|
|
|
|
* Triggered when the user accepts or declines the Terms of Sales
|
|
|
|
*/
|
2020-12-02 10:06:18 +01:00
|
|
|
const toggleTos = (): void => {
|
2020-11-24 16:26:18 +01:00
|
|
|
setTos(!tos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we are currently creating a payment schedule
|
|
|
|
*/
|
|
|
|
const isPaymentSchedule = (): boolean => {
|
|
|
|
return schedule !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the formatted localized amount for the given price (eg. 20.5 => "20,50 €")
|
|
|
|
*/
|
|
|
|
const formatPrice = (amount: number): string => {
|
|
|
|
return new Intl.NumberFormat(Fablab.intl_locale, {style: 'currency', currency: Fablab.intl_currency}).format(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-30 16:52:55 +01:00
|
|
|
* Return the logos, shown in the modal footer.
|
2020-11-24 13:26:15 +01:00
|
|
|
*/
|
2020-11-30 16:52:55 +01:00
|
|
|
const logoFooter = (): ReactNode => {
|
2020-11-24 13:26:15 +01:00
|
|
|
return (
|
2020-11-30 16:52:55 +01:00
|
|
|
<div className="stripe-modal-icons">
|
|
|
|
<i className="fa fa-lock fa-2x m-r-sm pos-rlt" />
|
|
|
|
<img src={stripeLogo} alt="powered by stripe" />
|
|
|
|
<img src={mastercardLogo} alt="mastercard" />
|
|
|
|
<img src={visaLogo} alt="visa" />
|
|
|
|
</div>
|
2020-11-24 13:26:15 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-02 10:06:18 +01:00
|
|
|
/**
|
|
|
|
* Set the component as 'currently submitting'
|
|
|
|
*/
|
2020-11-25 17:13:45 +01:00
|
|
|
const handleSubmit = (): void => {
|
|
|
|
setSubmitState(true);
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:55:23 +01:00
|
|
|
/**
|
|
|
|
* After sending the form with success, process the resulting payment method
|
|
|
|
*/
|
2020-12-08 17:30:33 +01:00
|
|
|
const handleFormSuccess = async (result: SetupIntent|PaymentConfirmation|any): Promise<void> => {
|
2020-11-25 17:13:45 +01:00
|
|
|
setSubmitState(false);
|
2020-12-07 13:49:11 +01:00
|
|
|
afterSuccess(result);
|
2020-11-25 17:13:45 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 17:55:23 +01:00
|
|
|
/**
|
|
|
|
* When stripe-form raise an error, it is handled by this callback which display it in the modal.
|
|
|
|
*/
|
2020-11-25 17:13:45 +01:00
|
|
|
const handleFormError = (message: string): void => {
|
|
|
|
setSubmitState(false);
|
|
|
|
setErrors(message);
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:55:23 +01:00
|
|
|
/**
|
|
|
|
* Check the form can be submitted.
|
|
|
|
* => We're not currently already submitting the form, and, if the terms of service are enabled, the user agrees with them.
|
|
|
|
*/
|
|
|
|
const canSubmit = (): boolean => {
|
|
|
|
let terms = true;
|
|
|
|
if (hasCgv()) { terms = tos; }
|
|
|
|
return !submitState && terms;
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:26:18 +01:00
|
|
|
|
2020-11-24 13:26:15 +01:00
|
|
|
return (
|
2020-11-30 16:52:55 +01:00
|
|
|
<FabModal title={t('app.shared.stripe.online_payment')}
|
|
|
|
isOpen={isOpen}
|
|
|
|
toggleModal={toggleModal}
|
|
|
|
width={ModalSize.medium}
|
|
|
|
closeButton={false}
|
|
|
|
customFooter={logoFooter()}
|
|
|
|
className="stripe-modal">
|
2020-12-02 10:06:18 +01:00
|
|
|
{ready && <StripeElements>
|
2020-12-29 17:31:57 +01:00
|
|
|
<WalletInfo cartItems={cartItems} currentUser={currentUser} wallet={wallet} price={price?.price} />
|
2020-12-01 17:55:23 +01:00
|
|
|
<StripeForm onSubmit={handleSubmit}
|
|
|
|
onSuccess={handleFormSuccess}
|
|
|
|
onError={handleFormError}
|
2020-12-23 15:29:56 +01:00
|
|
|
operator={currentUser}
|
2020-12-01 17:55:23 +01:00
|
|
|
className="stripe-form"
|
|
|
|
cartItems={cartItems}
|
2020-12-08 12:26:03 +01:00
|
|
|
customer={customer}
|
2020-12-08 17:30:33 +01:00
|
|
|
paymentSchedule={isPaymentSchedule()}>
|
2020-11-30 16:52:55 +01:00
|
|
|
{hasErrors() && <div className="stripe-errors">
|
|
|
|
{errors}
|
|
|
|
</div>}
|
2020-12-09 16:47:51 +01:00
|
|
|
{isPaymentSchedule() && <div className="payment-schedule-info">
|
|
|
|
<HtmlTranslate trKey="app.shared.stripe.payment_schedule_html" options={{ DEADLINES: schedule.items.length }} />
|
|
|
|
</div>}
|
2020-11-30 16:52:55 +01:00
|
|
|
{hasCgv() && <div className="terms-of-sales">
|
|
|
|
<input type="checkbox" id="acceptToS" name="acceptCondition" checked={tos} onChange={toggleTos} required />
|
|
|
|
<label htmlFor="acceptToS">{ t('app.shared.stripe.i_have_read_and_accept_') }
|
|
|
|
<a href={cgv.custom_asset_file_attributes.attachment_url} target="_blank">
|
|
|
|
{ t('app.shared.stripe._the_general_terms_and_conditions') }
|
|
|
|
</a>
|
|
|
|
</label>
|
|
|
|
</div>}
|
|
|
|
</StripeForm>
|
2020-12-07 12:58:11 +01:00
|
|
|
{!submitState && <button type="submit"
|
|
|
|
disabled={!canSubmit()}
|
|
|
|
form="stripe-form"
|
|
|
|
className="validate-btn">
|
2020-11-30 16:52:55 +01:00
|
|
|
{t('app.shared.stripe.confirm_payment_of_', { AMOUNT: formatPrice(remainingPrice) })}
|
2020-12-07 12:58:11 +01:00
|
|
|
</button>}
|
|
|
|
{submitState && <div className="payment-pending">
|
|
|
|
<div className="fa-2x">
|
|
|
|
<i className="fas fa-circle-notch fa-spin" />
|
|
|
|
</div>
|
|
|
|
</div>}
|
2020-12-02 10:06:18 +01:00
|
|
|
</StripeElements>}
|
2020-11-30 16:52:55 +01:00
|
|
|
</FabModal>
|
2020-11-24 13:26:15 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-08 12:26:03 +01:00
|
|
|
const StripeModalWrapper: React.FC<StripeModalProps> = ({ isOpen, toggleModal, afterSuccess, currentUser, schedule , cartItems, customer }) => {
|
2020-11-24 13:26:15 +01:00
|
|
|
return (
|
|
|
|
<Loader>
|
2020-12-08 12:26:03 +01:00
|
|
|
<StripeModal isOpen={isOpen} toggleModal={toggleModal} afterSuccess={afterSuccess} currentUser={currentUser} schedule={schedule} cartItems={cartItems} customer={customer} />
|
2020-11-24 13:26:15 +01:00
|
|
|
</Loader>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-08 12:26:03 +01:00
|
|
|
Application.Components.component('stripeModal', react2angular(StripeModalWrapper, ['isOpen', 'toggleModal', 'afterSuccess','currentUser', 'schedule', 'cartItems', 'customer']));
|