2022-08-29 18:23:22 +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-09-16 19:29:35 +02:00
|
|
|
import {
|
|
|
|
AbsoluteTime,
|
|
|
|
Amounts,
|
|
|
|
NotificationType,
|
|
|
|
PreparePayResult,
|
|
|
|
PreparePayResultType,
|
|
|
|
TalerErrorDetail,
|
2022-11-16 20:04:52 +01:00
|
|
|
TalerProtocolTimestamp,
|
2022-09-16 19:29:35 +02:00
|
|
|
} from "@gnu-taler/taler-util";
|
2022-10-25 17:23:08 +02:00
|
|
|
import { TalerError, WalletApiOperation } from "@gnu-taler/taler-wallet-core";
|
2022-09-11 04:21:44 +02:00
|
|
|
import { useEffect, useState } from "preact/hooks";
|
2023-01-09 12:38:48 +01:00
|
|
|
import { alertFromError } from "../../context/alert.js";
|
2022-12-15 21:11:24 +01:00
|
|
|
import { useBackendContext } from "../../context/backend.js";
|
2023-01-09 12:38:48 +01:00
|
|
|
import { useTranslationContext } from "../../context/translation.js";
|
2022-08-29 18:23:22 +02:00
|
|
|
import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js";
|
|
|
|
import { Props, State } from "./index.js";
|
|
|
|
|
2022-12-15 21:12:03 +01:00
|
|
|
export function useComponentState({
|
|
|
|
talerPayPullUri,
|
|
|
|
onClose,
|
|
|
|
goToWalletManualWithdraw,
|
|
|
|
onSuccess,
|
|
|
|
}: Props): State {
|
|
|
|
const api = useBackendContext();
|
2023-01-09 12:38:48 +01:00
|
|
|
const { i18n } = useTranslationContext();
|
2022-08-31 05:20:35 +02:00
|
|
|
const hook = useAsyncAsHook(async () => {
|
2022-10-25 17:23:08 +02:00
|
|
|
const p2p = await api.wallet.call(WalletApiOperation.CheckPeerPullPayment, {
|
2022-09-16 19:29:35 +02:00
|
|
|
talerUri: talerPayPullUri,
|
|
|
|
});
|
2022-10-25 17:23:08 +02:00
|
|
|
const balance = await api.wallet.call(WalletApiOperation.GetBalances, {});
|
2022-09-16 19:29:35 +02:00
|
|
|
return { p2p, balance };
|
|
|
|
});
|
2022-09-11 04:21:44 +02:00
|
|
|
|
2022-11-16 20:04:52 +01:00
|
|
|
useEffect(() =>
|
|
|
|
api.listener.onUpdateNotification(
|
|
|
|
[NotificationType.CoinWithdrawn],
|
|
|
|
hook?.retry,
|
|
|
|
),
|
|
|
|
);
|
2022-09-11 04:21:44 +02:00
|
|
|
|
2022-09-16 19:29:35 +02:00
|
|
|
const [operationError, setOperationError] = useState<
|
|
|
|
TalerErrorDetail | undefined
|
|
|
|
>(undefined);
|
2022-08-29 18:23:22 +02:00
|
|
|
|
|
|
|
if (!hook) {
|
|
|
|
return {
|
|
|
|
status: "loading",
|
|
|
|
error: undefined,
|
2022-09-16 19:29:35 +02:00
|
|
|
};
|
2022-08-29 18:23:22 +02:00
|
|
|
}
|
|
|
|
if (hook.hasError) {
|
|
|
|
return {
|
2023-01-09 12:38:48 +01:00
|
|
|
status: "error",
|
|
|
|
error: alertFromError(
|
|
|
|
i18n.str`Could not load the status of the term of service`,
|
|
|
|
hook,
|
|
|
|
),
|
2022-08-29 18:23:22 +02:00
|
|
|
};
|
|
|
|
}
|
2023-01-09 12:38:48 +01:00
|
|
|
// if (hook.hasError) {
|
|
|
|
// return {
|
|
|
|
// status: "loading-uri",
|
|
|
|
// error: hook,
|
|
|
|
// };
|
|
|
|
// }
|
2022-08-29 18:23:22 +02:00
|
|
|
|
2022-11-16 20:04:52 +01:00
|
|
|
const { contractTerms, peerPullPaymentIncomingId } = hook.response.p2p;
|
2022-08-31 16:46:39 +02:00
|
|
|
|
2022-09-16 19:29:35 +02:00
|
|
|
const amountStr: string = contractTerms?.amount;
|
|
|
|
const amount = Amounts.parseOrThrow(amountStr);
|
|
|
|
const summary: string | undefined = contractTerms?.summary;
|
|
|
|
const expiration: TalerProtocolTimestamp | undefined =
|
|
|
|
contractTerms?.purse_expiration;
|
2022-08-31 05:20:35 +02:00
|
|
|
|
2022-09-11 04:21:44 +02:00
|
|
|
const foundBalance = hook.response.balance.balances.find(
|
|
|
|
(b) => Amounts.parseOrThrow(b.available).currency === amount.currency,
|
|
|
|
);
|
|
|
|
|
|
|
|
const paymentPossible: PreparePayResult = {
|
|
|
|
status: PreparePayResultType.PaymentPossible,
|
|
|
|
proposalId: "fakeID",
|
2022-09-16 19:29:35 +02:00
|
|
|
contractTerms: {} as any,
|
2022-09-11 04:21:44 +02:00
|
|
|
contractTermsHash: "asd",
|
|
|
|
amountRaw: hook.response.p2p.amount,
|
|
|
|
amountEffective: hook.response.p2p.amount,
|
|
|
|
noncePriv: "",
|
2022-09-16 19:29:35 +02:00
|
|
|
} as PreparePayResult;
|
2022-09-11 04:21:44 +02:00
|
|
|
|
|
|
|
const insufficientBalance: PreparePayResult = {
|
|
|
|
status: PreparePayResultType.InsufficientBalance,
|
2022-11-25 03:16:01 +01:00
|
|
|
talerUri: "taler://pay",
|
2022-09-11 04:21:44 +02:00
|
|
|
proposalId: "fakeID",
|
2022-09-16 19:29:35 +02:00
|
|
|
contractTerms: {} as any,
|
2022-09-11 04:21:44 +02:00
|
|
|
amountRaw: hook.response.p2p.amount,
|
|
|
|
noncePriv: "",
|
2022-09-16 19:29:35 +02:00
|
|
|
};
|
2022-09-11 04:21:44 +02:00
|
|
|
|
|
|
|
const baseResult = {
|
|
|
|
uri: talerPayPullUri,
|
|
|
|
cancel: {
|
2022-09-16 19:29:35 +02:00
|
|
|
onClick: onClose,
|
2022-09-11 04:21:44 +02:00
|
|
|
},
|
|
|
|
amount,
|
|
|
|
goToWalletManualWithdraw,
|
|
|
|
summary,
|
|
|
|
expiration: expiration ? AbsoluteTime.fromTimestamp(expiration) : undefined,
|
|
|
|
operationError,
|
2022-09-16 19:29:35 +02:00
|
|
|
};
|
2022-09-11 04:21:44 +02:00
|
|
|
|
|
|
|
if (!foundBalance) {
|
|
|
|
return {
|
|
|
|
status: "no-balance-for-currency",
|
|
|
|
error: undefined,
|
|
|
|
balance: undefined,
|
|
|
|
...baseResult,
|
|
|
|
payStatus: insufficientBalance,
|
2022-09-16 19:29:35 +02:00
|
|
|
};
|
2022-09-11 04:21:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const foundAmount = Amounts.parseOrThrow(foundBalance.available);
|
|
|
|
|
|
|
|
//FIXME: should use pay result type since it check for coins exceptions
|
2022-09-16 19:29:35 +02:00
|
|
|
if (Amounts.cmp(foundAmount, amount) < 0) {
|
|
|
|
//payStatus.status === PreparePayResultType.InsufficientBalance) {
|
2022-09-11 04:21:44 +02:00
|
|
|
return {
|
2022-09-16 19:29:35 +02:00
|
|
|
status: "no-enough-balance",
|
2022-09-11 04:21:44 +02:00
|
|
|
error: undefined,
|
|
|
|
balance: foundAmount,
|
|
|
|
...baseResult,
|
|
|
|
payStatus: insufficientBalance,
|
2022-09-16 19:29:35 +02:00
|
|
|
};
|
2022-09-11 04:21:44 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 05:20:35 +02:00
|
|
|
async function accept(): Promise<void> {
|
|
|
|
try {
|
2022-11-16 20:04:52 +01:00
|
|
|
const resp = await api.wallet.call(
|
|
|
|
WalletApiOperation.AcceptPeerPullPayment,
|
|
|
|
{
|
|
|
|
peerPullPaymentIncomingId,
|
|
|
|
},
|
|
|
|
);
|
2022-09-16 21:03:58 +02:00
|
|
|
onSuccess(resp.transactionId);
|
2022-08-31 05:20:35 +02:00
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof TalerError) {
|
2022-09-16 19:29:35 +02:00
|
|
|
setOperationError(e.errorDetail);
|
2022-08-31 05:20:35 +02:00
|
|
|
}
|
2022-09-16 19:29:35 +02:00
|
|
|
console.error(e);
|
|
|
|
throw Error("error trying to accept");
|
2022-08-31 05:20:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 18:23:22 +02:00
|
|
|
return {
|
|
|
|
status: "ready",
|
|
|
|
error: undefined,
|
2022-09-11 04:21:44 +02:00
|
|
|
...baseResult,
|
|
|
|
payStatus: paymentPossible,
|
|
|
|
balance: foundAmount,
|
2022-08-31 05:20:35 +02:00
|
|
|
accept: {
|
2022-09-16 19:29:35 +02:00
|
|
|
onClick: accept,
|
2022-08-31 05:20:35 +02:00
|
|
|
},
|
2022-09-16 19:29:35 +02:00
|
|
|
};
|
2022-08-29 18:23:22 +02:00
|
|
|
}
|