2019-08-29 23:12:55 +02:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
|
|
|
(C) 2015 GNUnet e.V.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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
|
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Page shown to the user to confirm entering
|
|
|
|
* a contract.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
2019-12-12 20:53:15 +01:00
|
|
|
import * as i18n from "../i18n";
|
2019-08-29 23:12:55 +02:00
|
|
|
|
2020-04-06 17:35:51 +02:00
|
|
|
import { renderAmount, ProgressButton } from "../renderHtml";
|
2019-08-29 23:12:55 +02:00
|
|
|
import * as wxApi from "../wxApi";
|
|
|
|
|
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
|
2020-08-19 16:09:21 +02:00
|
|
|
import {
|
|
|
|
Amounts,
|
|
|
|
AmountJson,
|
|
|
|
PreparePayResult,
|
|
|
|
PreparePayResultType,
|
|
|
|
ContractTerms,
|
|
|
|
ConfirmPayResultType,
|
2020-08-24 16:09:09 +02:00
|
|
|
ConfirmPayResult,
|
|
|
|
getJsonI18n,
|
2020-08-19 16:09:21 +02:00
|
|
|
} from "taler-wallet-core";
|
2019-08-29 23:12:55 +02:00
|
|
|
|
2020-04-06 20:02:01 +02:00
|
|
|
function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element {
|
2020-08-12 09:11:00 +02:00
|
|
|
const [payStatus, setPayStatus] = useState<PreparePayResult | undefined>();
|
2020-08-24 16:09:09 +02:00
|
|
|
const [payResult, setPayResult] = useState<ConfirmPayResult | undefined>();
|
2019-08-29 23:12:55 +02:00
|
|
|
const [payErrMsg, setPayErrMsg] = useState<string | undefined>("");
|
|
|
|
const [numTries, setNumTries] = useState(0);
|
2019-08-31 11:49:36 +02:00
|
|
|
const [loading, setLoading] = useState(false);
|
2021-01-17 18:33:25 +01:00
|
|
|
let totalFees: AmountJson | undefined = undefined;
|
2019-08-29 23:12:55 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-04-06 20:02:01 +02:00
|
|
|
const doFetch = async (): Promise<void> => {
|
2019-08-29 23:12:55 +02:00
|
|
|
const p = await wxApi.preparePay(talerPayUri);
|
|
|
|
setPayStatus(p);
|
|
|
|
};
|
|
|
|
doFetch();
|
2020-04-07 10:07:32 +02:00
|
|
|
}, [numTries, talerPayUri]);
|
2019-08-29 23:12:55 +02:00
|
|
|
|
|
|
|
if (!payStatus) {
|
|
|
|
return <span>Loading payment information ...</span>;
|
|
|
|
}
|
|
|
|
|
2019-11-01 18:39:23 +01:00
|
|
|
let insufficientBalance = false;
|
2020-08-24 08:22:12 +02:00
|
|
|
if (payStatus.status == PreparePayResultType.InsufficientBalance) {
|
2019-11-01 18:39:23 +01:00
|
|
|
insufficientBalance = true;
|
|
|
|
}
|
|
|
|
|
2020-08-24 08:22:12 +02:00
|
|
|
if (payStatus.status === PreparePayResultType.PaymentPossible) {
|
2021-01-17 18:33:25 +01:00
|
|
|
let amountRaw = Amounts.parseOrThrow(payStatus.amountRaw);
|
|
|
|
let amountEffective: AmountJson = Amounts.parseOrThrow(payStatus.amountEffective);
|
|
|
|
totalFees = Amounts.sub(amountEffective, amountRaw).amount;
|
2019-08-29 23:12:55 +02:00
|
|
|
}
|
|
|
|
|
2020-08-19 16:09:21 +02:00
|
|
|
if (
|
|
|
|
payStatus.status === PreparePayResultType.AlreadyConfirmed &&
|
|
|
|
numTries === 0
|
|
|
|
) {
|
2020-08-24 16:09:09 +02:00
|
|
|
const fulfillmentUrl = payStatus.contractTerms.fulfillment_url;
|
|
|
|
if (fulfillmentUrl) {
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
You have already paid for this article. Click{" "}
|
|
|
|
<a href={fulfillmentUrl}>here</a> to view it again.
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
} else {
|
2019-08-29 23:12:55 +02:00
|
|
|
<span>
|
2020-08-24 16:09:09 +02:00
|
|
|
You have already paid for this article:{" "}
|
|
|
|
<em>
|
|
|
|
{payStatus.contractTerms.fulfillment_message ?? "no message given"}
|
|
|
|
</em>
|
|
|
|
</span>;
|
|
|
|
}
|
2019-08-29 23:12:55 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 16:09:09 +02:00
|
|
|
let contractTerms: ContractTerms = payStatus.contractTerms;
|
2019-08-29 23:12:55 +02:00
|
|
|
|
|
|
|
if (!contractTerms) {
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
Error: did not get contract terms from merchant or wallet backend.
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let merchantName: React.ReactElement;
|
|
|
|
if (contractTerms.merchant && contractTerms.merchant.name) {
|
|
|
|
merchantName = <strong>{contractTerms.merchant.name}</strong>;
|
|
|
|
} else {
|
|
|
|
merchantName = <strong>(pub: {contractTerms.merchant_pub})</strong>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const amount = (
|
|
|
|
<strong>{renderAmount(Amounts.parseOrThrow(contractTerms.amount))}</strong>
|
|
|
|
);
|
|
|
|
|
2020-04-06 20:02:01 +02:00
|
|
|
const doPayment = async (): Promise<void> => {
|
2019-09-06 11:06:28 +02:00
|
|
|
if (payStatus.status !== "payment-possible") {
|
2019-11-01 18:39:23 +01:00
|
|
|
throw Error(`invalid state: ${payStatus.status}`);
|
2019-09-06 11:06:28 +02:00
|
|
|
}
|
|
|
|
const proposalId = payStatus.proposalId;
|
2019-08-29 23:12:55 +02:00
|
|
|
setNumTries(numTries + 1);
|
|
|
|
try {
|
2019-08-31 11:49:36 +02:00
|
|
|
setLoading(true);
|
2019-09-06 11:06:28 +02:00
|
|
|
const res = await wxApi.confirmPay(proposalId, undefined);
|
2020-08-12 09:11:00 +02:00
|
|
|
if (res.type !== ConfirmPayResultType.Done) {
|
2020-08-11 14:02:11 +02:00
|
|
|
throw Error("payment pending");
|
|
|
|
}
|
2020-08-24 16:09:09 +02:00
|
|
|
const fu = res.contractTerms.fulfillment_url;
|
|
|
|
if (fu) {
|
|
|
|
document.location.href = fu;
|
|
|
|
}
|
|
|
|
setPayResult(res);
|
2019-08-29 23:12:55 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
setPayErrMsg(e.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-24 16:09:09 +02:00
|
|
|
if (payResult && payResult.type === ConfirmPayResultType.Done) {
|
|
|
|
if (payResult.contractTerms.fulfillment_message) {
|
|
|
|
const obj = {
|
|
|
|
fulfillment_message: payResult.contractTerms.fulfillment_message,
|
|
|
|
fulfillment_message_i18n: payResult.contractTerms.fulfillment_message_i18n,
|
|
|
|
};
|
|
|
|
const msg = getJsonI18n(obj, "fulfillment_message")
|
|
|
|
return <div>
|
|
|
|
<p>Payment succeeded.</p>
|
|
|
|
<p>{msg}</p>
|
|
|
|
</div>;
|
|
|
|
} else {
|
|
|
|
return <span>Redirecting ...</span>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 23:12:55 +02:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p>
|
|
|
|
<i18n.Translate wrap="p">
|
|
|
|
The merchant <span>{merchantName}</span> offers you to purchase:
|
|
|
|
</i18n.Translate>
|
|
|
|
<div style={{ textAlign: "center" }}>
|
|
|
|
<strong>{contractTerms.summary}</strong>
|
|
|
|
</div>
|
2021-01-17 18:33:25 +01:00
|
|
|
{totalFees ? (
|
2019-08-29 23:12:55 +02:00
|
|
|
<i18n.Translate wrap="p">
|
|
|
|
The total price is <span>{amount} </span>
|
2021-01-17 18:33:25 +01:00
|
|
|
(plus <span>{renderAmount(totalFees)}</span> fees).
|
2019-08-29 23:12:55 +02:00
|
|
|
</i18n.Translate>
|
|
|
|
) : (
|
|
|
|
<i18n.Translate wrap="p">
|
|
|
|
The total price is <span>{amount}</span>.
|
|
|
|
</i18n.Translate>
|
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
|
2019-11-01 18:39:23 +01:00
|
|
|
{insufficientBalance ? (
|
|
|
|
<div>
|
2020-03-30 12:39:32 +02:00
|
|
|
<p style={{ color: "red", fontWeight: "bold" }}>
|
|
|
|
Unable to pay: Your balance is insufficient.
|
|
|
|
</p>
|
2019-11-01 18:39:23 +01:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
2019-08-29 23:12:55 +02:00
|
|
|
{payErrMsg ? (
|
|
|
|
<div>
|
|
|
|
<p>Payment failed: {payErrMsg}</p>
|
|
|
|
<button
|
|
|
|
className="pure-button button-success"
|
|
|
|
onClick={() => doPayment()}
|
|
|
|
>
|
|
|
|
{i18n.str`Retry`}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div>
|
2019-08-31 11:49:36 +02:00
|
|
|
<ProgressButton
|
|
|
|
loading={loading}
|
2019-11-01 18:39:23 +01:00
|
|
|
disabled={insufficientBalance}
|
2020-03-30 12:39:32 +02:00
|
|
|
onClick={() => doPayment()}
|
|
|
|
>
|
2019-08-29 23:12:55 +02:00
|
|
|
{i18n.str`Confirm payment`}
|
2019-08-31 11:49:36 +02:00
|
|
|
</ProgressButton>
|
2019-08-29 23:12:55 +02:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:46:56 +02:00
|
|
|
export function createPayPage(): JSX.Element {
|
2019-12-02 00:42:40 +01:00
|
|
|
const url = new URL(document.location.href);
|
|
|
|
const talerPayUri = url.searchParams.get("talerPayUri");
|
|
|
|
if (!talerPayUri) {
|
|
|
|
throw Error("invalid parameter");
|
|
|
|
}
|
2019-09-05 16:23:54 +02:00
|
|
|
return <TalerPayDialog talerPayUri={talerPayUri} />;
|
2020-04-06 20:02:01 +02:00
|
|
|
}
|