import { useLocaleContext } from '@arcblock/ux/lib/Locale/context'; import Toast from '@arcblock/ux/lib/Toast'; import type { TPricingTableExpanded } from '@blocklet/payment-types'; import { Alert, CircularProgress, Stack, Typography } from '@mui/material'; import { Box } from '@mui/system'; import { useRequest } from 'ahooks'; import { useState } from 'react'; import Livemode from '../components/livemode'; import PricingTable from '../components/pricing-table'; import api from '../libs/api'; import { mergeExtraParams } from '../libs/util'; import type { CheckoutProps } from '../types'; import CheckoutForm from './form'; import { PaymentThemeProvider } from '../theme'; import TruncatedText from '../components/truncated-text'; const fetchData = async (id: string): Promise => { const { data } = await api.get(`/api/pricing-tables/${id}`); return data; }; const ensureProtocol = (url: string): string => { if (!/^https?:\/\//i.test(url)) { return `https://${url}`; } return url; }; function CheckoutTableInner({ id, mode, onPaid, onError, onChange, extraParams, goBack, theme }: CheckoutProps) { if (!id.startsWith('prctbl_')) { throw new Error('A valid pricing table id is required.'); } const [sessionId, setSessionId] = useState(''); const hashSessionId = window.location.hash.slice(1); const { t } = useLocaleContext(); const { error, loading, data } = useRequest(() => fetchData(id)); if (error) { return {error.message}; } if (loading || !data) { return ; } if (data.items.length === 0) { return {t('payment.checkout.noPricing')}; } const handleSelect = (priceId: string, currencyId: string) => { api .post( `/api/pricing-tables/${data.id}/checkout/${priceId}?${mergeExtraParams({ ...extraParams, currencyId, })}` ) .then((res: any) => { if (mode === 'standalone') { let { url } = res.data; url = url.indexOf('?') > -1 ? `${url}¤cyId=${currencyId}` : `${url}?currencyId=${currencyId}`; window.location.replace(ensureProtocol(url)); } else { window.location.hash = res.data.id; setSessionId(res.data.id); } }) .catch((err: any) => { console.error(err); Toast.error(err.message); }); }; const prop: any = {}; if (goBack) { prop.goBack = () => { window.location.hash = ''; if (typeof goBack !== 'undefined') { goBack(); } setSessionId(''); }; } if (!sessionId && !hashSessionId) { if (mode === 'standalone') { return ( {!data.livemode && } ); } return ; } return ( ); } export default function CheckoutTable(props: CheckoutProps) { if (props.theme === 'inherit') { return ; } if (props.theme && typeof props.theme === 'object') { return ( ); } return ( ); }