refund view
This commit is contained in:
parent
c3f47e8f58
commit
c35b3be795
@ -103,6 +103,14 @@ export function getZero(currency: string): AmountJson {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function sum(amounts: AmountJson[]) {
|
||||||
|
if (amounts.length <= 0) {
|
||||||
|
throw Error("can't sum zero amounts");
|
||||||
|
}
|
||||||
|
return add(amounts[0], ...amounts.slice(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add two amounts. Return the result and whether
|
* Add two amounts. Return the result and whether
|
||||||
* the addition overflowed. The overflow is always handled
|
* the addition overflowed. The overflow is always handled
|
||||||
|
@ -107,9 +107,15 @@ import {
|
|||||||
DownloadedWithdrawInfo,
|
DownloadedWithdrawInfo,
|
||||||
WithdrawDetails,
|
WithdrawDetails,
|
||||||
AcceptWithdrawalResponse,
|
AcceptWithdrawalResponse,
|
||||||
|
PurchaseDetails,
|
||||||
} from "./walletTypes";
|
} from "./walletTypes";
|
||||||
import { openPromise } from "./promiseUtils";
|
import { openPromise } from "./promiseUtils";
|
||||||
import { parsePayUri, parseWithdrawUri, parseTipUri, parseRefundUri } from "./taleruri";
|
import {
|
||||||
|
parsePayUri,
|
||||||
|
parseWithdrawUri,
|
||||||
|
parseTipUri,
|
||||||
|
parseRefundUri,
|
||||||
|
} from "./taleruri";
|
||||||
|
|
||||||
interface SpeculativePayData {
|
interface SpeculativePayData {
|
||||||
payCoinInfo: PayCoinInfo;
|
payCoinInfo: PayCoinInfo;
|
||||||
@ -3462,7 +3468,10 @@ export class Wallet {
|
|||||||
timestamp: new Date().getTime(),
|
timestamp: new Date().getTime(),
|
||||||
tipId: res.tipId,
|
tipId: res.tipId,
|
||||||
pickupUrl: res.tipPickupUrl,
|
pickupUrl: res.tipPickupUrl,
|
||||||
totalFees: Amounts.add(withdrawDetails.overhead, withdrawDetails.withdrawFee).amount,
|
totalFees: Amounts.add(
|
||||||
|
withdrawDetails.overhead,
|
||||||
|
withdrawDetails.withdrawFee,
|
||||||
|
).amount,
|
||||||
};
|
};
|
||||||
await this.q().put(Stores.tips, tipRecord);
|
await this.q().put(Stores.tips, tipRecord);
|
||||||
}
|
}
|
||||||
@ -3585,6 +3594,40 @@ export class Wallet {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getPurchaseDetails(hc: string): Promise<PurchaseDetails> {
|
||||||
|
const purchase = await this.q().get(Stores.purchases, hc);
|
||||||
|
if (!purchase) {
|
||||||
|
throw Error("unknown purchase");
|
||||||
|
}
|
||||||
|
const refundsDoneAmounts = Object.values(purchase.refundsDone).map(x =>
|
||||||
|
Amounts.parseOrThrow(x.refund_amount),
|
||||||
|
);
|
||||||
|
const refundsPendingAmounts = Object.values(purchase.refundsPending).map(
|
||||||
|
x => Amounts.parseOrThrow(x.refund_amount),
|
||||||
|
);
|
||||||
|
const totalRefundAmount = Amounts.sum([
|
||||||
|
...refundsDoneAmounts,
|
||||||
|
...refundsPendingAmounts,
|
||||||
|
]).amount;
|
||||||
|
const refundsDoneFees = Object.values(purchase.refundsDone).map(x =>
|
||||||
|
Amounts.parseOrThrow(x.refund_amount),
|
||||||
|
);
|
||||||
|
const refundsPendingFees = Object.values(purchase.refundsPending).map(
|
||||||
|
x => Amounts.parseOrThrow(x.refund_amount),
|
||||||
|
);
|
||||||
|
const totalRefundFees = Amounts.sum([
|
||||||
|
...refundsDoneFees,
|
||||||
|
...refundsPendingFees,
|
||||||
|
]).amount;
|
||||||
|
const totalFees = totalRefundFees;
|
||||||
|
return {
|
||||||
|
contractTerms: purchase.contractTerms,
|
||||||
|
hasRefund: purchase.timestamp_refund !== 0,
|
||||||
|
totalRefundAmount: totalRefundAmount,
|
||||||
|
totalRefundAndRefreshFees: totalFees,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset the retry timeouts for ongoing operations.
|
* Reset the retry timeouts for ongoing operations.
|
||||||
*/
|
*/
|
||||||
|
@ -502,3 +502,13 @@ export interface AcceptWithdrawalResponse {
|
|||||||
reservePub: string;
|
reservePub: string;
|
||||||
confirmTransferUrl?: string;
|
confirmTransferUrl?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Details about a purchase, including refund status.
|
||||||
|
*/
|
||||||
|
export interface PurchaseDetails {
|
||||||
|
contractTerms: ContractTerms,
|
||||||
|
hasRefund: boolean,
|
||||||
|
totalRefundAmount: AmountJson,
|
||||||
|
totalRefundAndRefreshFees: AmountJson,
|
||||||
|
}
|
@ -157,9 +157,9 @@ export interface MessageMap {
|
|||||||
request: { reportUid: string };
|
request: { reportUid: string };
|
||||||
response: void;
|
response: void;
|
||||||
};
|
};
|
||||||
"get-purchase": {
|
"get-purchase-details": {
|
||||||
request: { contractTermsHash: string };
|
request: { contractTermsHash: string };
|
||||||
response: dbTypes.PurchaseRecord;
|
response: walletTypes.PurchaseDetails;
|
||||||
};
|
};
|
||||||
"accept-tip": {
|
"accept-tip": {
|
||||||
request: { talerTipUri: string };
|
request: { talerTipUri: string };
|
||||||
|
@ -13,6 +13,9 @@
|
|||||||
<script src="/dist/refund-bundle.js"></script>
|
<script src="/dist/refund-bundle.js"></script>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div id="container"></div>
|
<section id="main">
|
||||||
|
<h1>GNU Taler Wallet</h1>
|
||||||
|
<article id="container" class="fade"></article>
|
||||||
|
</section>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -14,177 +14,74 @@
|
|||||||
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Page that shows refund status for purchases.
|
* Page that shows refund status for purchases.
|
||||||
*
|
*
|
||||||
* @author Florian Dold
|
* @author Florian Dold
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import React, { useEffect, useState } from "react";
|
||||||
import * as React from "react";
|
import ReactDOM from "react-dom";
|
||||||
import * as ReactDOM from "react-dom";
|
|
||||||
import URI = require("urijs");
|
import URI = require("urijs");
|
||||||
|
|
||||||
import * as dbTypes from "../../dbTypes";
|
|
||||||
|
|
||||||
import { AmountJson } from "../../amounts";
|
|
||||||
import * as Amounts from "../../amounts";
|
|
||||||
|
|
||||||
import * as timer from "../../timer";
|
|
||||||
|
|
||||||
import { AmountDisplay } from "../renderHtml";
|
|
||||||
import * as wxApi from "../wxApi";
|
import * as wxApi from "../wxApi";
|
||||||
|
import { PurchaseDetails } from "../../walletTypes";
|
||||||
|
import { AmountView } from "../renderHtml";
|
||||||
|
|
||||||
interface RefundStatusViewProps {
|
function RefundStatusView(props: { talerRefundUri: string }) {
|
||||||
contractTermsHash?: string;
|
const [applied, setApplied] = useState(false);
|
||||||
refundUrl?: string;
|
const [purchaseDetails, setPurchaseDetails] = useState<
|
||||||
}
|
PurchaseDetails | undefined
|
||||||
|
>(undefined);
|
||||||
|
const [errMsg, setErrMsg] = useState<string | undefined>(undefined);
|
||||||
|
|
||||||
interface RefundStatusViewState {
|
useEffect(() => {
|
||||||
contractTermsHash?: string;
|
const doFetch = async () => {
|
||||||
purchase?: dbTypes.PurchaseRecord;
|
try {
|
||||||
refundFees?: AmountJson;
|
const hc = await wxApi.applyRefund(props.talerRefundUri);
|
||||||
gotResult: boolean;
|
setApplied(true);
|
||||||
|
const r = await wxApi.getPurchaseDetails(hc);
|
||||||
|
setPurchaseDetails(r);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setErrMsg(e.message);
|
||||||
|
console.log("err message", e.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RefundDetailProps {
|
|
||||||
purchase: dbTypes.PurchaseRecord;
|
|
||||||
/**
|
|
||||||
* Full refund fees (including refreshing) so far, or undefined if no refund
|
|
||||||
* permission was processed yet
|
|
||||||
*/
|
|
||||||
fullRefundFees?: AmountJson;
|
|
||||||
}
|
|
||||||
|
|
||||||
const RefundDetail = ({purchase, fullRefundFees}: RefundDetailProps) => {
|
|
||||||
const pendingKeys = Object.keys(purchase.refundsPending);
|
|
||||||
const doneKeys = Object.keys(purchase.refundsDone);
|
|
||||||
if (pendingKeys.length === 0 && doneKeys.length === 0) {
|
|
||||||
return <p>No refunds</p>;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
if (!currency) {
|
|
||||||
throw Error("invariant");
|
|
||||||
}
|
|
||||||
|
|
||||||
let amountPending = Amounts.getZero(currency);
|
|
||||||
for (const k of pendingKeys) {
|
|
||||||
const refundAmount = Amounts.parseOrThrow(purchase.refundsPending[k].refund_amount);
|
|
||||||
amountPending = Amounts.add(amountPending, refundAmount).amount;
|
|
||||||
}
|
|
||||||
let amountDone = Amounts.getZero(currency);
|
|
||||||
for (const k of doneKeys) {
|
|
||||||
const refundAmount = Amounts.parseOrThrow(purchase.refundsDone[k].refund_amount);
|
|
||||||
amountDone = Amounts.add(amountDone, refundAmount).amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
const hasPending = amountPending.fraction !== 0 || amountPending.value !== 0;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
{hasPending ? <p>Refund pending: <AmountDisplay amount={amountPending} /></p> : null}
|
|
||||||
<p>
|
|
||||||
Refund received: <AmountDisplay amount={amountDone} />{" "}
|
|
||||||
(refund fees: {fullRefundFees ? <AmountDisplay amount={fullRefundFees} /> : "??" })
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
doFetch();
|
||||||
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();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
// Just to be safe: update every second, in case we miss a notification
|
|
||||||
// from the background page.
|
console.log("rendering");
|
||||||
timer.after(1000, () => this.update());
|
|
||||||
|
if (errMsg) {
|
||||||
|
return <span>Error: {errMsg}</span>;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!applied || !purchaseDetails) {
|
||||||
|
return <span>Updating refund status</span>;
|
||||||
}
|
}
|
||||||
|
|
||||||
render(): JSX.Element {
|
|
||||||
if (!this.props.contractTermsHash && !this.props.refundUrl) {
|
|
||||||
return (
|
return (
|
||||||
<div id="main">
|
<>
|
||||||
<span>Error: Neither contract terms hash nor refund url given.</span>
|
<h2>Refund Status</h2>
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const purchase = this.state.purchase;
|
|
||||||
if (!purchase) {
|
|
||||||
let message;
|
|
||||||
if (this.state.gotResult) {
|
|
||||||
message = <span>No purchase with contract terms hash {this.props.contractTermsHash} found</span>;
|
|
||||||
} else {
|
|
||||||
message = <span>...</span>;
|
|
||||||
}
|
|
||||||
return <div id="main">{message}</div>;
|
|
||||||
}
|
|
||||||
const merchantName = purchase.contractTerms.merchant.name || "(unknown)";
|
|
||||||
const summary = purchase.contractTerms.summary || purchase.contractTerms.order_id;
|
|
||||||
return (
|
|
||||||
<div id="main">
|
|
||||||
<h1>Refund Status</h1>
|
|
||||||
<p>
|
<p>
|
||||||
Status of purchase <strong>{summary}</strong> from merchant <strong>{merchantName}</strong>{" "}
|
The product <em>{purchaseDetails.contractTerms.summary!}</em> has
|
||||||
(order id {purchase.contractTerms.order_id}).
|
received a total refund of <AmountView amount={purchaseDetails.totalRefundAmount} />.
|
||||||
</p>
|
</p>
|
||||||
<p>Total amount: <AmountDisplay amount={Amounts.parseOrThrow(purchase.contractTerms.amount)} /></p>
|
<p>
|
||||||
{purchase.finished
|
Note that additional fees from the exchange may apply.
|
||||||
? <RefundDetail purchase={purchase} fullRefundFees={this.state.refundFees} />
|
</p>
|
||||||
: <p>Purchase not completed.</p>}
|
</>
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async update() {
|
|
||||||
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 });
|
|
||||||
}
|
|
||||||
const purchase = await wxApi.getPurchase(contractTermsHash);
|
|
||||||
console.log("got purchase", purchase);
|
|
||||||
// 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 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const url = new URI(document.location.href);
|
const url = new URI(document.location.href);
|
||||||
const query: any = URI.parseQuery(url.query());
|
const query: any = URI.parseQuery(url.query());
|
||||||
|
|
||||||
const container = document.getElementById("container");
|
const container = document.getElementById("container");
|
||||||
if (!container) {
|
if (!container) {
|
||||||
console.error("fatal: can't mount component, countainer missing");
|
console.error("fatal: can't mount component, container missing");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,7 +91,10 @@ async function main() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactDOM.render(<RefundStatusView contractTermsHash={contractTermsHash} refundUrl={refundUrl} />, container);
|
ReactDOM.render(
|
||||||
|
<RefundStatusView talerRefundUri={talerRefundUri} />,
|
||||||
|
container,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", () => main());
|
document.addEventListener("DOMContentLoaded", () => main());
|
||||||
|
@ -62,7 +62,7 @@ export function renderAmount(amount: AmountJson | string) {
|
|||||||
return <span>{x} {a.currency}</span>;
|
return <span>{x} {a.currency}</span>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AmountDisplay = ({amount}: {amount: AmountJson | string}) => renderAmount(amount);
|
export const AmountView = ({amount}: {amount: AmountJson | string}) => renderAmount(amount);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,6 +41,7 @@ import {
|
|||||||
SenderWireInfos,
|
SenderWireInfos,
|
||||||
TipStatus,
|
TipStatus,
|
||||||
WalletBalance,
|
WalletBalance,
|
||||||
|
PurchaseDetails,
|
||||||
} from "../walletTypes";
|
} from "../walletTypes";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -91,7 +92,7 @@ async function callBackend<T extends MessageType>(
|
|||||||
return new Promise<MessageMap[T]["response"]>((resolve, reject) => {
|
return new Promise<MessageMap[T]["response"]>((resolve, reject) => {
|
||||||
chrome.runtime.sendMessage({ type, detail }, (resp) => {
|
chrome.runtime.sendMessage({ type, detail }, (resp) => {
|
||||||
if (typeof resp === "object" && resp && resp.error) {
|
if (typeof resp === "object" && resp && resp.error) {
|
||||||
const e = new WalletApiError(resp.message, resp);
|
const e = new WalletApiError(resp.error.message, resp.error);
|
||||||
reject(e);
|
reject(e);
|
||||||
} else {
|
} else {
|
||||||
resolve(resp);
|
resolve(resp);
|
||||||
@ -318,8 +319,8 @@ export function getReport(reportUid: string): Promise<any> {
|
|||||||
* Look up a purchase in the wallet database from
|
* Look up a purchase in the wallet database from
|
||||||
* the contract terms hash.
|
* the contract terms hash.
|
||||||
*/
|
*/
|
||||||
export function getPurchase(contractTermsHash: string): Promise<PurchaseRecord> {
|
export function getPurchaseDetails(contractTermsHash: string): Promise<PurchaseDetails> {
|
||||||
return callBackend("get-purchase", { contractTermsHash });
|
return callBackend("get-purchase-details", { contractTermsHash });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -356,7 +357,7 @@ export function downloadProposal(url: string): Promise<number> {
|
|||||||
/**
|
/**
|
||||||
* Download a refund and accept it.
|
* Download a refund and accept it.
|
||||||
*/
|
*/
|
||||||
export function acceptRefund(refundUrl: string): Promise<string> {
|
export function applyRefund(refundUrl: string): Promise<string> {
|
||||||
return callBackend("accept-refund", { refundUrl });
|
return callBackend("accept-refund", { refundUrl });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +62,12 @@ function handleMessage(
|
|||||||
function assertNotFound(t: never): never {
|
function assertNotFound(t: never): never {
|
||||||
console.error(`Request type ${t as string} unknown`);
|
console.error(`Request type ${t as string} unknown`);
|
||||||
console.error(`Request detail was ${detail}`);
|
console.error(`Request detail was ${detail}`);
|
||||||
return { error: "request unknown", requestType: type } as never;
|
return {
|
||||||
|
error: {
|
||||||
|
message: `request type ${t as string} unknown`,
|
||||||
|
requestType: type,
|
||||||
|
},
|
||||||
|
} as never;
|
||||||
}
|
}
|
||||||
function needsWallet(): Wallet {
|
function needsWallet(): Wallet {
|
||||||
if (!currentWallet) {
|
if (!currentWallet) {
|
||||||
@ -264,12 +269,12 @@ function handleMessage(
|
|||||||
return;
|
return;
|
||||||
case "get-report":
|
case "get-report":
|
||||||
return logging.getReport(detail.reportUid);
|
return logging.getReport(detail.reportUid);
|
||||||
case "get-purchase": {
|
case "get-purchase-details": {
|
||||||
const contractTermsHash = detail.contractTermsHash;
|
const contractTermsHash = detail.contractTermsHash;
|
||||||
if (!contractTermsHash) {
|
if (!contractTermsHash) {
|
||||||
throw Error("contractTermsHash missing");
|
throw Error("contractTermsHash missing");
|
||||||
}
|
}
|
||||||
return needsWallet().getPurchase(contractTermsHash);
|
return needsWallet().getPurchaseDetails(contractTermsHash);
|
||||||
}
|
}
|
||||||
case "accept-refund":
|
case "accept-refund":
|
||||||
return needsWallet().applyRefund(detail.refundUrl);
|
return needsWallet().applyRefund(detail.refundUrl);
|
||||||
@ -343,9 +348,10 @@ async function dispatch(
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
sendResponse({
|
sendResponse({
|
||||||
error: "exception",
|
error: {
|
||||||
message: e.message,
|
message: e.message,
|
||||||
stack,
|
stack,
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
Loading…
Reference in New Issue
Block a user