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/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2022-08-29 16:32:07 +02:00
|
|
|
import { Amounts, 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";
|
2022-07-21 15:36:15 +02:00
|
|
|
import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js";
|
|
|
|
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
|
|
|
|
2022-08-29 16:32:07 +02:00
|
|
|
export function useComponentStateFromParams(
|
|
|
|
{ amount, cancel }: PropsFromParams,
|
|
|
|
api: typeof wxApi,
|
|
|
|
): State {
|
|
|
|
|
|
|
|
const [ageRestricted, setAgeRestricted] = useState(0);
|
|
|
|
|
|
|
|
const exchangeHook = useAsyncAsHook(api.listExchanges);
|
|
|
|
|
|
|
|
const exchangeHookDep =
|
|
|
|
!exchangeHook || exchangeHook.hasError || !exchangeHook.response
|
|
|
|
? undefined
|
|
|
|
: exchangeHook.response;
|
|
|
|
|
|
|
|
const chosenAmount = Amounts.parseOrThrow(amount);
|
|
|
|
|
|
|
|
// get the first exchange with the currency as the default one
|
|
|
|
const exchange = exchangeHookDep ? exchangeHookDep.exchanges.find(e => e.currency === chosenAmount.currency) : undefined
|
|
|
|
/**
|
|
|
|
* For the exchange selected, bring the status of the terms of service
|
|
|
|
*/
|
|
|
|
const terms = useAsyncAsHook(async () => {
|
|
|
|
if (!exchange) return undefined
|
|
|
|
|
|
|
|
const exchangeTos = await api.getExchangeTos(exchange.exchangeBaseUrl, ["text/xml"]);
|
|
|
|
|
|
|
|
const state = buildTermsOfServiceState(exchangeTos);
|
|
|
|
|
|
|
|
return { state };
|
|
|
|
}, [exchangeHookDep]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* With the exchange and amount, ask the wallet the information
|
|
|
|
* about the withdrawal
|
|
|
|
*/
|
|
|
|
const amountHook = useAsyncAsHook(async () => {
|
|
|
|
if (!exchange) return undefined
|
|
|
|
|
|
|
|
const info = await api.getExchangeWithdrawalInfo({
|
|
|
|
exchangeBaseUrl: exchange.exchangeBaseUrl,
|
|
|
|
amount: chosenAmount,
|
|
|
|
tosAcceptedFormat: ["text/xml"],
|
|
|
|
});
|
|
|
|
|
|
|
|
const withdrawAmount = {
|
|
|
|
raw: Amounts.parseOrThrow(info.withdrawalAmountRaw),
|
|
|
|
effective: Amounts.parseOrThrow(info.withdrawalAmountEffective),
|
|
|
|
}
|
|
|
|
|
|
|
|
return { amount: withdrawAmount };
|
|
|
|
}, [exchangeHookDep]);
|
|
|
|
|
|
|
|
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);
|
|
|
|
const [withdrawCompleted, setWithdrawCompleted] = useState<boolean>(false);
|
|
|
|
|
|
|
|
if (!exchangeHook) return { status: "loading", error: undefined }
|
|
|
|
if (exchangeHook.hasError) {
|
|
|
|
return {
|
|
|
|
status: "loading-uri",
|
|
|
|
error: exchangeHook,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exchange) {
|
|
|
|
return {
|
|
|
|
status: "loading-exchange",
|
|
|
|
error: {
|
|
|
|
hasError: true,
|
|
|
|
operational: false,
|
|
|
|
message: "ERROR_NO-DEFAULT-EXCHANGE",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doWithdrawAndCheckError(): Promise<void> {
|
|
|
|
if (!exchange) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
setDoingWithdraw(true);
|
|
|
|
|
|
|
|
const response = await wxApi.acceptManualWithdrawal(
|
|
|
|
exchange.exchangeBaseUrl,
|
|
|
|
Amounts.stringify(amount),
|
|
|
|
);
|
|
|
|
|
|
|
|
setWithdrawCompleted(true);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof TalerError) {
|
|
|
|
setWithdrawError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setDoingWithdraw(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!amountHook) {
|
|
|
|
return { status: "loading", error: undefined }
|
|
|
|
}
|
|
|
|
if (amountHook.hasError) {
|
|
|
|
return {
|
|
|
|
status: "loading-info",
|
|
|
|
error: amountHook,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (!amountHook.response) {
|
|
|
|
return { status: "loading", error: undefined };
|
|
|
|
}
|
|
|
|
if (withdrawCompleted) {
|
|
|
|
return { status: "completed", 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 || !exchange) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await api.setExchangeTosAccepted(
|
|
|
|
exchange.exchangeBaseUrl,
|
|
|
|
accepted ? termsState.version : undefined,
|
|
|
|
);
|
|
|
|
setReviewed(accepted);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Error) {
|
|
|
|
//FIXME: uncomment this and display error
|
|
|
|
// setErrorAccepting(e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mustAcceptFirst =
|
|
|
|
termsState !== undefined &&
|
|
|
|
(termsState.status === "changed" || termsState.status === "new");
|
|
|
|
|
|
|
|
const ageRestrictionOptions: Record<string, string> | undefined = "6:12:18"
|
|
|
|
.split(":")
|
|
|
|
.reduce((p, c) => ({ ...p, [c]: `under ${c}` }), {});
|
|
|
|
|
|
|
|
if (ageRestrictionOptions) {
|
|
|
|
ageRestrictionOptions["0"] = "Not restricted";
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: calculate based on exchange info
|
|
|
|
const ageRestrictionEnabled = false;
|
|
|
|
const ageRestriction = ageRestrictionEnabled ? {
|
|
|
|
list: ageRestrictionOptions,
|
|
|
|
value: String(ageRestricted),
|
|
|
|
onChange: async (v: string) => setAgeRestricted(parseInt(v, 10)),
|
|
|
|
} : undefined;
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: "success",
|
|
|
|
error: undefined,
|
|
|
|
exchangeUrl: exchange.exchangeBaseUrl,
|
|
|
|
toBeReceived,
|
|
|
|
withdrawalFee,
|
|
|
|
chosenAmount,
|
|
|
|
ageRestriction,
|
|
|
|
doWithdrawal: {
|
|
|
|
onClick:
|
|
|
|
doingWithdraw || (mustAcceptFirst && !reviewed)
|
|
|
|
? undefined
|
|
|
|
: doWithdrawAndCheckError,
|
|
|
|
error: withdrawError,
|
|
|
|
},
|
|
|
|
tosProps: !termsState
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
onAccept,
|
|
|
|
onReview: setReviewing,
|
|
|
|
reviewed: reviewed,
|
|
|
|
reviewing: reviewing,
|
|
|
|
terms: termsState,
|
|
|
|
},
|
|
|
|
mustAcceptFirst,
|
|
|
|
cancel,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useComponentStateFromURI(
|
|
|
|
{ talerWithdrawUri, cancel }: PropsFromURI,
|
2022-07-21 15:36:15 +02:00
|
|
|
api: typeof wxApi,
|
|
|
|
): State {
|
|
|
|
const [ageRestricted, setAgeRestricted] = useState(0);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
});
|
2022-08-10 16:50:46 +02:00
|
|
|
const { amount, defaultExchangeBaseUrl } = uriInfo
|
|
|
|
return { amount, thisExchange: defaultExchangeBaseUrl };
|
2022-07-21 15:36:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the amount and select one exchange
|
|
|
|
*/
|
|
|
|
const uriHookDep =
|
|
|
|
!uriInfoHook || uriInfoHook.hasError || !uriInfoHook.response
|
|
|
|
? undefined
|
|
|
|
: uriInfoHook.response;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For the exchange selected, bring the status of the terms of service
|
|
|
|
*/
|
|
|
|
const terms = useAsyncAsHook(async () => {
|
2022-08-10 16:50:46 +02:00
|
|
|
if (!uriHookDep?.thisExchange) return false;
|
2022-07-21 15:36:15 +02:00
|
|
|
|
2022-08-10 16:50:46 +02:00
|
|
|
const exchangeTos = await api.getExchangeTos(uriHookDep.thisExchange, ["text/xml"]);
|
2022-07-21 15:36:15 +02:00
|
|
|
|
|
|
|
const state = buildTermsOfServiceState(exchangeTos);
|
|
|
|
|
|
|
|
return { state };
|
2022-08-10 16:50:46 +02:00
|
|
|
}, [uriHookDep]);
|
2022-07-21 15:36:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* With the exchange and amount, ask the wallet the information
|
|
|
|
* about the withdrawal
|
|
|
|
*/
|
2022-08-10 16:50:46 +02:00
|
|
|
const amountHook = useAsyncAsHook(async () => {
|
|
|
|
if (!uriHookDep?.thisExchange) return false;
|
2022-07-21 15:36:15 +02:00
|
|
|
|
|
|
|
const info = await api.getExchangeWithdrawalInfo({
|
2022-08-10 16:50:46 +02:00
|
|
|
exchangeBaseUrl: uriHookDep?.thisExchange,
|
|
|
|
amount: Amounts.parseOrThrow(uriHookDep.amount),
|
2022-07-21 15:36:15 +02:00
|
|
|
tosAcceptedFormat: ["text/xml"],
|
|
|
|
});
|
|
|
|
|
2022-08-10 16:50:46 +02:00
|
|
|
const withdrawAmount = {
|
|
|
|
raw: Amounts.parseOrThrow(info.withdrawalAmountRaw),
|
|
|
|
effective: Amounts.parseOrThrow(info.withdrawalAmountEffective),
|
|
|
|
}
|
2022-07-21 15:36:15 +02:00
|
|
|
|
2022-08-10 16:50:46 +02:00
|
|
|
return { amount: withdrawAmount };
|
|
|
|
}, [uriHookDep]);
|
2022-07-21 15:36:15 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
const [withdrawCompleted, setWithdrawCompleted] = useState<boolean>(false);
|
|
|
|
|
2022-07-31 01:55:41 +02:00
|
|
|
if (!uriInfoHook) return { status: "loading", error: undefined }
|
|
|
|
if (uriInfoHook.hasError) {
|
2022-07-21 15:36:15 +02:00
|
|
|
return {
|
|
|
|
status: "loading-uri",
|
2022-07-31 01:55:41 +02:00
|
|
|
error: uriInfoHook,
|
2022-07-21 15:36:15 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-10 16:50:46 +02:00
|
|
|
const { amount, thisExchange } = uriInfoHook.response
|
2022-08-29 16:32:07 +02:00
|
|
|
|
2022-08-10 16:50:46 +02:00
|
|
|
const chosenAmount = Amounts.parseOrThrow(amount);
|
|
|
|
|
|
|
|
if (!thisExchange) {
|
2022-07-21 15:36:15 +02:00
|
|
|
return {
|
|
|
|
status: "loading-exchange",
|
2022-07-31 01:55:41 +02:00
|
|
|
error: {
|
2022-07-21 15:36:15 +02:00
|
|
|
hasError: true,
|
|
|
|
operational: false,
|
|
|
|
message: "ERROR_NO-DEFAULT-EXCHANGE",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-10 16:50:46 +02:00
|
|
|
// const selectedExchange = thisExchange;
|
2022-07-21 15:36:15 +02:00
|
|
|
|
|
|
|
async function doWithdrawAndCheckError(): Promise<void> {
|
2022-08-10 16:50:46 +02:00
|
|
|
if (!thisExchange) return;
|
|
|
|
|
2022-07-21 15:36:15 +02:00
|
|
|
try {
|
|
|
|
setDoingWithdraw(true);
|
|
|
|
if (!talerWithdrawUri) return;
|
|
|
|
const res = await api.acceptWithdrawal(
|
|
|
|
talerWithdrawUri,
|
2022-08-10 16:50:46 +02:00
|
|
|
thisExchange,
|
2022-07-21 15:36:15 +02:00
|
|
|
!ageRestricted ? undefined : ageRestricted,
|
|
|
|
);
|
|
|
|
if (res.confirmTransferUrl) {
|
|
|
|
document.location.href = res.confirmTransferUrl;
|
|
|
|
}
|
|
|
|
setWithdrawCompleted(true);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof TalerError) {
|
|
|
|
setWithdrawError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setDoingWithdraw(false);
|
|
|
|
}
|
|
|
|
|
2022-08-10 16:50:46 +02:00
|
|
|
if (!amountHook) {
|
2022-07-31 01:55:41 +02:00
|
|
|
return { status: "loading", error: undefined }
|
|
|
|
}
|
2022-08-10 16:50:46 +02:00
|
|
|
if (amountHook.hasError) {
|
2022-07-21 15:36:15 +02:00
|
|
|
return {
|
|
|
|
status: "loading-info",
|
2022-08-10 16:50:46 +02:00
|
|
|
error: amountHook,
|
2022-07-21 15:36:15 +02:00
|
|
|
};
|
|
|
|
}
|
2022-08-10 16:50:46 +02:00
|
|
|
if (!amountHook.response) {
|
2022-07-31 01:55:41 +02:00
|
|
|
return { status: "loading", error: undefined };
|
2022-07-21 15:36:15 +02:00
|
|
|
}
|
|
|
|
if (withdrawCompleted) {
|
2022-07-31 01:55:41 +02:00
|
|
|
return { status: "completed", error: undefined };
|
2022-07-21 15:36:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 16:50:46 +02:00
|
|
|
const withdrawalFee = Amounts.sub(
|
|
|
|
amountHook.response.amount.raw,
|
|
|
|
amountHook.response.amount.effective,
|
|
|
|
).amount;
|
|
|
|
const toBeReceived = amountHook.response.amount.effective;
|
2022-07-21 15:36:15 +02:00
|
|
|
|
|
|
|
const { state: termsState } = (!terms
|
|
|
|
? undefined
|
|
|
|
: terms.hasError
|
|
|
|
? undefined
|
|
|
|
: terms.response) || { state: undefined };
|
|
|
|
|
|
|
|
async function onAccept(accepted: boolean): Promise<void> {
|
2022-08-10 16:50:46 +02:00
|
|
|
if (!termsState || !thisExchange) return;
|
2022-07-21 15:36:15 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await api.setExchangeTosAccepted(
|
2022-08-10 16:50:46 +02:00
|
|
|
thisExchange,
|
2022-07-21 15:36:15 +02:00
|
|
|
accepted ? termsState.version : undefined,
|
|
|
|
);
|
|
|
|
setReviewed(accepted);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Error) {
|
|
|
|
//FIXME: uncomment this and display error
|
|
|
|
// setErrorAccepting(e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mustAcceptFirst =
|
|
|
|
termsState !== undefined &&
|
|
|
|
(termsState.status === "changed" || termsState.status === "new");
|
|
|
|
|
|
|
|
const ageRestrictionOptions: Record<string, string> | undefined = "6:12:18"
|
|
|
|
.split(":")
|
|
|
|
.reduce((p, c) => ({ ...p, [c]: `under ${c}` }), {});
|
|
|
|
|
|
|
|
if (ageRestrictionOptions) {
|
|
|
|
ageRestrictionOptions["0"] = "Not restricted";
|
|
|
|
}
|
|
|
|
|
2022-08-10 16:50:46 +02:00
|
|
|
//TODO: calculate based on exchange info
|
|
|
|
const ageRestrictionEnabled = false;
|
|
|
|
const ageRestriction = ageRestrictionEnabled ? {
|
|
|
|
list: ageRestrictionOptions,
|
|
|
|
value: String(ageRestricted),
|
|
|
|
onChange: async (v: string) => setAgeRestricted(parseInt(v, 10)),
|
|
|
|
} : undefined;
|
|
|
|
|
2022-07-21 15:36:15 +02:00
|
|
|
return {
|
|
|
|
status: "success",
|
2022-07-31 01:55:41 +02:00
|
|
|
error: undefined,
|
2022-08-10 16:50:46 +02:00
|
|
|
exchangeUrl: thisExchange,
|
2022-07-21 15:36:15 +02:00
|
|
|
toBeReceived,
|
|
|
|
withdrawalFee,
|
2022-08-10 16:50:46 +02:00
|
|
|
chosenAmount,
|
|
|
|
ageRestriction,
|
2022-07-21 15:36:15 +02:00
|
|
|
doWithdrawal: {
|
|
|
|
onClick:
|
|
|
|
doingWithdraw || (mustAcceptFirst && !reviewed)
|
|
|
|
? undefined
|
|
|
|
: doWithdrawAndCheckError,
|
|
|
|
error: withdrawError,
|
|
|
|
},
|
|
|
|
tosProps: !termsState
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
onAccept,
|
|
|
|
onReview: setReviewing,
|
|
|
|
reviewed: reviewed,
|
|
|
|
reviewing: reviewing,
|
|
|
|
terms: termsState,
|
|
|
|
},
|
|
|
|
mustAcceptFirst,
|
2022-08-10 16:50:46 +02:00
|
|
|
cancel,
|
2022-07-21 15:36:15 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|