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> {
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;

View File

@ -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,
};
}

View File

@ -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;

View File

@ -39,7 +39,7 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element {
const [payErrMsg, setPayErrMsg] = useState<string | undefined>("");
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<void> => {
@ -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 <span>Invalid contract terms.</span>;
}
@ -129,10 +129,10 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }): JSX.Element {
<div style={{ textAlign: "center" }}>
<strong>{contractTerms.summary}</strong>
</div>
{totalFees ? (
{amountEffective ? (
<i18n.Translate wrap="p">
The total price is <span>{amount} </span>
(plus <span>{renderAmount(totalFees)}</span> fees).
(plus <span>{renderAmount(amountEffective)}</span> fees).
</i18n.Translate>
) : (
<i18n.Translate wrap="p">