From d5b7c732df6c0181c90d17b9550902961a78727b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 16 Apr 2024 15:07:22 -0300 Subject: fix #8746 --- packages/taler-wallet-core/src/pay-merchant.ts | 52 +++++++++++++++++++++----- 1 file changed, 43 insertions(+), 9 deletions(-) (limited to 'packages/taler-wallet-core/src/pay-merchant.ts') diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts index 80e88337e..e08f23beb 100644 --- a/packages/taler-wallet-core/src/pay-merchant.ts +++ b/packages/taler-wallet-core/src/pay-merchant.ts @@ -2752,14 +2752,36 @@ async function processPurchaseAutoRefund( const download = await expectProposalDownload(wex, purchase); - if ( - !purchase.autoRefundDeadline || - AbsoluteTime.isExpired( - AbsoluteTime.fromProtocolTimestamp( - timestampProtocolFromDb(purchase.autoRefundDeadline), - ), - ) - ) { + const noAutoRefundOrExpired = !purchase.autoRefundDeadline || + AbsoluteTime.isExpired( + AbsoluteTime.fromProtocolTimestamp( + timestampProtocolFromDb(purchase.autoRefundDeadline), + ), + ) + + const totalKnownRefund = await wex.db.runReadOnlyTx( + ["refundGroups"], + async (tx) => { + const refunds = await tx.refundGroups.indexes.byProposalId.getAll( + purchase.proposalId, + ); + const am = Amounts.parseOrThrow(download.contractData.amount); + return refunds.reduce((prev, cur) => { + if ( + cur.status === RefundGroupStatus.Done || + cur.status === RefundGroupStatus.Pending + ) { + return Amounts.add(prev, cur.amountEffective).amount; + } + return prev; + }, Amounts.zeroOfAmount(am)); + }, + ); + + const refundedIsLessThanPrice = Amounts.cmp(download.contractData.amount, totalKnownRefund) === +1 + const nothingMoreToRefund = !refundedIsLessThanPrice + + if (noAutoRefundOrExpired || nothingMoreToRefund) { const transitionInfo = await wex.db.runReadWriteTx( ["purchases"], async (tx) => { @@ -2792,8 +2814,9 @@ async function processPurchaseAutoRefund( download.contractData.contractTermsHash, ); - requestUrl.searchParams.set("timeout_ms", "1000"); + requestUrl.searchParams.set("timeout_ms", "10000"); requestUrl.searchParams.set("await_refund_obtained", "yes"); + requestUrl.searchParams.set("refund", Amounts.stringify(totalKnownRefund)); const resp = await wex.http.fetch(requestUrl.href, { cancellationToken: wex.cancellationToken, @@ -3344,9 +3367,20 @@ async function storeRefunds( } const oldTxState = computePayMerchantTransactionState(myPurchase); + + const shouldCheckAutoRefund = + myPurchase.autoRefundDeadline && + !AbsoluteTime.isExpired( + AbsoluteTime.fromProtocolTimestamp( + timestampProtocolFromDb(myPurchase.autoRefundDeadline), + ), + ); + if (numPendingItemsTotal === 0) { if (isAborting) { myPurchase.purchaseStatus = PurchaseStatus.AbortedRefunded; + } else if (shouldCheckAutoRefund) { + myPurchase.purchaseStatus = PurchaseStatus.PendingQueryingAutoRefund; } else { myPurchase.purchaseStatus = PurchaseStatus.Done; } -- cgit v1.2.3