2021-03-03 15:28:56 +01:00
|
|
|
/**
|
2021-03-10 17:15:36 +01:00
|
|
|
* This component allows an administrator to select and configure a payment gateway.
|
|
|
|
* The configuration of a payment gateway is required to enable the online payments.
|
2021-03-03 15:28:56 +01:00
|
|
|
*/
|
|
|
|
|
2021-03-30 15:56:36 +02:00
|
|
|
import React, { BaseSyntheticEvent, useEffect, useState } from 'react';
|
2021-03-03 15:28:56 +01:00
|
|
|
import { react2angular } from 'react2angular';
|
|
|
|
import { Loader } from './loader';
|
|
|
|
import { IApplication } from '../models/application';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { FabModal, ModalSize } from './fab-modal';
|
|
|
|
import { User } from '../models/user';
|
2021-03-24 17:31:50 +01:00
|
|
|
import { Gateway } from '../models/gateway';
|
|
|
|
import { StripeKeysForm } from './stripe-keys-form';
|
2021-03-30 15:56:36 +02:00
|
|
|
import { SettingBulkResult, SettingName } from '../models/setting';
|
|
|
|
import SettingAPI from '../api/setting';
|
2021-03-31 16:03:51 +02:00
|
|
|
import { PayZenKeysForm } from './payzen-keys-form';
|
2021-03-03 15:28:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
declare var Application: IApplication;
|
|
|
|
|
|
|
|
interface SelectGatewayModalModalProps {
|
|
|
|
isOpen: boolean,
|
|
|
|
toggleModal: () => void,
|
|
|
|
currentUser: User,
|
2021-03-30 15:56:36 +02:00
|
|
|
onError: (errors: Map<SettingName, SettingBulkResult>|any) => void,
|
|
|
|
onSuccess: (results: Map<SettingName, SettingBulkResult>) => void,
|
2021-03-03 15:28:56 +01:00
|
|
|
}
|
|
|
|
|
2021-04-07 16:21:12 +02:00
|
|
|
// initial request to the API
|
2021-03-30 15:56:36 +02:00
|
|
|
const paymentGateway = SettingAPI.get(SettingName.PaymentGateway);
|
|
|
|
|
|
|
|
const SelectGatewayModal: React.FC<SelectGatewayModalModalProps> = ({ isOpen, toggleModal, onError, onSuccess }) => {
|
2021-03-03 15:28:56 +01:00
|
|
|
const { t } = useTranslation('admin');
|
|
|
|
|
2021-03-10 16:46:18 +01:00
|
|
|
const [preventConfirmGateway, setPreventConfirmGateway] = useState<boolean>(true);
|
|
|
|
const [selectedGateway, setSelectedGateway] = useState<string>('');
|
2021-03-30 11:26:47 +02:00
|
|
|
const [gatewayConfig, setGatewayConfig] = useState<Map<SettingName, string>>(new Map());
|
2021-03-10 16:46:18 +01:00
|
|
|
|
2021-03-30 15:56:36 +02:00
|
|
|
useEffect(() => {
|
|
|
|
const gateway = paymentGateway.read();
|
|
|
|
setSelectedGateway(gateway.value);
|
|
|
|
}, []);
|
2021-03-10 16:46:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback triggered when the user has filled and confirmed the settings of his gateway
|
|
|
|
*/
|
|
|
|
const onGatewayConfirmed = () => {
|
|
|
|
setPreventConfirmGateway(true);
|
2021-03-30 15:56:36 +02:00
|
|
|
updateSettings();
|
|
|
|
setPreventConfirmGateway(false);
|
2021-03-10 16:46:18 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:31:50 +01:00
|
|
|
/**
|
|
|
|
* Save the gateway provided by the target input into the component state
|
|
|
|
*/
|
2021-03-10 16:46:18 +01:00
|
|
|
const setGateway = (event: BaseSyntheticEvent) => {
|
|
|
|
const gateway = event.target.value;
|
|
|
|
setSelectedGateway(gateway);
|
|
|
|
}
|
2021-03-03 15:28:56 +01:00
|
|
|
|
2021-03-24 17:31:50 +01:00
|
|
|
/**
|
|
|
|
* Check if any payment gateway was selected
|
|
|
|
*/
|
|
|
|
const hasSelectedGateway = (): boolean => {
|
|
|
|
return selectedGateway !== '';
|
|
|
|
}
|
|
|
|
|
2021-03-30 11:26:47 +02:00
|
|
|
/**
|
|
|
|
* Callback triggered when the embedded form has validated all the stripe keys
|
|
|
|
*/
|
|
|
|
const handleValidStripeKeys = (publicKey: string, secretKey: string): void => {
|
|
|
|
setGatewayConfig((prev) => {
|
|
|
|
const newMap = new Map(prev);
|
|
|
|
newMap.set(SettingName.StripeSecretKey, secretKey);
|
|
|
|
newMap.set(SettingName.StripePublicKey, publicKey);
|
|
|
|
return newMap;
|
|
|
|
});
|
|
|
|
setPreventConfirmGateway(false);
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:58:09 +02:00
|
|
|
/**
|
|
|
|
* Callback triggered when the embedded for has validated all the PayZen keys
|
|
|
|
*/
|
|
|
|
const handleValidPayZenKeys = (payZenKeys: Map<SettingName, string>): void => {
|
|
|
|
setGatewayConfig(payZenKeys);
|
|
|
|
setPreventConfirmGateway(false);
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:56:36 +02:00
|
|
|
/**
|
|
|
|
* Send the new gateway settings to the API to save them
|
|
|
|
*/
|
|
|
|
const updateSettings = (): void => {
|
|
|
|
const settings = new Map<SettingName, string>(gatewayConfig);
|
|
|
|
settings.set(SettingName.PaymentGateway, selectedGateway);
|
|
|
|
|
|
|
|
const api = new SettingAPI();
|
|
|
|
api.bulkUpdate(settings).then(result => {
|
|
|
|
if (Array.from(result.values()).filter(item => !item.status).length > 0) {
|
|
|
|
onError(result);
|
|
|
|
} else {
|
|
|
|
onSuccess(result);
|
|
|
|
}
|
|
|
|
}, reason => {
|
|
|
|
onError(reason);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-03 15:28:56 +01:00
|
|
|
return (
|
2021-03-10 16:46:18 +01:00
|
|
|
<FabModal title={t('app.admin.invoices.payment.gateway_modal.select_gateway_title')}
|
2021-03-03 15:28:56 +01:00
|
|
|
isOpen={isOpen}
|
|
|
|
toggleModal={toggleModal}
|
|
|
|
width={ModalSize.medium}
|
2021-03-10 16:46:18 +01:00
|
|
|
className="gateway-modal"
|
|
|
|
confirmButton={t('app.admin.invoices.payment.gateway_modal.confirm_button')}
|
|
|
|
onConfirm={onGatewayConfirmed}
|
|
|
|
preventConfirm={preventConfirmGateway}>
|
2021-03-24 17:31:50 +01:00
|
|
|
{!hasSelectedGateway() && <p className="info-gateway">
|
2021-03-10 16:46:18 +01:00
|
|
|
{t('app.admin.invoices.payment.gateway_modal.gateway_info')}
|
2021-03-24 17:31:50 +01:00
|
|
|
</p>}
|
2021-03-10 16:46:18 +01:00
|
|
|
<label htmlFor="gateway">{t('app.admin.invoices.payment.gateway_modal.select_gateway')}</label>
|
|
|
|
<select id="gateway" className="select-gateway" onChange={setGateway} value={selectedGateway}>
|
|
|
|
<option />
|
2021-03-24 17:31:50 +01:00
|
|
|
<option value={Gateway.Stripe}>{t('app.admin.invoices.payment.gateway_modal.stripe')}</option>
|
|
|
|
<option value={Gateway.PayZen}>{t('app.admin.invoices.payment.gateway_modal.payzen')}</option>
|
2021-03-10 16:46:18 +01:00
|
|
|
</select>
|
2021-03-30 11:26:47 +02:00
|
|
|
{selectedGateway === Gateway.Stripe && <StripeKeysForm onValidKeys={handleValidStripeKeys} />}
|
2021-03-31 17:58:09 +02:00
|
|
|
{selectedGateway === Gateway.PayZen && <PayZenKeysForm onValidKeys={handleValidPayZenKeys} />}
|
2021-03-03 15:28:56 +01:00
|
|
|
</FabModal>
|
|
|
|
);
|
2021-03-24 17:31:50 +01:00
|
|
|
};
|
2021-03-03 15:28:56 +01:00
|
|
|
|
2021-03-30 15:56:36 +02:00
|
|
|
const SelectGatewayModalWrapper: React.FC<SelectGatewayModalModalProps> = ({ isOpen, toggleModal, currentUser, onSuccess, onError }) => {
|
2021-03-03 15:28:56 +01:00
|
|
|
return (
|
|
|
|
<Loader>
|
2021-03-30 15:56:36 +02:00
|
|
|
<SelectGatewayModal isOpen={isOpen} toggleModal={toggleModal} currentUser={currentUser} onSuccess={onSuccess} onError={onError} />
|
2021-03-03 15:28:56 +01:00
|
|
|
</Loader>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:56:36 +02:00
|
|
|
Application.Components.component('selectGatewayModal', react2angular(SelectGatewayModalWrapper, ['isOpen', 'toggleModal', 'currentUser', 'onSuccess', 'onError']));
|