From 35c83414f9e0af11012c1a2e00672d2a7c1856ae Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Fri, 15 May 2020 12:53:35 +0530 Subject: [PATCH] fix issues in the webextension refund UI --- src/operations/refund.ts | 12 ++++++------ src/wallet.ts | 33 +++++++++++++++++++-------------- src/webex/messages.ts | 6 +++--- src/webex/pageEntryPoint.ts | 4 ++++ src/webex/pages/refund.tsx | 4 ++-- src/webex/wxApi.ts | 15 +++++++++------ src/webex/wxBackend.ts | 11 +++++++---- 7 files changed, 50 insertions(+), 35 deletions(-) diff --git a/src/operations/refund.ts b/src/operations/refund.ts index 1ffcd2da2..cbcb96446 100644 --- a/src/operations/refund.ts +++ b/src/operations/refund.ts @@ -252,10 +252,7 @@ async function acceptRefundResponse( let queryDone = true; if (numNewRefunds === 0) { - if ( - p.autoRefundDeadline && - p.autoRefundDeadline.t_ms > now.t_ms - ) { + if (p.autoRefundDeadline && p.autoRefundDeadline.t_ms > now.t_ms) { queryDone = false; } } else { @@ -361,7 +358,7 @@ async function startRefundQuery( export async function applyRefund( ws: InternalWalletState, talerRefundUri: string, -): Promise<{ contractTermsHash: string }> { +): Promise<{ contractTermsHash: string; proposalId: string }> { const parseResult = parseRefundUri(talerRefundUri); console.log("applying refund", parseResult); @@ -384,7 +381,10 @@ export async function applyRefund( logger.info("processing purchase for refund"); await startRefundQuery(ws, purchase.proposalId); - return { contractTermsHash: purchase.contractData.contractTermsHash }; + return { + contractTermsHash: purchase.contractData.contractTermsHash, + proposalId: purchase.proposalId, + }; } export async function processPurchaseQueryRefund( diff --git a/src/wallet.ts b/src/wallet.ts index 2d63e2298..31f9af8c2 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -112,7 +112,10 @@ import { import { durationMin, Duration } from "./util/time"; import { processRecoupGroup } from "./operations/recoup"; import { OperationFailedAndReportedError } from "./operations/errors"; -import { TransactionsRequest, TransactionsResponse } from "./types/transactions"; +import { + TransactionsRequest, + TransactionsResponse, +} from "./types/transactions"; import { getTransactions } from "./operations/transactions"; const builtinCurrencies: CurrencyRecord[] = [ @@ -631,7 +634,7 @@ export class Wallet { */ async applyRefund( talerRefundUri: string, - ): Promise<{ contractTermsHash: string }> { + ): Promise<{ contractTermsHash: string; proposalId: string }> { return applyRefund(this.ws, talerRefundUri); } @@ -719,14 +722,14 @@ export class Wallet { return refuseProposal(this.ws, proposalId); } - async getPurchaseDetails(hc: string): Promise { - const purchase = await this.db.get(Stores.purchases, hc); + async getPurchaseDetails(proposalId: string): Promise { + const purchase = await this.db.get(Stores.purchases, proposalId); if (!purchase) { throw Error("unknown purchase"); } - const refundsDoneAmounts = Object.values( - purchase.refundsDone, - ).map((x) => Amounts.parseOrThrow(x.perm.refund_amount)); + const refundsDoneAmounts = Object.values(purchase.refundsDone).map((x) => + Amounts.parseOrThrow(x.perm.refund_amount), + ); const refundsPendingAmounts = Object.values( purchase.refundsPending, ).map((x) => Amounts.parseOrThrow(x.perm.refund_amount)); @@ -734,12 +737,12 @@ export class Wallet { ...refundsDoneAmounts, ...refundsPendingAmounts, ]).amount; - const refundsDoneFees = Object.values( - purchase.refundsDone, - ).map((x) => Amounts.parseOrThrow(x.perm.refund_amount)); - const refundsPendingFees = Object.values( - purchase.refundsPending, - ).map((x) => Amounts.parseOrThrow(x.perm.refund_amount)); + const refundsDoneFees = Object.values(purchase.refundsDone).map((x) => + Amounts.parseOrThrow(x.perm.refund_amount), + ); + const refundsPendingFees = Object.values(purchase.refundsPending).map((x) => + Amounts.parseOrThrow(x.perm.refund_amount), + ); const totalRefundFees = Amounts.sum([ ...refundsDoneFees, ...refundsPendingFees, @@ -818,7 +821,9 @@ export class Wallet { return coinsJson; } - async getTransactions(request: TransactionsRequest): Promise { + async getTransactions( + request: TransactionsRequest, + ): Promise { return getTransactions(this.ws, request); } } diff --git a/src/webex/messages.ts b/src/webex/messages.ts index 179eec88a..932636f1a 100644 --- a/src/webex/messages.ts +++ b/src/webex/messages.ts @@ -122,7 +122,7 @@ export interface MessageMap { response: void; }; "get-purchase-details": { - request: { contractTermsHash: string }; + request: { proposalId: string }; response: walletTypes.PurchaseDetails; }; "accept-tip": { @@ -135,7 +135,7 @@ export interface MessageMap { }; "accept-refund": { request: { refundUrl: string }; - response: string; + response: { contractTermsHash: string; proposalId: string }; }; "abort-failed-payment": { request: { contractTermsHash: string }; @@ -169,7 +169,7 @@ export interface MessageMap { response: walletTypes.ExtendedPermissionsResponse; }; "get-extended-permissions": { - request: { }; + request: {}; response: walletTypes.ExtendedPermissionsResponse; }; } diff --git a/src/webex/pageEntryPoint.ts b/src/webex/pageEntryPoint.ts index b9bdba06e..9fd1d36f1 100644 --- a/src/webex/pageEntryPoint.ts +++ b/src/webex/pageEntryPoint.ts @@ -25,6 +25,7 @@ import { createPopup } from "./pages/popup"; import { createWithdrawPage } from "./pages/withdraw"; import { createWelcomePage } from "./pages/welcome"; import { createPayPage } from "./pages/pay"; +import { createRefundPage } from "./pages/refund"; function main(): void { try { @@ -47,6 +48,9 @@ function main(): void { case "pay.html": mainElement = createPayPage(); break; + case "refund.html": + mainElement = createRefundPage(); + break; default: throw Error(`page '${page}' not implemented`); } diff --git a/src/webex/pages/refund.tsx b/src/webex/pages/refund.tsx index 621a286bb..c5d6a00df 100644 --- a/src/webex/pages/refund.tsx +++ b/src/webex/pages/refund.tsx @@ -36,9 +36,9 @@ function RefundStatusView(props: { talerRefundUri: string }): JSX.Element { useEffect(() => { const doFetch = async (): Promise => { try { - const hc = await wxApi.applyRefund(props.talerRefundUri); + const result = await wxApi.applyRefund(props.talerRefundUri); setApplied(true); - const r = await wxApi.getPurchaseDetails(hc); + const r = await wxApi.getPurchaseDetails(result.proposalId); setPurchaseDetails(r); } catch (e) { console.error(e); diff --git a/src/webex/wxApi.ts b/src/webex/wxApi.ts index 0bd444856..1823f662c 100644 --- a/src/webex/wxApi.ts +++ b/src/webex/wxApi.ts @@ -246,9 +246,9 @@ export function returnCoins(args: { * the contract terms hash. */ export function getPurchaseDetails( - contractTermsHash: string, + proposalId: string, ): Promise { - return callBackend("get-purchase-details", { contractTermsHash }); + return callBackend("get-purchase-details", { proposalId }); } /** @@ -268,7 +268,9 @@ export function acceptTip(talerTipUri: string): Promise { /** * Download a refund and accept it. */ -export function applyRefund(refundUrl: string): Promise { +export function applyRefund( + refundUrl: string, +): Promise<{ contractTermsHash: string; proposalId: string }> { return callBackend("accept-refund", { refundUrl }); } @@ -329,7 +331,9 @@ export function getDiagnostics(): Promise { /** * Get diagnostics information */ -export function setExtendedPermissions(value: boolean): Promise { +export function setExtendedPermissions( + value: boolean, +): Promise { return callBackend("set-extended-permissions", { value }); } @@ -340,7 +344,6 @@ export function getExtendedPermissions(): Promise { return callBackend("get-extended-permissions", {}); } - export function onUpdateNotification(f: () => void): () => void { const port = chrome.runtime.connect({ name: "notifications" }); const listener = (): void => { @@ -350,4 +353,4 @@ export function onUpdateNotification(f: () => void): () => void { return () => { port.onMessage.removeListener(listener); }; -} \ No newline at end of file +} diff --git a/src/webex/wxBackend.ts b/src/webex/wxBackend.ts index f56cba176..0e8b53070 100644 --- a/src/webex/wxBackend.ts +++ b/src/webex/wxBackend.ts @@ -212,11 +212,14 @@ async function handleMessage( return resp; } case "get-purchase-details": { - const contractTermsHash = detail.contractTermsHash; - if (!contractTermsHash) { - throw Error("contractTermsHash missing"); + const proposalId = detail.proposalId; + if (!proposalId) { + throw Error("proposalId missing"); } - return needsWallet().getPurchaseDetails(contractTermsHash); + if (typeof proposalId !== "string") { + throw Error("proposalId must be a string"); + } + return needsWallet().getPurchaseDetails(proposalId); } case "accept-refund": return needsWallet().applyRefund(detail.refundUrl);