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
|
/*
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.
*/
import * as i18n from "../../i18n";
import { CheckPayResult, PreparePayResult } from "../../walletTypes";
import { renderAmount, ProgressButton, registerMountPage } from "../renderHtml";
import * as wxApi from "../wxApi";
import React, { useState, useEffect } from "react";
import URI = require("urijs");
import * as Amounts from "../../amounts";
function TalerPayDialog({ talerPayUri }: { talerPayUri: string }) {
const [payStatus, setPayStatus] = useState<PreparePayResult | undefined>();
const [payErrMsg, setPayErrMsg] = useState<string | undefined>("");
const [numTries, setNumTries] = useState(0);
const [loading, setLoading] = useState(false);
let totalFees: Amounts.AmountJson | undefined = undefined;
useEffect(() => {
const doFetch = async () => {
const p = await wxApi.preparePay(talerPayUri);
setPayStatus(p);
};
doFetch();
}, [numTries]);
if (!payStatus) {
return <span>Loading payment information ...</span>;
}
if (payStatus.status === "error") {
return <span>Error: {payStatus.error}</span>;
}
if (payStatus.status === "payment-possible") {
totalFees = payStatus.totalFees;
}
if (payStatus.status === "paid" && numTries === 0) {
return (
<span>
You have already paid for this article. Click{" "}
<a href={payStatus.nextUrl}>here</a> to view it again.
</span>
);
}
const contractTerms = payStatus.contractTerms;
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>
);
const doPayment = async () => {
setNumTries(numTries + 1);
try {
setLoading(true);
const res = await wxApi.confirmPay(payStatus!.proposalId!, undefined);
document.location.href = res.nextUrl;
} catch (e) {
console.error(e);
setPayErrMsg(e.message);
}
};
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>
{totalFees ? (
<i18n.Translate wrap="p">
The total price is <span>{amount} </span>
(plus <span>{renderAmount(totalFees)}</span> fees).
</i18n.Translate>
) : (
<i18n.Translate wrap="p">
The total price is <span>{amount}</span>.
</i18n.Translate>
)}
</p>
{payErrMsg ? (
<div>
<p>Payment failed: {payErrMsg}</p>
<button
className="pure-button button-success"
onClick={() => doPayment()}
>
{i18n.str`Retry`}
</button>
</div>
) : (
<div>
<ProgressButton
loading={loading}
onClick={() => doPayment()}>
{i18n.str`Confirm payment`}
</ProgressButton>
</div>
)}
</div>
);
}
registerMountPage(() => {
const url = new URI(document.location.href);
const query: any = URI.parseQuery(url.query());
let talerPayUri = query.talerPayUri;
return <TalerPayDialog talerPayUri={talerPayUri} />;
});
|