From e5c9f588e4618d01f6b4c91028e175147a6b5a69 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 2 May 2022 19:21:13 -0300 Subject: add prepareRefund operation to gather information about the refund before confirm --- .../taler-wallet-core/src/operations/refund.ts | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'packages/taler-wallet-core/src/operations/refund.ts') diff --git a/packages/taler-wallet-core/src/operations/refund.ts b/packages/taler-wallet-core/src/operations/refund.ts index 7ef8076f0..dad8c6001 100644 --- a/packages/taler-wallet-core/src/operations/refund.ts +++ b/packages/taler-wallet-core/src/operations/refund.ts @@ -46,6 +46,8 @@ import { AbsoluteTime, TalerProtocolTimestamp, Duration, + PrepareRefundRequest, + PrepareRefundResult, } from "@gnu-taler/taler-util"; import { AbortStatus, @@ -69,6 +71,72 @@ import { guardOperationException } from "./common.js"; const logger = new Logger("refund.ts"); + +export async function prepareRefund( + ws: InternalWalletState, + talerRefundUri: string, +): Promise { + const parseResult = parseRefundUri(talerRefundUri); + + logger.trace("preparing refund offer", parseResult); + + if (!parseResult) { + throw Error("invalid refund URI"); + } + + const purchase = await ws.db + .mktx((x) => ({ + purchases: x.purchases, + })) + .runReadOnly(async (tx) => { + return tx.purchases.indexes.byMerchantUrlAndOrderId.get([ + parseResult.merchantBaseUrl, + parseResult.orderId, + ]); + }); + + if (!purchase) { + throw Error( + `no purchase for the taler://refund/ URI (${talerRefundUri}) was found`, + ); + } + + const proposalId = purchase.proposalId; + const rfs = Object.values(purchase.refunds) + + let applied = 0; + let failed = 0; + const total = rfs.length; + rfs.forEach((refund) => { + if (refund.type === RefundState.Failed) { + failed = failed + 1; + } + if (refund.type === RefundState.Applied) { + applied = applied + 1; + } + }); + + const { contractData: c } = purchase.download + + return { + proposalId, + amountEffectivePaid: Amounts.stringify(purchase.totalPayCost), + applied, + failed, + total, + info: { + contractTermsHash: c.contractTermsHash, + merchant: c.merchant, + orderId: c.orderId, + products: c.products, + summary: c.summary, + fulfillmentMessage: c.fulfillmentMessage, + summary_i18n: c.summaryI18n, + fulfillmentMessage_i18n: + c.fulfillmentMessageI18n, + }, + } +} /** * Retry querying and applying refunds for an order later. */ -- cgit v1.2.3