wallet-core/packages/taler-wallet-webextension/src/cta/Payment/views.tsx

149 lines
4.4 KiB
TypeScript
Raw Normal View History

/*
2022-06-06 17:05:26 +02:00
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
2022-06-06 17:05:26 +02:00
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.
2022-06-06 17:05:26 +02:00
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
2022-06-06 17:05:26 +02:00
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
2021-11-15 15:18:58 +01:00
import {
AbsoluteTime,
2021-11-15 15:18:58 +01:00
Amounts,
2022-11-01 11:34:20 +01:00
MerchantContractTerms as ContractTerms,
2021-11-15 15:18:58 +01:00
PreparePayResultType,
2023-01-09 12:38:48 +01:00
TranslatedString,
2021-11-15 15:18:58 +01:00
} from "@gnu-taler/taler-util";
2021-11-16 17:59:53 +01:00
import { Fragment, h, VNode } from "preact";
2022-07-31 01:55:41 +02:00
import { Part } from "../../components/Part.js";
2023-01-04 15:24:58 +01:00
import { PaymentButtons } from "../../components/PaymentButtons.js";
2023-01-09 12:38:48 +01:00
import { SuccessBox, WarningBox } from "../../components/styled/index.js";
import { Time } from "../../components/Time.js";
2023-05-05 13:47:00 +02:00
import { useTranslationContext } from "@gnu-taler/web-util/browser";
2023-01-20 19:44:53 +01:00
import {
getAmountWithFee,
MerchantDetails,
PurchaseDetails,
} from "../../wallet/Transaction.js";
2022-07-31 01:55:41 +02:00
import { State } from "./index.js";
2022-04-21 19:23:53 +02:00
2022-07-31 01:55:41 +02:00
type SupportedStates =
| State.Ready
| State.Confirmed
| State.NoBalanceForCurrency
| State.NoEnoughBalance;
export function BaseView(state: SupportedStates): VNode {
const { i18n } = useTranslationContext();
2022-11-25 03:16:01 +01:00
2022-04-21 19:23:53 +02:00
const contractTerms: ContractTerms = state.payStatus.contractTerms;
2023-01-20 19:44:53 +01:00
const effective =
"amountEffective" in state.payStatus
? Amounts.parseOrThrow(state.payStatus.amountEffective)
: state.amount;
2021-11-15 15:18:58 +01:00
return (
2023-01-04 15:24:58 +01:00
<Fragment>
2022-04-21 19:23:53 +02:00
<ShowImportantMessage state={state} />
<section style={{ textAlign: "left" }}>
<Part
2023-01-09 12:38:48 +01:00
title={i18n.str`Purchase`}
text={contractTerms.summary as TranslatedString}
kind="neutral"
/>
2021-11-15 15:18:58 +01:00
<Part
2023-01-09 12:38:48 +01:00
title={i18n.str`Merchant`}
text={<MerchantDetails merchant={contractTerms.merchant} />}
2021-11-15 15:18:58 +01:00
kind="neutral"
/>
2022-02-23 19:18:37 +01:00
<Part
2023-01-09 12:38:48 +01:00
title={i18n.str`Details`}
text={
<PurchaseDetails
2023-01-20 19:44:53 +01:00
price={getAmountWithFee(effective, state.amount, "debit")}
info={{
...contractTerms,
orderId: contractTerms.order_id,
contractTermsHash: "",
products: contractTerms.products!,
}}
proposalId={state.payStatus.proposalId}
/>
}
2022-02-23 19:18:37 +01:00
kind="neutral"
/>
2021-11-15 15:18:58 +01:00
{contractTerms.order_id && (
<Part
2023-01-09 12:38:48 +01:00
title={i18n.str`Receipt`}
text={`#${contractTerms.order_id}` as TranslatedString}
2021-11-15 15:18:58 +01:00
kind="neutral"
/>
)}
{contractTerms.pay_deadline && (
<Part
2023-01-09 12:38:48 +01:00
title={i18n.str`Valid until`}
text={
<Time
timestamp={AbsoluteTime.fromProtocolTimestamp(
contractTerms.pay_deadline,
)}
format="dd MMMM yyyy, HH:mm"
/>
}
kind="neutral"
/>
2022-01-19 17:51:48 +01:00
)}
2021-11-15 15:18:58 +01:00
</section>
2023-01-04 15:24:58 +01:00
<PaymentButtons
2022-09-16 21:04:41 +02:00
amount={state.amount}
payStatus={state.payStatus}
uri={state.uri}
payHandler={state.status === "ready" ? state.payHandler : undefined}
goToWalletManualWithdraw={state.goToWalletManualWithdraw}
/>
2022-01-19 17:51:48 +01:00
</Fragment>
);
}
2022-04-21 19:23:53 +02:00
2022-07-31 01:55:41 +02:00
function ShowImportantMessage({ state }: { state: SupportedStates }): VNode {
2022-04-21 19:23:53 +02:00
const { i18n } = useTranslationContext();
const { payStatus } = state;
if (payStatus.status === PreparePayResultType.AlreadyConfirmed) {
if (payStatus.paid) {
if (payStatus.contractTerms.fulfillment_url) {
return (
<SuccessBox>
<i18n.Translate>
Already paid, you are going to be redirected to{" "}
<a href={payStatus.contractTerms.fulfillment_url}>
{payStatus.contractTerms.fulfillment_url}
</a>
</i18n.Translate>
</SuccessBox>
);
}
return (
<SuccessBox>
<i18n.Translate>Already paid</i18n.Translate>
</SuccessBox>
);
}
return (
<WarningBox>
<i18n.Translate>Already claimed</i18n.Translate>
</WarningBox>
);
}
return <Fragment />;
}