wallet-core/src/webex/pages/refund.tsx

197 lines
6.3 KiB
TypeScript
Raw Normal View History

2017-08-27 03:56:19 +02:00
/*
This file is part of TALER
(C) 2015-2016 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 that shows refund status for purchases.
*
* @author Florian Dold
*/
import * as React from "react";
import * as ReactDOM from "react-dom";
import URI = require("urijs");
import * as dbTypes from "../../dbTypes";
import { AmountJson } from "../../amounts";
import * as Amounts from "../../amounts";
2017-08-27 03:56:19 +02:00
2018-04-10 21:20:04 +02:00
import * as timer from "../../timer";
2017-08-27 03:56:19 +02:00
import { AmountDisplay } from "../renderHtml";
2017-10-15 19:28:35 +02:00
import * as wxApi from "../wxApi";
2017-08-27 03:56:19 +02:00
interface RefundStatusViewProps {
2018-01-22 01:12:08 +01:00
contractTermsHash?: string;
refundUrl?: string;
2017-08-27 03:56:19 +02:00
}
interface RefundStatusViewState {
2018-01-22 01:12:08 +01:00
contractTermsHash?: string;
purchase?: dbTypes.PurchaseRecord;
refundFees?: AmountJson;
2017-08-27 03:56:19 +02:00
gotResult: boolean;
}
2017-10-15 19:28:35 +02:00
interface RefundDetailProps {
purchase: dbTypes.PurchaseRecord;
2018-04-10 21:20:04 +02:00
/**
* Full refund fees (including refreshing) so far, or undefined if no refund
* permission was processed yet
*/
fullRefundFees?: AmountJson;
2017-10-15 19:28:35 +02:00
}
2017-08-27 03:56:19 +02:00
2017-10-15 19:28:35 +02:00
const RefundDetail = ({purchase, fullRefundFees}: RefundDetailProps) => {
2017-08-27 03:56:19 +02:00
const pendingKeys = Object.keys(purchase.refundsPending);
const doneKeys = Object.keys(purchase.refundsDone);
2017-10-15 19:28:35 +02:00
if (pendingKeys.length === 0 && doneKeys.length === 0) {
2017-08-27 03:56:19 +02:00
return <p>No refunds</p>;
}
2017-10-15 19:28:35 +02:00
const firstRefundKey = [...pendingKeys, ...doneKeys][0];
if (!firstRefundKey) {
return <p>Waiting for refunds ...</p>;
}
const allRefunds = { ...purchase.refundsDone, ...purchase.refundsPending };
const currency = Amounts.parseOrThrow(allRefunds[firstRefundKey].refund_amount).currency;
2017-08-27 03:56:19 +02:00
if (!currency) {
throw Error("invariant");
}
let amountPending = Amounts.getZero(currency);
2017-10-15 19:28:35 +02:00
for (const k of pendingKeys) {
const refundAmount = Amounts.parseOrThrow(purchase.refundsPending[k].refund_amount);
amountPending = Amounts.add(amountPending, refundAmount).amount;
2017-08-27 03:56:19 +02:00
}
let amountDone = Amounts.getZero(currency);
2017-10-15 19:28:35 +02:00
for (const k of doneKeys) {
const refundAmount = Amounts.parseOrThrow(purchase.refundsDone[k].refund_amount);
amountDone = Amounts.add(amountDone, refundAmount).amount;
2017-08-27 03:56:19 +02:00
}
const hasPending = amountPending.fraction !== 0 || amountPending.value !== 0;
2017-08-27 03:56:19 +02:00
return (
<div>
{hasPending ? <p>Refund pending: <AmountDisplay amount={amountPending} /></p> : null}
2017-10-15 19:28:35 +02:00
<p>
2018-04-10 21:20:04 +02:00
Refund received: <AmountDisplay amount={amountDone} />{" "}
(refund fees: {fullRefundFees ? <AmountDisplay amount={fullRefundFees} /> : "??" })
2017-10-15 19:28:35 +02:00
</p>
2017-08-27 03:56:19 +02:00
</div>
);
};
class RefundStatusView extends React.Component<RefundStatusViewProps, RefundStatusViewState> {
constructor(props: RefundStatusViewProps) {
super(props);
this.state = { gotResult: false };
}
componentDidMount() {
this.update();
const port = chrome.runtime.connect();
port.onMessage.addListener((msg: any) => {
if (msg.notify) {
console.log("got notified");
this.update();
}
});
2018-04-10 21:20:04 +02:00
// Just to be safe: update every second, in case we miss a notification
// from the background page.
timer.after(1000, () => this.update());
2017-08-27 03:56:19 +02:00
}
render(): JSX.Element {
2018-01-22 01:12:08 +01:00
if (!this.props.contractTermsHash && !this.props.refundUrl) {
return (
<div id="main">
<span>Error: Neither contract terms hash nor refund url given.</span>
</div>
);
}
2017-08-27 03:56:19 +02:00
const purchase = this.state.purchase;
if (!purchase) {
2018-01-22 01:12:08 +01:00
let message;
2017-08-27 03:56:19 +02:00
if (this.state.gotResult) {
2018-01-22 01:12:08 +01:00
message = <span>No purchase with contract terms hash {this.props.contractTermsHash} found</span>;
2017-08-27 03:56:19 +02:00
} else {
2018-01-22 01:12:08 +01:00
message = <span>...</span>;
2017-08-27 03:56:19 +02:00
}
2018-01-22 01:12:08 +01:00
return <div id="main">{message}</div>;
2017-08-27 03:56:19 +02:00
}
const merchantName = purchase.contractTerms.merchant.name || "(unknown)";
const summary = purchase.contractTerms.summary || purchase.contractTerms.order_id;
return (
<div id="main">
<h1>Refund Status</h1>
2017-10-15 19:28:35 +02:00
<p>
Status of purchase <strong>{summary}</strong> from merchant <strong>{merchantName}</strong>{" "}
(order id {purchase.contractTerms.order_id}).
</p>
<p>Total amount: <AmountDisplay amount={Amounts.parseOrThrow(purchase.contractTerms.amount)} /></p>
2017-10-15 19:28:35 +02:00
{purchase.finished
2018-04-10 21:20:04 +02:00
? <RefundDetail purchase={purchase} fullRefundFees={this.state.refundFees} />
2017-10-15 19:28:35 +02:00
: <p>Purchase not completed.</p>}
2017-08-27 03:56:19 +02:00
</div>
);
}
async update() {
2018-01-22 01:12:08 +01:00
let contractTermsHash = this.state.contractTermsHash;
if (!contractTermsHash) {
const refundUrl = this.props.refundUrl;
if (!refundUrl) {
console.error("neither contractTermsHash nor refundUrl is given");
return;
}
contractTermsHash = await wxApi.acceptRefund(refundUrl);
this.setState({ contractTermsHash });
2018-01-22 01:12:08 +01:00
}
const purchase = await wxApi.getPurchase(contractTermsHash);
2017-08-27 03:56:19 +02:00
console.log("got purchase", purchase);
2018-04-10 21:20:04 +02:00
// We got a result, but it might be undefined if not found in DB.
this.setState({ purchase, gotResult: true });
const refundsDone = Object.keys(purchase.refundsDone).map((x) => purchase.refundsDone[x]);
if (refundsDone.length) {
const refundFees = await wxApi.getFullRefundFees({ refundPermissions: refundsDone });
this.setState({ purchase, gotResult: true, refundFees });
}
2017-08-27 03:56:19 +02:00
}
}
async function main() {
const url = new URI(document.location.href);
const query: any = URI.parseQuery(url.query());
const container = document.getElementById("container");
if (!container) {
console.error("fatal: can't mount component, countainer missing");
return;
}
2018-01-22 01:12:08 +01:00
const contractTermsHash = query.contractTermsHash;
const refundUrl = query.refundUrl;
ReactDOM.render(<RefundStatusView contractTermsHash={contractTermsHash} refundUrl={refundUrl} />, container);
2017-08-27 03:56:19 +02:00
}
document.addEventListener("DOMContentLoaded", () => main());