From c8553f3bc53064d157b56220e9d2333b8931e7a5 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 29 Jul 2020 23:10:41 +0530 Subject: [PATCH] tweaks to pay API --- src/headless/taler-wallet-cli.ts | 7 ++++--- src/operations/pay.ts | 20 ++++++++++---------- src/types/walletTypes.ts | 9 +++++---- src/webex/pages/pay.tsx | 12 ++++++------ 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/headless/taler-wallet-cli.ts b/src/headless/taler-wallet-cli.ts index ed6146a6b..ba629537e 100644 --- a/src/headless/taler-wallet-cli.ts +++ b/src/headless/taler-wallet-cli.ts @@ -60,7 +60,7 @@ async function doPay( ): Promise { const result = await wallet.preparePayForUri(payUrl); if (result.status === PreparePayResultType.InsufficientBalance) { - console.log("contract", result.contractTermsRaw); + console.log("contract", result.contractTerms); console.error("insufficient balance"); process.exit(1); return; @@ -80,8 +80,9 @@ async function doPay( } else { throw Error("not reached"); } - console.log("contract", result.contractTermsRaw); - console.log("total fees:", Amounts.stringify(result.totalFees)); + console.log("contract", result.contractTerms); + console.log("raw amount:", result.amountRaw); + console.log("effective amount:", result.amountEffective); let pay; if (options.alwaysYes) { pay = true; diff --git a/src/operations/pay.ts b/src/operations/pay.ts index 08bbe43a1..c2877da64 100644 --- a/src/operations/pay.ts +++ b/src/operations/pay.ts @@ -915,26 +915,26 @@ export async function preparePayForUri( console.log("not confirming payment, insufficient coins"); return { status: PreparePayResultType.InsufficientBalance, - contractTermsRaw: d.contractTermsRaw, + contractTerms: d.contractTermsRaw, proposalId: proposal.proposalId, }; } const costInfo = await getTotalPaymentCost(ws, res); - console.log("costInfo", costInfo); - console.log("coinsForPayment", res); - const totalFees = Amounts.sub(costInfo.totalCost, res.paymentAmount).amount; + logger.trace("costInfo", costInfo); + logger.trace("coinsForPayment", res); return { status: PreparePayResultType.PaymentPossible, - contractTermsRaw: d.contractTermsRaw, + contractTerms: d.contractTermsRaw, proposalId: proposal.proposalId, - totalFees, + amountEffective: Amounts.stringify(costInfo.totalCost), + amountRaw: Amounts.stringify(res.paymentAmount), }; } if (purchase.lastSessionId !== uriResult.sessionId) { - console.log( + logger.trace( "automatically re-submitting payment with different session ID", ); await ws.db.runWithWriteTransaction([Stores.purchases], async (tx) => { @@ -948,20 +948,20 @@ export async function preparePayForUri( const r = await submitPay(ws, proposalId); return { status: PreparePayResultType.AlreadyConfirmed, - contractTermsRaw: purchase.contractTermsRaw, + contractTerms: purchase.contractTermsRaw, paid: true, nextUrl: r.nextUrl, }; } else if (!purchase.timestampFirstSuccessfulPay) { return { status: PreparePayResultType.AlreadyConfirmed, - contractTermsRaw: purchase.contractTermsRaw, + contractTerms: purchase.contractTermsRaw, paid: false, }; } else if (purchase.paymentSubmitPending) { return { status: PreparePayResultType.AlreadyConfirmed, - contractTermsRaw: purchase.contractTermsRaw, + contractTerms: purchase.contractTermsRaw, paid: false, }; } diff --git a/src/types/walletTypes.ts b/src/types/walletTypes.ts index a6bc26d88..9f0bfaf37 100644 --- a/src/types/walletTypes.ts +++ b/src/types/walletTypes.ts @@ -329,19 +329,20 @@ export type PreparePayResult = export interface PreparePayResultPaymentPossible { status: PreparePayResultType.PaymentPossible; proposalId: string; - contractTermsRaw: string; - totalFees: AmountJson; + contractTerms: string; + amountRaw: string; + amountEffective: string; } export interface PreparePayResultInsufficientBalance { status: PreparePayResultType.InsufficientBalance; proposalId: string; - contractTermsRaw: any; + contractTerms: any; } export interface PreparePayResultAlreadyConfirmed { status: PreparePayResultType.AlreadyConfirmed; - contractTermsRaw: any; + contractTerms: any; paid: boolean; // Only specified if paid. nextUrl?: string; diff --git a/src/webex/pages/pay.tsx b/src/webex/pages/pay.tsx index 87f3c83d3..d255ab6e5 100644 --- a/src/webex/pages/pay.tsx +++ b/src/webex/pages/pay.tsx @@ -39,7 +39,7 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element { const [payErrMsg, setPayErrMsg] = useState(""); const [numTries, setNumTries] = useState(0); const [loading, setLoading] = useState(false); - let totalFees: Amounts.AmountJson | undefined = undefined; + let amountEffective: Amounts.AmountJson | undefined = undefined; useEffect(() => { const doFetch = async (): Promise => { @@ -59,7 +59,7 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element { } if (payStatus.status === "payment-possible") { - totalFees = payStatus.totalFees; + amountEffective = Amounts.parseOrThrow(payStatus.amountEffective); } if (payStatus.status === PreparePayResultType.AlreadyConfirmed && numTries === 0) { @@ -75,13 +75,13 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element { try { contractTerms = codecForContractTerms().decode( - JSON.parse(payStatus.contractTermsRaw), + JSON.parse(payStatus.contractTerms), ); } catch (e) { // This should never happen, as the wallet is supposed to check the contract terms // before storing them. console.error(e); - console.log("raw contract terms were", payStatus.contractTermsRaw); + console.log("raw contract terms were", payStatus.contractTerms); return Invalid contract terms.; } @@ -129,10 +129,10 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element {
{contractTerms.summary}
- {totalFees ? ( + {amountEffective ? ( The total price is {amount} - (plus {renderAmount(totalFees)} fees). + (plus {renderAmount(amountEffective)} fees). ) : (