aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-09-10 23:21:44 -0300
committerSebastian <sebasjm@gmail.com>2022-09-10 23:21:44 -0300
commite4f3acfeb2ae6a24c579e7ba8d89625f398d2ee6 (patch)
treea71787f25c9a6093ef16c7f36dec1d2e7cd94312 /packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts
parentdda90b51f6fc6fca48a68bc53088e1ed3f018a21 (diff)
downloadwallet-core-e4f3acfeb2ae6a24c579e7ba8d89625f398d2ee6.tar.xz
fix #7343
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts')
-rw-r--r--packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts105
1 files changed, 91 insertions, 14 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts b/packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts
index 27be1e424..f87cdf8e1 100644
--- a/packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts
+++ b/packages/taler-wallet-webextension/src/cta/InvoicePay/state.ts
@@ -14,22 +14,31 @@
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
-import { AbsoluteTime, Amounts, TalerErrorDetail, TalerProtocolTimestamp } from "@gnu-taler/taler-util";
+import { AbsoluteTime, Amounts, NotificationType, PreparePayResult, PreparePayResultType, TalerErrorDetail, TalerProtocolTimestamp } from "@gnu-taler/taler-util";
import { TalerError } from "@gnu-taler/taler-wallet-core";
-import { useState } from "preact/hooks";
+import { useEffect, useState } from "preact/hooks";
import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js";
import * as wxApi from "../../wxApi.js";
import { Props, State } from "./index.js";
export function useComponentState(
- { talerPayPullUri, onClose }: Props,
+ { talerPayPullUri, onClose, goToWalletManualWithdraw }: Props,
api: typeof wxApi,
): State {
const hook = useAsyncAsHook(async () => {
- return await api.checkPeerPullPayment({
+ const p2p = await api.checkPeerPullPayment({
talerUri: talerPayPullUri
})
- }, [])
+ const balance = await api.getBalance();
+ return { p2p, balance }
+ })
+
+ useEffect(() => {
+ api.onUpdateNotification([NotificationType.CoinWithdrawn], () => {
+ hook?.retry();
+ });
+ });
+
const [operationError, setOperationError] = useState<TalerErrorDetail | undefined>(undefined)
if (!hook) {
@@ -45,12 +54,84 @@ export function useComponentState(
};
}
- const { amount: purseAmount, contractTerms, peerPullPaymentIncomingId } = hook.response
+ // const { payStatus } = hook.response.p2p;
+
+ const { amount: purseAmount, contractTerms, peerPullPaymentIncomingId } = hook.response.p2p
- const amount: string = contractTerms?.amount
+
+ const amountStr: string = contractTerms?.amount
+ const amount = Amounts.parseOrThrow(amountStr)
const summary: string | undefined = contractTerms?.summary
const expiration: TalerProtocolTimestamp | undefined = contractTerms?.purse_expiration
+ const foundBalance = hook.response.balance.balances.find(
+ (b) => Amounts.parseOrThrow(b.available).currency === amount.currency,
+ );
+
+ const paymentPossible: PreparePayResult = {
+ status: PreparePayResultType.PaymentPossible,
+ proposalId: "fakeID",
+ contractTerms: {
+ } as any,
+ contractTermsHash: "asd",
+ amountRaw: hook.response.p2p.amount,
+ amountEffective: hook.response.p2p.amount,
+ noncePriv: "",
+ } as PreparePayResult
+
+ const insufficientBalance: PreparePayResult = {
+ status: PreparePayResultType.InsufficientBalance,
+ proposalId: "fakeID",
+ contractTerms: {
+ } as any,
+ amountRaw: hook.response.p2p.amount,
+ noncePriv: "",
+ }
+
+
+ const baseResult = {
+ uri: talerPayPullUri,
+ cancel: {
+ onClick: onClose
+ },
+ amount,
+ goToWalletManualWithdraw,
+ summary,
+ expiration: expiration ? AbsoluteTime.fromTimestamp(expiration) : undefined,
+ operationError,
+ }
+
+ if (!foundBalance) {
+ return {
+ status: "no-balance-for-currency",
+ error: undefined,
+ balance: undefined,
+ ...baseResult,
+ payStatus: insufficientBalance,
+ }
+ }
+
+ const foundAmount = Amounts.parseOrThrow(foundBalance.available);
+
+ //FIXME: should use pay result type since it check for coins exceptions
+ if (Amounts.cmp(foundAmount, amount) < 0) { //payStatus.status === PreparePayResultType.InsufficientBalance) {
+ return {
+ status: 'no-enough-balance',
+ error: undefined,
+ balance: foundAmount,
+ ...baseResult,
+ payStatus: insufficientBalance,
+ }
+ }
+
+ // if (payStatus.status === PreparePayResultType.AlreadyConfirmed) {
+ // return {
+ // status: "confirmed",
+ // balance: foundAmount,
+ // ...baseResult,
+ // };
+ // }
+
async function accept(): Promise<void> {
try {
const resp = await api.acceptPeerPullPayment({
@@ -69,16 +150,12 @@ export function useComponentState(
return {
status: "ready",
- amount: Amounts.parseOrThrow(amount),
error: undefined,
+ ...baseResult,
+ payStatus: paymentPossible,
+ balance: foundAmount,
accept: {
onClick: accept
},
- summary,
- expiration: expiration ? AbsoluteTime.fromTimestamp(expiration) : undefined,
- cancel: {
- onClick: onClose
- },
- operationError
}
}