diff options
author | Florian Dold <florian@dold.me> | 2024-09-23 22:36:18 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-09-23 22:36:23 +0200 |
commit | 4cb0ada16cc3c0b156d4809d81376b0f7007adf8 (patch) | |
tree | 85e08e33c37cba1b7c69ac02b0c510dc23720315 /packages/taler-wallet-core | |
parent | cc74057905904b22322f59256993d04adfe713d0 (diff) |
wallet-core: fix repurchase, add test
The repurchase logic was broken if we already had previously detected a
repurchase for the same fulfullment URL.
Diffstat (limited to 'packages/taler-wallet-core')
-rw-r--r-- | packages/taler-wallet-core/src/pay-merchant.ts | 60 |
1 files changed, 40 insertions, 20 deletions
diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts index 8744fc5d1..4ed82b10f 100644 --- a/packages/taler-wallet-core/src/pay-merchant.ts +++ b/packages/taler-wallet-core/src/pay-merchant.ts @@ -891,7 +891,8 @@ async function processDownloadProposal( if (proposal.purchaseStatus != PurchaseStatus.PendingDownloadingProposal) { logger.error( - `unexpected state ${proposal.purchaseStatus}/${PurchaseStatus[proposal.purchaseStatus] + `unexpected state ${proposal.purchaseStatus}/${ + PurchaseStatus[proposal.purchaseStatus] } for ${ctx.transactionId} in processDownloadProposal`, ); return TaskRunResult.finished(); @@ -1075,22 +1076,29 @@ async function processDownloadProposal( fulfillmentUrl && (fulfillmentUrl.startsWith("http://") || fulfillmentUrl.startsWith("https://")); - let otherPurchase: PurchaseRecord | undefined; + let repurchase: PurchaseRecord | undefined = undefined; + const otherPurchases = + await tx.purchases.indexes.byFulfillmentUrl.getAll(fulfillmentUrl); if (isResourceFulfillmentUrl) { - otherPurchase = - await tx.purchases.indexes.byFulfillmentUrl.get(fulfillmentUrl); + for (const otherPurchase of otherPurchases) { + if ( + otherPurchase.purchaseStatus == PurchaseStatus.Done || + otherPurchase.purchaseStatus == PurchaseStatus.PendingPaying || + otherPurchase.purchaseStatus == PurchaseStatus.PendingPayingReplay + ) { + repurchase = otherPurchase; + break; + } + } } + // FIXME: Adjust this to account for refunds, don't count as repurchase // if original order is refunded. - if ( - otherPurchase && - (otherPurchase.purchaseStatus == PurchaseStatus.Done || - otherPurchase.purchaseStatus == PurchaseStatus.PendingPaying || - otherPurchase.purchaseStatus == PurchaseStatus.PendingPayingReplay) - ) { + + if (repurchase) { logger.warn("repurchase detected"); p.purchaseStatus = PurchaseStatus.DoneRepurchaseDetected; - p.repurchaseProposalId = otherPurchase.proposalId; + p.repurchaseProposalId = repurchase.proposalId; await tx.purchases.put(p); } else { p.purchaseStatus = p.shared @@ -1151,7 +1159,8 @@ async function createOrReusePurchase( oldProposal.claimToken === claimToken ) { logger.info( - `Found old proposal (status=${PurchaseStatus[oldProposal.purchaseStatus] + `Found old proposal (status=${ + PurchaseStatus[oldProposal.purchaseStatus] }) for order ${orderId} at ${merchantBaseUrl}`, ); if (oldProposal.purchaseStatus === PurchaseStatus.DialogShared) { @@ -1211,6 +1220,10 @@ async function createOrReusePurchase( const { priv, pub } = noncePair; const proposalId = encodeCrock(getRandomBytes(32)); + logger.info( + `created new proposal for ${orderId} at ${merchantBaseUrl} session ${sessionId}`, + ); + const proposalRecord: PurchaseRecord = { download: undefined, noncePriv: priv, @@ -1648,8 +1661,8 @@ async function checkPaymentByProposalId( } if ( - purchase.purchaseStatus === PurchaseStatus.Done && - purchase.lastSessionId !== sessionId + purchase.purchaseStatus === PurchaseStatus.Done || + purchase.purchaseStatus === PurchaseStatus.PendingPayingReplay ) { logger.trace( "automatically re-submitting payment with different session ID", @@ -1708,11 +1721,7 @@ async function checkPaymentByProposalId( talerUri, }; } else { - const paid = - purchase.purchaseStatus === PurchaseStatus.Done || - purchase.purchaseStatus === PurchaseStatus.PendingQueryingRefund || - purchase.purchaseStatus === PurchaseStatus.FinalizingQueryingAutoRefund || - purchase.purchaseStatus === PurchaseStatus.PendingQueryingAutoRefund; + const paid = isPurchasePaid(purchase); const download = await expectProposalDownload(wex, purchase); return { status: PreparePayResultType.AlreadyConfirmed, @@ -1731,6 +1740,15 @@ async function checkPaymentByProposalId( } } +function isPurchasePaid(purchase: PurchaseRecord): boolean { + return ( + purchase.purchaseStatus === PurchaseStatus.Done || + purchase.purchaseStatus === PurchaseStatus.PendingQueryingRefund || + purchase.purchaseStatus === PurchaseStatus.FinalizingQueryingAutoRefund || + purchase.purchaseStatus === PurchaseStatus.PendingQueryingAutoRefund + ); +} + export async function getContractTermsDetails( wex: WalletExecutionContext, proposalId: string, @@ -1897,7 +1915,9 @@ export async function checkPayForTemplate( if (cfg.detail) { throw TalerError.fromUncheckedDetail(cfg.detail); } else { - throw TalerError.fromException(new Error("failed to get merchant remote config")) + throw TalerError.fromException( + new Error("failed to get merchant remote config"), + ); } } |