From 8124d2400e7787020cca549ccc9d021eb5f75a09 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 14 Feb 2024 17:09:30 -0300 Subject: fix #8403 --- .../src/operations/transactions.ts | 112 +++++++++++++++------ 1 file changed, 79 insertions(+), 33 deletions(-) (limited to 'packages/taler-wallet-core/src/operations/transactions.ts') diff --git a/packages/taler-wallet-core/src/operations/transactions.ts b/packages/taler-wallet-core/src/operations/transactions.ts index 13eda7a92..3b4e75427 100644 --- a/packages/taler-wallet-core/src/operations/transactions.ts +++ b/packages/taler-wallet-core/src/operations/transactions.ts @@ -492,11 +492,14 @@ function buildTransactionForPushPaymentDebit( contractPriv: pi.contractPriv, }); } + const txState = computePeerPushDebitTransactionState(pi); return { type: TransactionType.PeerPushDebit, - txState: computePeerPushDebitTransactionState(pi), + txState, txActions: computePeerPushDebitTransactionActions(pi), - amountEffective: pi.totalCost, + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(pi.totalCost)) + : pi.totalCost, amountRaw: pi.amount, exchangeBaseUrl: pi.exchangeBaseUrl, info: { @@ -518,13 +521,16 @@ function buildTransactionForPullPaymentDebit( contractTerms: PeerContractTerms, ort?: OperationRetryRecord, ): Transaction { + const txState = computePeerPullDebitTransactionState(pi); return { type: TransactionType.PeerPullDebit, - txState: computePeerPullDebitTransactionState(pi), + txState, txActions: computePeerPullDebitTransactionActions(pi), - amountEffective: pi.coinSel?.totalCost - ? pi.coinSel?.totalCost - : Amounts.stringify(pi.amount), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(pi.amount)) + : pi.coinSel?.totalCost + ? pi.coinSel?.totalCost + : Amounts.stringify(pi.amount), amountRaw: Amounts.stringify(pi.amount), exchangeBaseUrl: pi.exchangeBaseUrl, info: { @@ -566,11 +572,14 @@ function buildTransactionForPeerPullCredit( e.httpStatusCode === 409 ); }); + const txState = computePeerPullCreditTransactionState(pullCredit); return { type: TransactionType.PeerPullCredit, - txState: computePeerPullCreditTransactionState(pullCredit), + txState, txActions: computePeerPullCreditTransactionActions(pullCredit), - amountEffective: Amounts.stringify(wsr.denomsSel.totalCoinValue), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(wsr.instructedAmount)) + : Amounts.stringify(wsr.denomsSel.totalCoinValue), amountRaw: Amounts.stringify(wsr.instructedAmount), exchangeBaseUrl: wsr.exchangeBaseUrl, timestamp: timestampPreciseFromDb(pullCredit.mergeTimestamp), @@ -597,11 +606,14 @@ function buildTransactionForPeerPullCredit( }; } + const txState = computePeerPullCreditTransactionState(pullCredit); return { type: TransactionType.PeerPullCredit, - txState: computePeerPullCreditTransactionState(pullCredit), + txState, txActions: computePeerPullCreditTransactionActions(pullCredit), - amountEffective: Amounts.stringify(pullCredit.estimatedAmountEffective), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(peerContractTerms.amount)) + : Amounts.stringify(pullCredit.estimatedAmountEffective), amountRaw: Amounts.stringify(peerContractTerms.amount), exchangeBaseUrl: pullCredit.exchangeBaseUrl, timestamp: timestampPreciseFromDb(pullCredit.mergeTimestamp), @@ -634,11 +646,14 @@ function buildTransactionForPeerPushCredit( throw Error("invalid withdrawal group type for push payment credit"); } + const txState = computePeerPushCreditTransactionState(pushInc); return { type: TransactionType.PeerPushCredit, - txState: computePeerPushCreditTransactionState(pushInc), + txState, txActions: computePeerPushCreditTransactionActions(pushInc), - amountEffective: Amounts.stringify(wsr.denomsSel.totalCoinValue), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(wsr.instructedAmount)) + : Amounts.stringify(wsr.denomsSel.totalCoinValue), amountRaw: Amounts.stringify(wsr.instructedAmount), exchangeBaseUrl: wsr.exchangeBaseUrl, info: { @@ -655,12 +670,15 @@ function buildTransactionForPeerPushCredit( }; } + const txState = computePeerPushCreditTransactionState(pushInc); return { type: TransactionType.PeerPushCredit, - txState: computePeerPushCreditTransactionState(pushInc), + txState, txActions: computePeerPushCreditTransactionActions(pushInc), - // FIXME: This is wrong, needs to consider fees! - amountEffective: Amounts.stringify(peerContractTerms.amount), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(peerContractTerms.amount)) + // FIXME: This is wrong, needs to consider fees! + : Amounts.stringify(peerContractTerms.amount), amountRaw: Amounts.stringify(peerContractTerms.amount), exchangeBaseUrl: pushInc.exchangeBaseUrl, info: { @@ -684,11 +702,14 @@ function buildTransactionForBankIntegratedWithdraw( if (wgRecord.wgInfo.withdrawalType !== WithdrawalRecordType.BankIntegrated) throw Error(""); + const txState = computeWithdrawalTransactionStatus(wgRecord); return { type: TransactionType.Withdrawal, - txState: computeWithdrawalTransactionStatus(wgRecord), + txState, txActions: computeWithdrawalTransactionActions(wgRecord), - amountEffective: Amounts.stringify(wgRecord.denomsSel.totalCoinValue), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(wgRecord.instructedAmount)) + : Amounts.stringify(wgRecord.denomsSel.totalCoinValue), amountRaw: Amounts.stringify(wgRecord.instructedAmount), withdrawalDetails: { type: WithdrawalType.TalerBankIntegrationApi, @@ -711,6 +732,14 @@ function buildTransactionForBankIntegratedWithdraw( }; } +function isUnsuccessfulTransaction(state: TransactionState): boolean { + return state.major === TransactionMajorState.Aborted || + state.major === TransactionMajorState.Expired || + state.major === TransactionMajorState.Aborting || + state.major === TransactionMajorState.Deleted || + state.major === TransactionMajorState.Failed; +} + function buildTransactionForManualWithdraw( withdrawalGroup: WithdrawalGroupRecord, exchangeDetails: ExchangeWireDetails, @@ -728,13 +757,15 @@ function buildTransactionForManualWithdraw( withdrawalGroup.instructedAmount, ); + const txState = computeWithdrawalTransactionStatus(withdrawalGroup); + return { type: TransactionType.Withdrawal, - txState: computeWithdrawalTransactionStatus(withdrawalGroup), + txState, txActions: computeWithdrawalTransactionActions(withdrawalGroup), - amountEffective: Amounts.stringify( - withdrawalGroup.denomsSel.totalCoinValue, - ), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(withdrawalGroup.instructedAmount)) + : Amounts.stringify(withdrawalGroup.denomsSel.totalCoinValue), amountRaw: Amounts.stringify(withdrawalGroup.instructedAmount), withdrawalDetails: { type: WithdrawalType.ManualTransfer, @@ -771,9 +802,12 @@ function buildTransactionForRefund( }; } + const txState = computeRefundTransactionState(refundRecord); return { type: TransactionType.Refund, - amountEffective: refundRecord.amountEffective, + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(refundRecord.amountEffective)) + : refundRecord.amountEffective, amountRaw: refundRecord.amountRaw, refundedTransactionId: constructTransactionIdentifier({ tag: TransactionType.Payment, @@ -784,7 +818,7 @@ function buildTransactionForRefund( tag: TransactionType.Refund, refundGroupId: refundRecord.refundGroupId, }), - txState: computeRefundTransactionState(refundRecord), + txState, txActions: [], paymentInfo, }; @@ -802,14 +836,17 @@ function buildTransactionForRefresh( refreshGroupRecord.currency, refreshGroupRecord.expectedOutputPerCoin, ).amount; + const txState = computeRefreshTransactionState(refreshGroupRecord); return { type: TransactionType.Refresh, - txState: computeRefreshTransactionState(refreshGroupRecord), + txState, txActions: computeRefreshTransactionActions(refreshGroupRecord), refreshReason: refreshGroupRecord.reason, - amountEffective: Amounts.stringify( - Amounts.sub(outputAmount, inputAmount).amount, - ), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(inputAmount)) + : Amounts.stringify( + Amounts.sub(outputAmount, inputAmount).amount, + ), amountRaw: Amounts.stringify( Amounts.zeroOfCurrency(refreshGroupRecord.currency), ), @@ -847,12 +884,15 @@ function buildTransactionForDeposit( }); } + const txState = computeDepositTransactionStatus(dg); return { type: TransactionType.Deposit, - txState: computeDepositTransactionStatus(dg), + txState, txActions: computeDepositTransactionActions(dg), amountRaw: Amounts.stringify(dg.counterpartyEffectiveDepositAmount), - amountEffective: Amounts.stringify(dg.totalPayCost), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(dg.totalPayCost)) + : Amounts.stringify(dg.totalPayCost), timestamp: timestampPreciseFromDb(dg.timestampCreated), targetPaytoUri: dg.wire.payto_uri, wireTransferDeadline: timestampProtocolFromDb(dg.wireTransferDeadline), @@ -880,11 +920,14 @@ function buildTransactionForTip( ): Transaction { checkLogicInvariant(!!tipRecord.acceptedTimestamp); + const txState = computeRewardTransactionStatus(tipRecord); return { type: TransactionType.Reward, - txState: computeRewardTransactionStatus(tipRecord), + txState, txActions: computeTipTransactionActions(tipRecord), - amountEffective: Amounts.stringify(tipRecord.rewardAmountEffective), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(Amounts.zeroOfAmount(tipRecord.rewardAmountEffective)) + : Amounts.stringify(tipRecord.rewardAmountEffective), amountRaw: Amounts.stringify(tipRecord.rewardAmountRaw), timestamp: timestampPreciseFromDb(tipRecord.acceptedTimestamp), transactionId: constructTransactionIdentifier({ @@ -959,12 +1002,15 @@ async function buildTransactionForPurchase( checkDbInvariant(!!timestamp); checkDbInvariant(!!purchaseRecord.payInfo); + const txState = computePayMerchantTransactionState(purchaseRecord); return { type: TransactionType.Payment, - txState: computePayMerchantTransactionState(purchaseRecord), + txState, txActions: computePayMerchantTransactionActions(purchaseRecord), amountRaw: Amounts.stringify(contractData.amount), - amountEffective: Amounts.stringify(purchaseRecord.payInfo.totalPayCost), + amountEffective: isUnsuccessfulTransaction(txState) + ? Amounts.stringify(zero) + : Amounts.stringify(purchaseRecord.payInfo.totalPayCost), totalRefundRaw: Amounts.stringify(zero), // FIXME! totalRefundEffective: Amounts.stringify(zero), // FIXME! refundPending: -- cgit v1.2.3