tweaks to pay API

This commit is contained in:
Florian Dold 2020-07-29 23:10:41 +05:30
parent dfe5e95bc8
commit c8553f3bc5
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 25 additions and 23 deletions

View File

@ -60,7 +60,7 @@ async function doPay(
): Promise<void> { ): Promise<void> {
const result = await wallet.preparePayForUri(payUrl); const result = await wallet.preparePayForUri(payUrl);
if (result.status === PreparePayResultType.InsufficientBalance) { if (result.status === PreparePayResultType.InsufficientBalance) {
console.log("contract", result.contractTermsRaw); console.log("contract", result.contractTerms);
console.error("insufficient balance"); console.error("insufficient balance");
process.exit(1); process.exit(1);
return; return;
@ -80,8 +80,9 @@ async function doPay(
} else { } else {
throw Error("not reached"); throw Error("not reached");
} }
console.log("contract", result.contractTermsRaw); console.log("contract", result.contractTerms);
console.log("total fees:", Amounts.stringify(result.totalFees)); console.log("raw amount:", result.amountRaw);
console.log("effective amount:", result.amountEffective);
let pay; let pay;
if (options.alwaysYes) { if (options.alwaysYes) {
pay = true; pay = true;

View File

@ -915,26 +915,26 @@ export async function preparePayForUri(
console.log("not confirming payment, insufficient coins"); console.log("not confirming payment, insufficient coins");
return { return {
status: PreparePayResultType.InsufficientBalance, status: PreparePayResultType.InsufficientBalance,
contractTermsRaw: d.contractTermsRaw, contractTerms: d.contractTermsRaw,
proposalId: proposal.proposalId, proposalId: proposal.proposalId,
}; };
} }
const costInfo = await getTotalPaymentCost(ws, res); const costInfo = await getTotalPaymentCost(ws, res);
console.log("costInfo", costInfo); logger.trace("costInfo", costInfo);
console.log("coinsForPayment", res); logger.trace("coinsForPayment", res);
const totalFees = Amounts.sub(costInfo.totalCost, res.paymentAmount).amount;
return { return {
status: PreparePayResultType.PaymentPossible, status: PreparePayResultType.PaymentPossible,
contractTermsRaw: d.contractTermsRaw, contractTerms: d.contractTermsRaw,
proposalId: proposal.proposalId, proposalId: proposal.proposalId,
totalFees, amountEffective: Amounts.stringify(costInfo.totalCost),
amountRaw: Amounts.stringify(res.paymentAmount),
}; };
} }
if (purchase.lastSessionId !== uriResult.sessionId) { if (purchase.lastSessionId !== uriResult.sessionId) {
console.log( logger.trace(
"automatically re-submitting payment with different session ID", "automatically re-submitting payment with different session ID",
); );
await ws.db.runWithWriteTransaction([Stores.purchases], async (tx) => { await ws.db.runWithWriteTransaction([Stores.purchases], async (tx) => {
@ -948,20 +948,20 @@ export async function preparePayForUri(
const r = await submitPay(ws, proposalId); const r = await submitPay(ws, proposalId);
return { return {
status: PreparePayResultType.AlreadyConfirmed, status: PreparePayResultType.AlreadyConfirmed,
contractTermsRaw: purchase.contractTermsRaw, contractTerms: purchase.contractTermsRaw,
paid: true, paid: true,
nextUrl: r.nextUrl, nextUrl: r.nextUrl,
}; };
} else if (!purchase.timestampFirstSuccessfulPay) { } else if (!purchase.timestampFirstSuccessfulPay) {
return { return {
status: PreparePayResultType.AlreadyConfirmed, status: PreparePayResultType.AlreadyConfirmed,
contractTermsRaw: purchase.contractTermsRaw, contractTerms: purchase.contractTermsRaw,
paid: false, paid: false,
}; };
} else if (purchase.paymentSubmitPending) { } else if (purchase.paymentSubmitPending) {
return { return {
status: PreparePayResultType.AlreadyConfirmed, status: PreparePayResultType.AlreadyConfirmed,
contractTermsRaw: purchase.contractTermsRaw, contractTerms: purchase.contractTermsRaw,
paid: false, paid: false,
}; };
} }

View File

@ -329,19 +329,20 @@ export type PreparePayResult =
export interface PreparePayResultPaymentPossible { export interface PreparePayResultPaymentPossible {
status: PreparePayResultType.PaymentPossible; status: PreparePayResultType.PaymentPossible;
proposalId: string; proposalId: string;
contractTermsRaw: string; contractTerms: string;
totalFees: AmountJson; amountRaw: string;
amountEffective: string;
} }
export interface PreparePayResultInsufficientBalance { export interface PreparePayResultInsufficientBalance {
status: PreparePayResultType.InsufficientBalance; status: PreparePayResultType.InsufficientBalance;
proposalId: string; proposalId: string;
contractTermsRaw: any; contractTerms: any;
} }
export interface PreparePayResultAlreadyConfirmed { export interface PreparePayResultAlreadyConfirmed {
status: PreparePayResultType.AlreadyConfirmed; status: PreparePayResultType.AlreadyConfirmed;
contractTermsRaw: any; contractTerms: any;
paid: boolean; paid: boolean;
// Only specified if paid. // Only specified if paid.
nextUrl?: string; nextUrl?: string;

View File

@ -39,7 +39,7 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element {
const [payErrMsg, setPayErrMsg] = useState<string | undefined>(""); const [payErrMsg, setPayErrMsg] = useState<string | undefined>("");
const [numTries, setNumTries] = useState(0); const [numTries, setNumTries] = useState(0);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
let totalFees: Amounts.AmountJson | undefined = undefined; let amountEffective: Amounts.AmountJson | undefined = undefined;
useEffect(() => { useEffect(() => {
const doFetch = async (): Promise<void> => { const doFetch = async (): Promise<void> => {
@ -59,7 +59,7 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element {
} }
if (payStatus.status === "payment-possible") { if (payStatus.status === "payment-possible") {
totalFees = payStatus.totalFees; amountEffective = Amounts.parseOrThrow(payStatus.amountEffective);
} }
if (payStatus.status === PreparePayResultType.AlreadyConfirmed && numTries === 0) { if (payStatus.status === PreparePayResultType.AlreadyConfirmed && numTries === 0) {
@ -75,13 +75,13 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element {
try { try {
contractTerms = codecForContractTerms().decode( contractTerms = codecForContractTerms().decode(
JSON.parse(payStatus.contractTermsRaw), JSON.parse(payStatus.contractTerms),
); );
} catch (e) { } catch (e) {
// This should never happen, as the wallet is supposed to check the contract terms // This should never happen, as the wallet is supposed to check the contract terms
// before storing them. // before storing them.
console.error(e); console.error(e);
console.log("raw contract terms were", payStatus.contractTermsRaw); console.log("raw contract terms were", payStatus.contractTerms);
return <span>Invalid contract terms.</span>; return <span>Invalid contract terms.</span>;
} }
@ -129,10 +129,10 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element {
<div style={{ textAlign: "center" }}> <div style={{ textAlign: "center" }}>
<strong>{contractTerms.summary}</strong> <strong>{contractTerms.summary}</strong>
</div> </div>
{totalFees ? ( {amountEffective ? (
<i18n.Translate wrap="p"> <i18n.Translate wrap="p">
The total price is <span>{amount} </span> The total price is <span>{amount} </span>
(plus <span>{renderAmount(totalFees)}</span> fees). (plus <span>{renderAmount(amountEffective)}</span> fees).
</i18n.Translate> </i18n.Translate>
) : ( ) : (
<i18n.Translate wrap="p"> <i18n.Translate wrap="p">