1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
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/>
*/
import { AmountJson, Amounts, PreparePayResult } from "@gnu-taler/taler-util";
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
import { useState } from "preact/hooks";
import { alertFromError, useAlertContext } from "../../context/alert.js";
import { useBackendContext } from "../../context/backend.js";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js";
import { AmountFieldHandler, TextFieldHandler } from "../../mui/handlers.js";
import { Props, State } from "./index.js";
import { RecursiveState } from "../../utils/index.js";
export function useComponentState({
talerTemplateUri,
cancel,
goToWalletManualWithdraw,
onSuccess,
}: Props): RecursiveState<State> {
const api = useBackendContext();
const { i18n } = useTranslationContext();
const { safely } = useAlertContext();
// const url = talerTemplateUri ? new URL(talerTemplateUri) : undefined;
// const parsedAmount = !amountParam ? undefined : Amounts.parse(amountParam);
// const currency = parsedAmount ? parsedAmount.currency : amountParam;
// const initialAmount =
// parsedAmount ?? (currency ? Amounts.zeroOfCurrency(currency) : undefined);
const [newOrder, setNewOrder] = useState("");
const hook = useAsyncAsHook(async () => {
if (!talerTemplateUri) throw Error("ERROR_NO-URI-FOR-PAYMENT-TEMPLATE");
const templateP = await api.wallet.call(
WalletApiOperation.CheckPayForTemplate, { talerPayTemplateUri: talerTemplateUri },
);
const requireMoreInfo = !templateP.template_contract.amount || !templateP.template_contract.summary;
let payStatus: PreparePayResult | undefined = undefined;
if (!requireMoreInfo) {
payStatus = await api.wallet.call(WalletApiOperation.PreparePayForTemplate, { talerPayTemplateUri: talerTemplateUri });
}
const balance = await api.wallet.call(WalletApiOperation.GetBalances, {});
return { payStatus, balance, uri: talerTemplateUri, templateP };
}, []);
if (!hook) {
return {
status: "loading",
error: undefined,
};
}
if (hook.hasError) {
return {
status: "error",
error: alertFromError(
i18n,
i18n.str`Could not load the status of the order template`,
hook,
),
};
}
if (hook.response.payStatus) {
return {
status: "order-ready",
error: undefined,
cancel,
goToWalletManualWithdraw,
onSuccess,
talerPayUri: hook.response.payStatus.talerUri!,
};
}
if (newOrder) {
return {
status: "order-ready",
error: undefined,
cancel,
goToWalletManualWithdraw,
onSuccess,
talerPayUri: newOrder,
};
}
return () => {
const cfg = hook.response.templateP.template_contract;
const def = hook.response.templateP.editable_defaults;
const fixedAmount = cfg.amount !== undefined ? Amounts.parseOrThrow(cfg.amount) : undefined;
const fixedSummary = cfg.summary !== undefined ? cfg.summary : undefined;
const defaultAmount = def?.amount !== undefined ? Amounts.parseOrThrow(def.amount) : undefined;
const defaultSummary = def?.summary !== undefined ? def.summary : undefined;
const zero = fixedAmount ? Amounts.zeroOfAmount(fixedAmount) :
cfg.currency !== undefined ? Amounts.zeroOfCurrency(cfg.currency) :
defaultAmount !== undefined ? Amounts.zeroOfAmount(defaultAmount) :
def?.currency !== undefined ? Amounts.zeroOfCurrency(def.currency) :
Amounts.zeroOfCurrency(hook.response.templateP.supportedCurrencies[0]);
const [amount, setAmount] = useState(defaultAmount ?? zero);
const [summary, setSummary] = useState(defaultSummary ?? "");
async function createOrder() {
try {
const templateParams: Record<string, string> = {};
if (amount && !fixedAmount) {
templateParams["amount"] = Amounts.stringify(amount);
}
if (summary && !fixedSummary) {
templateParams["summary"] = summary;
}
const payStatus = await api.wallet.call(
WalletApiOperation.PreparePayForTemplate,
{
talerPayTemplateUri: talerTemplateUri,
templateParams,
},
);
setNewOrder(payStatus.talerUri!);
} catch (e) {
console.error(e);
}
}
const errors = undefinedIfEmpty({
amount: fixedAmount !== undefined ? undefined : amount && Amounts.isZero(amount) ? i18n.str`required` : undefined,
summary: fixedSummary !== undefined ? undefined : summary !== undefined && !summary ? i18n.str`required` : undefined,
});
return {
status: "fill-template",
error: undefined,
amount:
fixedAmount === undefined
? ({
onInput: (a) => {
setAmount(a);
},
value: amount,
error: errors?.amount,
} as AmountFieldHandler)
: undefined,
summary:
fixedSummary === undefined
? ({
onInput: (t) => {
setSummary(t);
},
value: summary,
error: errors?.summary,
} as TextFieldHandler)
: undefined,
onCreate: {
onClick: errors
? undefined
: safely("create order for pay template", createOrder),
},
};
}
}
function undefinedIfEmpty<T extends object>(obj: T): T | undefined {
return Object.keys(obj).some(
(k) => (obj as Record<string, unknown>)[k] !== undefined,
)
? obj
: undefined;
}
|