re-enable tipping support in the WebExtension UI

This commit is contained in:
Florian Dold 2020-11-18 12:44:06 +01:00
parent 636d3cd30c
commit d6409f185d
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
6 changed files with 116 additions and 8 deletions

View File

@ -121,6 +121,7 @@ export async function prepareTip(
accepted: !!tipRecord && !!tipRecord.acceptedTimestamp, accepted: !!tipRecord && !!tipRecord.acceptedTimestamp,
tipAmountRaw: Amounts.stringify(tipRecord.tipAmountRaw), tipAmountRaw: Amounts.stringify(tipRecord.tipAmountRaw),
exchangeBaseUrl: tipRecord.exchangeBaseUrl, exchangeBaseUrl: tipRecord.exchangeBaseUrl,
merchantBaseUrl: tipRecord.merchantBaseUrl,
expirationTimestamp: tipRecord.tipExpiration, expirationTimestamp: tipRecord.tipExpiration,
tipAmountEffective: Amounts.stringify(tipRecord.tipAmountEffective), tipAmountEffective: Amounts.stringify(tipRecord.tipAmountEffective),
walletTipId: tipRecord.walletTipId, walletTipId: tipRecord.walletTipId,

View File

@ -360,9 +360,33 @@ export interface PrepareTipResult {
* Has the tip already been accepted? * Has the tip already been accepted?
*/ */
accepted: boolean; accepted: boolean;
/**
* Amount that the merchant gave.
*/
tipAmountRaw: AmountString; tipAmountRaw: AmountString;
/**
* Amount that arrived at the wallet.
* Might be lower than the raw amount due to fees.
*/
tipAmountEffective: AmountString; tipAmountEffective: AmountString;
/**
* Base URL of the merchant backend giving then tip.
*/
merchantBaseUrl: string;
/**
* Base URL of the exchange that is used to withdraw the tip.
* Determined by the merchant, the wallet/user has no choice here.
*/
exchangeBaseUrl: string; exchangeBaseUrl: string;
/**
* Time when the tip will expire. After it expired, it can't be picked
* up anymore.
*/
expirationTimestamp: Timestamp; expirationTimestamp: Timestamp;
} }
@ -372,6 +396,7 @@ export const codecForPrepareTipResult = (): Codec<PrepareTipResult> =>
.property("tipAmountRaw", codecForAmountString()) .property("tipAmountRaw", codecForAmountString())
.property("tipAmountEffective", codecForAmountString()) .property("tipAmountEffective", codecForAmountString())
.property("exchangeBaseUrl", codecForString()) .property("exchangeBaseUrl", codecForString())
.property("merchantBaseUrl", codecForString())
.property("expirationTimestamp", codecForTimestamp) .property("expirationTimestamp", codecForTimestamp)
.property("walletTipId", codecForString()) .property("walletTipId", codecForString())
.build("PrepareTipResult"); .build("PrepareTipResult");

View File

@ -27,6 +27,7 @@ import { createWelcomePage } from "./pages/welcome";
import { createPayPage } from "./pages/pay"; import { createPayPage } from "./pages/pay";
import { createRefundPage } from "./pages/refund"; import { createRefundPage } from "./pages/refund";
import { setupI18n } from "taler-wallet-core"; import { setupI18n } from "taler-wallet-core";
import { createTipPage } from './pages/tip';
function main(): void { function main(): void {
try { try {
@ -52,6 +53,9 @@ function main(): void {
case "refund.html": case "refund.html":
mainElement = createRefundPage(); mainElement = createRefundPage();
break; break;
case "tip.html":
mainElement = createTipPage();
break;
default: default:
throw Error(`page '${page}' not implemented`); throw Error(`page '${page}' not implemented`);
} }

View File

@ -15,14 +15,81 @@
*/ */
/** /**
* Page shown to the user to confirm creation * Page shown to the user to accept or ignore a tip from a merchant.
* of a reserve, usually requested by the bank.
* *
* @author Florian Dold * @author Florian Dold <dold@taler.net>
*/ */
import * as React from "react"; import * as React from "react";
import { useEffect, useState } from "react";
import { PrepareTipResult } from "taler-wallet-core";
import { AmountView } from "../renderHtml";
import * as wxApi from "../wxApi";
function TalerTipDialog({ talerTipUri }: { talerTipUri: string }): JSX.Element {
const [updateCounter, setUpdateCounter] = useState<number>(0);
const [prepareTipResult, setPrepareTipResult] = useState<
PrepareTipResult | undefined
>();
const [tipIgnored, setTipIgnored] = useState(false);
useEffect(() => {
const doFetch = async (): Promise<void> => {
const p = await wxApi.prepareTip({ talerTipUri });
setPrepareTipResult(p);
};
doFetch();
}, [talerTipUri, updateCounter]);
const doAccept = async () => {
if (!prepareTipResult) {
return;
}
await wxApi.acceptTip({ walletTipId: prepareTipResult?.walletTipId });
setUpdateCounter(updateCounter + 1);
};
const doIgnore = () => {
setTipIgnored(true);
};
if (tipIgnored) {
return <span>You've ignored the tip.</span>;
}
if (!prepareTipResult) {
return <span>Loading ...</span>;
}
if (prepareTipResult.accepted) {
return (
<span>
Tip from <code>{prepareTipResult.merchantBaseUrl}</code> accepted.
Check your transactions list for more details.
</span>
);
} else {
return (
<div>
<p>
The merchant <code>{prepareTipResult.merchantBaseUrl}</code> is
offering you a tip of{" "}
<strong><AmountView amount={prepareTipResult.tipAmountEffective} /></strong> via the
exchange <code>{prepareTipResult.exchangeBaseUrl}</code>
</p>
<button onClick={doAccept}>Accept tip</button>
<button onClick={doIgnore}>Ignore</button>
</div>
);
}
}
export function createTipPage(): JSX.Element { export function createTipPage(): JSX.Element {
return <span>not implemented</span>; const url = new URL(document.location.href);
const talerTipUri = url.searchParams.get("talerTipUri");
if (!talerTipUri) {
throw Error("invalid parameter");
}
return <TalerTipDialog talerTipUri={talerTipUri} />;
} }

View File

@ -33,6 +33,9 @@ import {
WithdrawUriInfoResponse, WithdrawUriInfoResponse,
TransactionsResponse, TransactionsResponse,
ApplyRefundResponse, ApplyRefundResponse,
PrepareTipRequest,
PrepareTipResult,
AcceptTipRequest,
} from "taler-wallet-core"; } from "taler-wallet-core";
export interface ExtendedPermissionsResponse { export interface ExtendedPermissionsResponse {
@ -188,6 +191,14 @@ export function getWithdrawalDetailsForUri(
return callBackend("getWithdrawalDetailsForUri", req); return callBackend("getWithdrawalDetailsForUri", req);
} }
export function prepareTip(req: PrepareTipRequest): Promise<PrepareTipResult> {
return callBackend("prepareTip", req);
}
export function acceptTip(req: AcceptTipRequest): Promise<void> {
return callBackend("acceptTip", req);
}
export function onUpdateNotification(f: () => void): () => void { export function onUpdateNotification(f: () => void): () => void {
const port = chrome.runtime.connect({ name: "notifications" }); const port = chrome.runtime.connect({ name: "notifications" });
const listener = (): void => { const listener = (): void => {

View File

@ -4,10 +4,10 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<title>Taler Wallet: Received Tip</title> <title>Taler Wallet: Received Tip</title>
<link rel="icon" href="/img/icon.png" /> <link rel="icon" href="/static/img/icon.png" />
<link rel="stylesheet" type="text/css" href="/style/pure.css" /> <link rel="stylesheet" type="text/css" href="/static/style/pure.css" />
<link rel="stylesheet" type="text/css" href="/style/wallet.css" /> <link rel="stylesheet" type="text/css" href="/static/style/wallet.css" />
<script src="/pageEntryPoint.js"></script> <script src="/dist/pageEntryPoint.js"></script>
</head> </head>
<body> <body>