wallet-core/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts

294 lines
9.5 KiB
TypeScript
Raw Normal View History

2022-07-21 15:36:15 +02:00
/*
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/* eslint-disable react-hooks/rules-of-hooks */
import { AmountJson, Amounts, ExchangeListItem, parsePaytoUri } from "@gnu-taler/taler-util";
2022-07-21 15:36:15 +02:00
import { TalerError } from "@gnu-taler/taler-wallet-core";
2022-08-10 16:50:46 +02:00
import { useState } from "preact/hooks";
import { Amount } from "../../components/Amount.js";
2022-07-21 15:36:15 +02:00
import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js";
import { useSelectedExchange } from "../../hooks/useSelectedExchange.js";
2022-07-21 15:36:15 +02:00
import { buildTermsOfServiceState } from "../../utils/index.js";
import * as wxApi from "../../wxApi.js";
2022-08-29 16:32:07 +02:00
import { PropsFromURI, PropsFromParams, State } from "./index.js";
2022-07-21 15:36:15 +02:00
type RecursiveState<S extends object> = S | (() => RecursiveState<S>)
2022-08-29 16:32:07 +02:00
export function useComponentStateFromParams(
2022-09-16 21:03:58 +02:00
{ amount, cancel, onSuccess }: PropsFromParams,
2022-08-29 16:32:07 +02:00
api: typeof wxApi,
): RecursiveState<State> {
const uriInfoHook = useAsyncAsHook(async () => {
const exchanges = await api.listExchanges();
return { amount: Amounts.parseOrThrow(amount), exchanges };
});
2022-08-29 16:32:07 +02:00
console.log("uri info", uriInfoHook)
2022-08-29 16:32:07 +02:00
if (!uriInfoHook) return { status: "loading", error: undefined };
2022-08-29 16:32:07 +02:00
if (uriInfoHook.hasError) {
2022-08-29 16:32:07 +02:00
return {
status: "loading-error",
error: uriInfoHook,
2022-08-29 16:32:07 +02:00
};
}
const chosenAmount = uriInfoHook.response.amount;
const exchangeList = uriInfoHook.response.exchanges.exchanges
2022-08-29 16:32:07 +02:00
async function doManualWithdraw(exchange: string, ageRestricted: number | undefined): Promise<{ transactionId: string, confirmTransferUrl: string | undefined }> {
const res = await api.acceptManualWithdrawal(exchange, Amounts.stringify(chosenAmount), ageRestricted);
2022-08-29 16:32:07 +02:00
return {
confirmTransferUrl: undefined,
transactionId: res.transactionId
2022-08-29 16:32:07 +02:00
};
}
return () => exchangeSelectionState(doManualWithdraw, cancel, onSuccess, undefined, chosenAmount, exchangeList, undefined, api)
2022-08-29 16:32:07 +02:00
}
export function useComponentStateFromURI(
2022-09-16 21:03:58 +02:00
{ talerWithdrawUri, cancel, onSuccess }: PropsFromURI,
2022-07-21 15:36:15 +02:00
api: typeof wxApi,
): RecursiveState<State> {
2022-07-21 15:36:15 +02:00
/**
* Ask the wallet about the withdraw URI
*/
const uriInfoHook = useAsyncAsHook(async () => {
if (!talerWithdrawUri) throw Error("ERROR_NO-URI-FOR-WITHDRAWAL");
const uriInfo = await api.getWithdrawalDetailsForUri({
talerWithdrawUri,
});
const exchanges = await api.listExchanges();
2022-09-16 19:29:35 +02:00
const { amount, defaultExchangeBaseUrl } = uriInfo;
return { talerWithdrawUri, amount: Amounts.parseOrThrow(amount), thisExchange: defaultExchangeBaseUrl, exchanges };
2022-07-21 15:36:15 +02:00
});
console.log("uri info", uriInfoHook)
2022-09-16 19:29:35 +02:00
if (!uriInfoHook) return { status: "loading", error: undefined };
2022-07-31 01:55:41 +02:00
if (uriInfoHook.hasError) {
2022-07-21 15:36:15 +02:00
return {
status: "loading-error",
2022-07-31 01:55:41 +02:00
error: uriInfoHook,
2022-07-21 15:36:15 +02:00
};
}
const uri = uriInfoHook.response.talerWithdrawUri;
const chosenAmount = uriInfoHook.response.amount;
const defaultExchange = uriInfoHook.response.thisExchange;
const exchangeList = uriInfoHook.response.exchanges.exchanges
2022-08-29 16:32:07 +02:00
async function doManagedWithdraw(exchange: string, ageRestricted: number | undefined): Promise<{ transactionId: string, confirmTransferUrl: string | undefined }> {
const res = await api.acceptWithdrawal(uri, exchange, ageRestricted,);
2022-07-21 15:36:15 +02:00
return {
confirmTransferUrl: res.confirmTransferUrl,
transactionId: res.transactionId
2022-07-21 15:36:15 +02:00
};
}
return () => exchangeSelectionState(doManagedWithdraw, cancel, onSuccess, uri, chosenAmount, exchangeList, defaultExchange, api)
2022-07-21 15:36:15 +02:00
}
2022-08-10 16:50:46 +02:00
type ManualOrManagedWithdrawFunction = (exchange: string, ageRestricted: number | undefined) => Promise<{ transactionId: string, confirmTransferUrl: string | undefined }>
2022-07-21 15:36:15 +02:00
function exchangeSelectionState(doWithdraw: ManualOrManagedWithdrawFunction, cancel: () => Promise<void>, onSuccess: (txid: string) => Promise<void>, talerWithdrawUri: string | undefined, chosenAmount: AmountJson, exchangeList: ExchangeListItem[], defaultExchange: string | undefined, api: typeof wxApi,): RecursiveState<State> {
//FIXME: use substates here
const selectedExchange = useSelectedExchange({ currency: chosenAmount.currency, defaultExchange, list: exchangeList })
if (selectedExchange.status === 'no-exchange') {
2022-07-21 15:36:15 +02:00
return {
status: "no-exchange",
error: undefined,
}
2022-07-21 15:36:15 +02:00
}
if (selectedExchange.status === 'selecting-exchange') {
return selectedExchange
2022-07-21 15:36:15 +02:00
}
console.log("exchange selected", selectedExchange.selected)
return () => {
const [ageRestricted, setAgeRestricted] = useState(0);
const currentExchange = selectedExchange.selected
/**
* For the exchange selected, bring the status of the terms of service
*/
const terms = useAsyncAsHook(async () => {
const exchangeTos = await api.getExchangeTos(currentExchange.exchangeBaseUrl, [
"text/xml",
]);
const state = buildTermsOfServiceState(exchangeTos);
return { state };
}, []);
console.log("terms", terms)
/**
* With the exchange and amount, ask the wallet the information
* about the withdrawal
*/
const amountHook = useAsyncAsHook(async () => {
const info = await api.getExchangeWithdrawalInfo({
exchangeBaseUrl: currentExchange.exchangeBaseUrl,
amount: chosenAmount,
tosAcceptedFormat: ["text/xml"],
ageRestricted,
});
const withdrawAmount = {
raw: Amounts.parseOrThrow(info.withdrawalAmountRaw),
effective: Amounts.parseOrThrow(info.withdrawalAmountEffective),
};
return {
amount: withdrawAmount,
ageRestrictionOptions: info.ageRestrictionOptions,
};
}, []);
const [reviewing, setReviewing] = useState<boolean>(false);
const [reviewed, setReviewed] = useState<boolean>(false);
const [withdrawError, setWithdrawError] = useState<TalerError | undefined>(
undefined,
);
const [doingWithdraw, setDoingWithdraw] = useState<boolean>(false);
async function doWithdrawAndCheckError(): Promise<void> {
try {
setDoingWithdraw(true);
const res = await doWithdraw(currentExchange.exchangeBaseUrl, !ageRestricted ? undefined : ageRestricted)
if (res.confirmTransferUrl) {
document.location.href = res.confirmTransferUrl;
} else {
onSuccess(res.transactionId);
}
} catch (e) {
if (e instanceof TalerError) {
setWithdrawError(e);
}
}
setDoingWithdraw(false);
}
2022-07-21 15:36:15 +02:00
if (!amountHook) {
return { status: "loading", error: undefined };
}
if (amountHook.hasError) {
return {
status: "loading-info",
error: amountHook,
};
}
if (!amountHook.response) {
return { status: "loading", error: undefined };
}
const withdrawalFee = Amounts.sub(
amountHook.response.amount.raw,
amountHook.response.amount.effective,
).amount;
const toBeReceived = amountHook.response.amount.effective;
const { state: termsState } = (!terms
? undefined
: terms.hasError
? undefined
: terms.response) || { state: undefined };
async function onAccept(accepted: boolean): Promise<void> {
if (!termsState) return;
try {
await api.setExchangeTosAccepted(
currentExchange.exchangeBaseUrl,
accepted ? termsState.version : undefined,
);
setReviewed(accepted);
} catch (e) {
if (e instanceof Error) {
//FIXME: uncomment this and display error
// setErrorAccepting(e.message);
}
2022-07-21 15:36:15 +02:00
}
}
const mustAcceptFirst =
termsState !== undefined &&
(termsState.status === "changed" || termsState.status === "new");
2022-07-21 15:36:15 +02:00
const ageRestrictionOptions =
amountHook.response.ageRestrictionOptions?.reduce(
(p, c) => ({ ...p, [c]: `under ${c}` }),
{} as Record<string, string>,
);
2022-07-21 15:36:15 +02:00
const ageRestrictionEnabled = ageRestrictionOptions !== undefined;
if (ageRestrictionEnabled) {
ageRestrictionOptions["0"] = "Not restricted";
}
2022-07-21 15:36:15 +02:00
//TODO: calculate based on exchange info
const ageRestriction = ageRestrictionEnabled
? {
2022-09-16 21:04:41 +02:00
list: ageRestrictionOptions,
value: String(ageRestricted),
onChange: async (v: string) => setAgeRestricted(parseInt(v, 10)),
}
: undefined;
return {
status: "success",
error: undefined,
doSelectExchange: selectedExchange.doSelect,
exchangeUrl: currentExchange.exchangeBaseUrl,
toBeReceived,
withdrawalFee,
chosenAmount,
talerWithdrawUri,
ageRestriction,
doWithdrawal: {
onClick:
doingWithdraw || (mustAcceptFirst && !reviewed)
? undefined
: doWithdrawAndCheckError,
error: withdrawError,
},
tosProps: !termsState
? undefined
: {
2022-09-16 21:04:41 +02:00
onAccept,
onReview: setReviewing,
reviewed: reviewed,
reviewing: reviewing,
terms: termsState,
},
mustAcceptFirst,
cancel,
};
}
2022-07-21 15:36:15 +02:00
}