2019-12-15 19:08:07 +01:00
|
|
|
/*
|
|
|
|
This file is part of GNU Taler
|
2020-01-19 17:14:43 +01:00
|
|
|
(C) 2019-2019 Taler Systems S.A.
|
2019-12-15 19:08:07 +01:00
|
|
|
|
|
|
|
GNU Taler is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of the refund operation.
|
|
|
|
*
|
|
|
|
* @author Florian Dold
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
|
|
|
import { InternalWalletState } from "./state";
|
|
|
|
import {
|
2020-09-01 14:57:22 +02:00
|
|
|
TalerErrorDetails,
|
2019-12-15 19:08:07 +01:00
|
|
|
RefreshReason,
|
2019-12-16 12:53:22 +01:00
|
|
|
CoinPublicKey,
|
2020-09-01 16:26:08 +02:00
|
|
|
ApplyRefundResponse,
|
2019-12-15 19:08:07 +01:00
|
|
|
} from "../types/walletTypes";
|
|
|
|
import {
|
|
|
|
Stores,
|
|
|
|
updateRetryInfoTimeout,
|
|
|
|
initRetryInfo,
|
|
|
|
CoinStatus,
|
|
|
|
RefundReason,
|
2020-07-23 14:05:17 +02:00
|
|
|
RefundState,
|
|
|
|
PurchaseRecord,
|
2019-12-15 19:08:07 +01:00
|
|
|
} from "../types/dbTypes";
|
|
|
|
import { NotificationType } from "../types/notifications";
|
|
|
|
import { parseRefundUri } from "../util/taleruri";
|
|
|
|
import { createRefreshGroup, getTotalRefreshCost } from "./refresh";
|
2020-08-11 14:25:45 +02:00
|
|
|
import { Amounts, AmountJson } from "../util/amounts";
|
2019-12-15 19:08:07 +01:00
|
|
|
import {
|
2020-07-23 14:05:17 +02:00
|
|
|
MerchantCoinRefundStatus,
|
|
|
|
MerchantCoinRefundSuccessStatus,
|
|
|
|
MerchantCoinRefundFailureStatus,
|
2020-07-27 10:06:32 +02:00
|
|
|
codecForMerchantOrderStatusPaid,
|
2020-08-11 14:25:45 +02:00
|
|
|
AmountString,
|
2020-08-20 08:09:34 +02:00
|
|
|
codecForMerchantOrderRefundPickupResponse,
|
2019-12-15 19:08:07 +01:00
|
|
|
} from "../types/talerTypes";
|
2020-05-12 12:14:48 +02:00
|
|
|
import { guardOperationException } from "./errors";
|
2020-08-14 12:23:50 +02:00
|
|
|
import { getTimestampNow, Timestamp } from "../util/time";
|
2020-01-19 20:41:51 +01:00
|
|
|
import { Logger } from "../util/logging";
|
2020-07-22 10:52:03 +02:00
|
|
|
import { readSuccessResponseJsonOrThrow } from "../util/http";
|
2020-07-23 14:05:17 +02:00
|
|
|
import { TransactionHandle } from "../util/query";
|
2020-08-03 09:30:48 +02:00
|
|
|
import { URL } from "../util/url";
|
2020-01-19 20:41:51 +01:00
|
|
|
|
|
|
|
const logger = new Logger("refund.ts");
|
2019-12-15 19:08:07 +01:00
|
|
|
|
2020-07-22 10:52:03 +02:00
|
|
|
/**
|
|
|
|
* Retry querying and applying refunds for an order later.
|
|
|
|
*/
|
2019-12-15 19:08:07 +01:00
|
|
|
async function incrementPurchaseQueryRefundRetry(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
proposalId: string,
|
2020-09-01 14:57:22 +02:00
|
|
|
err: TalerErrorDetails | undefined,
|
2019-12-15 19:08:07 +01:00
|
|
|
): Promise<void> {
|
2020-03-30 12:34:16 +02:00
|
|
|
await ws.db.runWithWriteTransaction([Stores.purchases], async (tx) => {
|
2019-12-15 19:08:07 +01:00
|
|
|
const pr = await tx.get(Stores.purchases, proposalId);
|
|
|
|
if (!pr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!pr.refundStatusRetryInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pr.refundStatusRetryInfo.retryCounter++;
|
|
|
|
updateRetryInfoTimeout(pr.refundStatusRetryInfo);
|
|
|
|
pr.lastRefundStatusError = err;
|
|
|
|
await tx.put(Stores.purchases, pr);
|
|
|
|
});
|
2020-07-22 10:52:03 +02:00
|
|
|
if (err) {
|
|
|
|
ws.notify({
|
|
|
|
type: NotificationType.RefundStatusOperationError,
|
|
|
|
error: err,
|
|
|
|
});
|
2019-12-15 19:08:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
function getRefundKey(d: MerchantCoinRefundStatus): string {
|
2020-05-15 19:24:39 +02:00
|
|
|
return `${d.coin_pub}-${d.rtransaction_id}`;
|
2020-04-27 17:41:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
async function applySuccessfulRefund(
|
|
|
|
tx: TransactionHandle,
|
|
|
|
p: PurchaseRecord,
|
|
|
|
refreshCoinsMap: Record<string, { coinPub: string }>,
|
|
|
|
r: MerchantCoinRefundSuccessStatus,
|
2019-12-15 19:08:07 +01:00
|
|
|
): Promise<void> {
|
2020-07-23 14:05:17 +02:00
|
|
|
// FIXME: check signature before storing it as valid!
|
2019-12-15 19:08:07 +01:00
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
const refundKey = getRefundKey(r);
|
|
|
|
const coin = await tx.get(Stores.coins, r.coin_pub);
|
|
|
|
if (!coin) {
|
|
|
|
console.warn("coin not found, can't apply refund");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const denom = await tx.getIndexed(
|
|
|
|
Stores.denominations.denomPubHashIndex,
|
|
|
|
coin.denomPubHash,
|
|
|
|
);
|
|
|
|
if (!denom) {
|
|
|
|
throw Error("inconsistent database");
|
|
|
|
}
|
|
|
|
refreshCoinsMap[coin.coinPub] = { coinPub: coin.coinPub };
|
|
|
|
const refundAmount = Amounts.parseOrThrow(r.refund_amount);
|
|
|
|
const refundFee = denom.feeRefund;
|
|
|
|
coin.status = CoinStatus.Dormant;
|
|
|
|
coin.currentAmount = Amounts.add(coin.currentAmount, refundAmount).amount;
|
|
|
|
coin.currentAmount = Amounts.sub(coin.currentAmount, refundFee).amount;
|
|
|
|
logger.trace(`coin amount after is ${Amounts.stringify(coin.currentAmount)}`);
|
|
|
|
await tx.put(Stores.coins, coin);
|
|
|
|
|
|
|
|
const allDenoms = await tx
|
2020-08-03 09:30:48 +02:00
|
|
|
.iterIndexed(
|
|
|
|
Stores.denominations.exchangeBaseUrlIndex,
|
|
|
|
coin.exchangeBaseUrl,
|
|
|
|
)
|
2020-07-23 14:05:17 +02:00
|
|
|
.toArray();
|
|
|
|
|
|
|
|
const amountLeft = Amounts.sub(
|
|
|
|
Amounts.add(coin.currentAmount, Amounts.parseOrThrow(r.refund_amount))
|
|
|
|
.amount,
|
|
|
|
denom.feeRefund,
|
|
|
|
).amount;
|
|
|
|
|
|
|
|
const totalRefreshCostBound = getTotalRefreshCost(
|
|
|
|
allDenoms,
|
|
|
|
denom,
|
|
|
|
amountLeft,
|
|
|
|
);
|
2020-04-27 17:41:20 +02:00
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
p.refunds[refundKey] = {
|
|
|
|
type: RefundState.Applied,
|
2020-08-14 12:23:50 +02:00
|
|
|
obtainedTime: getTimestampNow(),
|
2020-07-23 14:05:17 +02:00
|
|
|
executionTime: r.execution_time,
|
|
|
|
refundAmount: Amounts.parseOrThrow(r.refund_amount),
|
|
|
|
refundFee: denom.feeRefund,
|
|
|
|
totalRefreshCostBound,
|
|
|
|
};
|
|
|
|
}
|
2020-05-15 19:24:39 +02:00
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
async function storePendingRefund(
|
|
|
|
tx: TransactionHandle,
|
|
|
|
p: PurchaseRecord,
|
|
|
|
r: MerchantCoinRefundFailureStatus,
|
|
|
|
): Promise<void> {
|
|
|
|
const refundKey = getRefundKey(r);
|
2020-05-15 13:09:40 +02:00
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
const coin = await tx.get(Stores.coins, r.coin_pub);
|
|
|
|
if (!coin) {
|
|
|
|
console.warn("coin not found, can't apply refund");
|
|
|
|
return;
|
2020-04-27 17:41:20 +02:00
|
|
|
}
|
2020-07-23 14:05:17 +02:00
|
|
|
const denom = await tx.getIndexed(
|
|
|
|
Stores.denominations.denomPubHashIndex,
|
|
|
|
coin.denomPubHash,
|
|
|
|
);
|
2019-12-15 19:08:07 +01:00
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
if (!denom) {
|
|
|
|
throw Error("inconsistent database");
|
2020-05-15 13:09:40 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
const allDenoms = await tx
|
2020-08-03 09:30:48 +02:00
|
|
|
.iterIndexed(
|
|
|
|
Stores.denominations.exchangeBaseUrlIndex,
|
|
|
|
coin.exchangeBaseUrl,
|
|
|
|
)
|
2020-07-23 14:05:17 +02:00
|
|
|
.toArray();
|
|
|
|
|
|
|
|
const amountLeft = Amounts.sub(
|
|
|
|
Amounts.add(coin.currentAmount, Amounts.parseOrThrow(r.refund_amount))
|
|
|
|
.amount,
|
|
|
|
denom.feeRefund,
|
|
|
|
).amount;
|
|
|
|
|
|
|
|
const totalRefreshCostBound = getTotalRefreshCost(
|
|
|
|
allDenoms,
|
|
|
|
denom,
|
|
|
|
amountLeft,
|
|
|
|
);
|
|
|
|
|
|
|
|
p.refunds[refundKey] = {
|
|
|
|
type: RefundState.Pending,
|
2020-08-14 12:23:50 +02:00
|
|
|
obtainedTime: getTimestampNow(),
|
2020-07-23 14:05:17 +02:00
|
|
|
executionTime: r.execution_time,
|
|
|
|
refundAmount: Amounts.parseOrThrow(r.refund_amount),
|
|
|
|
refundFee: denom.feeRefund,
|
|
|
|
totalRefreshCostBound,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function acceptRefunds(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
proposalId: string,
|
|
|
|
refunds: MerchantCoinRefundStatus[],
|
|
|
|
reason: RefundReason,
|
|
|
|
): Promise<void> {
|
2020-08-10 13:18:38 +02:00
|
|
|
logger.trace("handling refunds", refunds);
|
2020-05-12 12:14:48 +02:00
|
|
|
const now = getTimestampNow();
|
|
|
|
|
2020-04-27 17:41:20 +02:00
|
|
|
await ws.db.runWithWriteTransaction(
|
2020-08-03 09:30:48 +02:00
|
|
|
[
|
|
|
|
Stores.purchases,
|
|
|
|
Stores.coins,
|
|
|
|
Stores.denominations,
|
|
|
|
Stores.refreshGroups,
|
|
|
|
Stores.refundEvents,
|
|
|
|
],
|
2020-04-27 17:41:20 +02:00
|
|
|
async (tx) => {
|
|
|
|
const p = await tx.get(Stores.purchases, proposalId);
|
|
|
|
if (!p) {
|
|
|
|
console.error("purchase not found, not adding refunds");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
const refreshCoinsMap: Record<string, CoinPublicKey> = {};
|
2020-04-27 17:41:20 +02:00
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
for (const refundStatus of refunds) {
|
|
|
|
const refundKey = getRefundKey(refundStatus);
|
|
|
|
const existingRefundInfo = p.refunds[refundKey];
|
|
|
|
|
|
|
|
// Already failed.
|
|
|
|
if (existingRefundInfo?.type === RefundState.Failed) {
|
2020-04-27 17:41:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
// Already applied.
|
|
|
|
if (existingRefundInfo?.type === RefundState.Applied) {
|
|
|
|
continue;
|
2020-04-27 17:41:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
// Still pending.
|
|
|
|
if (
|
2020-07-24 11:12:35 +02:00
|
|
|
refundStatus.type === "failure" &&
|
2020-07-23 14:05:17 +02:00
|
|
|
existingRefundInfo?.type === RefundState.Pending
|
|
|
|
) {
|
2020-04-27 17:41:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
// Invariant: (!existingRefundInfo) || (existingRefundInfo === Pending)
|
2020-04-27 17:41:20 +02:00
|
|
|
|
2020-07-24 11:12:35 +02:00
|
|
|
if (refundStatus.type === "success") {
|
2020-07-23 14:05:17 +02:00
|
|
|
await applySuccessfulRefund(tx, p, refreshCoinsMap, refundStatus);
|
|
|
|
} else {
|
|
|
|
await storePendingRefund(tx, p, refundStatus);
|
2020-04-27 17:41:20 +02:00
|
|
|
}
|
2019-12-15 19:08:07 +01:00
|
|
|
}
|
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
const refreshCoinsPubs = Object.values(refreshCoinsMap);
|
2020-08-20 08:29:06 +02:00
|
|
|
if (refreshCoinsPubs.length > 0) {
|
2020-09-01 15:37:14 +02:00
|
|
|
await createRefreshGroup(
|
|
|
|
ws,
|
|
|
|
tx,
|
|
|
|
refreshCoinsPubs,
|
|
|
|
RefreshReason.Refund,
|
|
|
|
);
|
2020-08-20 08:29:06 +02:00
|
|
|
}
|
2020-07-23 14:05:17 +02:00
|
|
|
|
2020-04-27 17:41:20 +02:00
|
|
|
// Are we done with querying yet, or do we need to do another round
|
|
|
|
// after a retry delay?
|
|
|
|
let queryDone = true;
|
2019-12-15 19:08:07 +01:00
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
if (p.autoRefundDeadline && p.autoRefundDeadline.t_ms > now.t_ms) {
|
|
|
|
queryDone = false;
|
|
|
|
}
|
2020-05-15 19:24:39 +02:00
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
let numPendingRefunds = 0;
|
|
|
|
for (const ri of Object.values(p.refunds)) {
|
|
|
|
switch (ri.type) {
|
|
|
|
case RefundState.Pending:
|
|
|
|
numPendingRefunds++;
|
|
|
|
break;
|
2020-04-27 17:41:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
if (numPendingRefunds > 0) {
|
2019-12-15 19:08:07 +01:00
|
|
|
queryDone = false;
|
|
|
|
}
|
|
|
|
|
2020-04-27 17:41:20 +02:00
|
|
|
if (queryDone) {
|
2020-05-12 12:14:48 +02:00
|
|
|
p.timestampLastRefundStatus = now;
|
2020-04-27 17:41:20 +02:00
|
|
|
p.lastRefundStatusError = undefined;
|
|
|
|
p.refundStatusRetryInfo = initRetryInfo(false);
|
|
|
|
p.refundStatusRequested = false;
|
2020-07-22 10:52:03 +02:00
|
|
|
logger.trace("refund query done");
|
2020-04-27 17:41:20 +02:00
|
|
|
} else {
|
|
|
|
// No error, but we need to try again!
|
2020-05-12 12:14:48 +02:00
|
|
|
p.timestampLastRefundStatus = now;
|
2020-04-27 17:41:20 +02:00
|
|
|
p.refundStatusRetryInfo.retryCounter++;
|
|
|
|
updateRetryInfoTimeout(p.refundStatusRetryInfo);
|
|
|
|
p.lastRefundStatusError = undefined;
|
2020-07-22 10:52:03 +02:00
|
|
|
logger.trace("refund query not done");
|
2020-04-27 17:41:20 +02:00
|
|
|
}
|
2019-12-15 19:08:07 +01:00
|
|
|
|
2020-04-27 17:41:20 +02:00
|
|
|
await tx.put(Stores.purchases, p);
|
|
|
|
},
|
|
|
|
);
|
2019-12-15 19:08:07 +01:00
|
|
|
|
|
|
|
ws.notify({
|
|
|
|
type: NotificationType.RefundQueried,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:25:45 +02:00
|
|
|
/**
|
|
|
|
* Summary of the refund status of a purchase.
|
|
|
|
*/
|
|
|
|
export interface RefundSummary {
|
|
|
|
pendingAtExchange: boolean;
|
|
|
|
amountEffectivePaid: AmountJson;
|
|
|
|
amountRefundGranted: AmountJson;
|
|
|
|
amountRefundGone: AmountJson;
|
|
|
|
}
|
|
|
|
|
2019-12-15 19:08:07 +01:00
|
|
|
/**
|
|
|
|
* Accept a refund, return the contract hash for the contract
|
|
|
|
* that was involved in the refund.
|
|
|
|
*/
|
|
|
|
export async function applyRefund(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
talerRefundUri: string,
|
2020-08-11 14:25:45 +02:00
|
|
|
): Promise<ApplyRefundResponse> {
|
2019-12-15 19:08:07 +01:00
|
|
|
const parseResult = parseRefundUri(talerRefundUri);
|
|
|
|
|
2020-07-22 10:52:03 +02:00
|
|
|
logger.trace("applying refund", parseResult);
|
2019-12-15 19:08:07 +01:00
|
|
|
|
|
|
|
if (!parseResult) {
|
|
|
|
throw Error("invalid refund URI");
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:25:45 +02:00
|
|
|
let purchase = await ws.db.getIndexed(Stores.purchases.orderIdIndex, [
|
2019-12-15 19:08:07 +01:00
|
|
|
parseResult.merchantBaseUrl,
|
|
|
|
parseResult.orderId,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!purchase) {
|
2020-03-30 12:34:16 +02:00
|
|
|
throw Error(
|
|
|
|
`no purchase for the taler://refund/ URI (${talerRefundUri}) was found`,
|
|
|
|
);
|
2019-12-15 19:08:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-10 13:18:38 +02:00
|
|
|
const proposalId = purchase.proposalId;
|
|
|
|
|
2020-03-30 12:42:28 +02:00
|
|
|
logger.info("processing purchase for refund");
|
2020-08-10 13:18:38 +02:00
|
|
|
const success = await ws.db.runWithWriteTransaction(
|
|
|
|
[Stores.purchases],
|
|
|
|
async (tx) => {
|
|
|
|
const p = await tx.get(Stores.purchases, proposalId);
|
|
|
|
if (!p) {
|
|
|
|
logger.error("no purchase found for refund URL");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
p.refundStatusRequested = true;
|
|
|
|
p.lastRefundStatusError = undefined;
|
|
|
|
p.refundStatusRetryInfo = initRetryInfo();
|
|
|
|
await tx.put(Stores.purchases, p);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
ws.notify({
|
|
|
|
type: NotificationType.RefundStarted,
|
|
|
|
});
|
|
|
|
await processPurchaseQueryRefund(ws, proposalId);
|
|
|
|
}
|
2019-12-15 19:08:07 +01:00
|
|
|
|
2020-08-11 14:25:45 +02:00
|
|
|
purchase = await ws.db.get(Stores.purchases, proposalId);
|
|
|
|
|
|
|
|
if (!purchase) {
|
|
|
|
throw Error("purchase no longer exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
const p = purchase;
|
|
|
|
|
|
|
|
let amountRefundGranted = Amounts.getZero(
|
|
|
|
purchase.contractData.amount.currency,
|
|
|
|
);
|
|
|
|
let amountRefundGone = Amounts.getZero(purchase.contractData.amount.currency);
|
|
|
|
|
|
|
|
let pendingAtExchange = false;
|
|
|
|
|
|
|
|
Object.keys(purchase.refunds).forEach((rk) => {
|
|
|
|
const refund = p.refunds[rk];
|
|
|
|
if (refund.type === RefundState.Pending) {
|
|
|
|
pendingAtExchange = true;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
refund.type === RefundState.Applied ||
|
|
|
|
refund.type === RefundState.Pending
|
|
|
|
) {
|
|
|
|
amountRefundGranted = Amounts.add(
|
|
|
|
amountRefundGranted,
|
|
|
|
Amounts.sub(
|
|
|
|
refund.refundAmount,
|
|
|
|
refund.refundFee,
|
|
|
|
refund.totalRefreshCostBound,
|
|
|
|
).amount,
|
|
|
|
).amount;
|
|
|
|
} else {
|
2020-08-12 09:11:00 +02:00
|
|
|
amountRefundGone = Amounts.add(amountRefundGone, refund.refundAmount)
|
|
|
|
.amount;
|
2020-08-11 14:25:45 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-15 09:23:35 +02:00
|
|
|
return {
|
|
|
|
contractTermsHash: purchase.contractData.contractTermsHash,
|
|
|
|
proposalId: purchase.proposalId,
|
2020-08-11 14:25:45 +02:00
|
|
|
amountEffectivePaid: Amounts.stringify(purchase.payCostInfo.totalCost),
|
|
|
|
amountRefundGone: Amounts.stringify(amountRefundGone),
|
|
|
|
amountRefundGranted: Amounts.stringify(amountRefundGranted),
|
|
|
|
pendingAtExchange,
|
2020-05-15 09:23:35 +02:00
|
|
|
};
|
2019-12-15 19:08:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function processPurchaseQueryRefund(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
proposalId: string,
|
2020-04-06 17:45:41 +02:00
|
|
|
forceNow = false,
|
2019-12-15 19:08:07 +01:00
|
|
|
): Promise<void> {
|
2020-09-01 14:57:22 +02:00
|
|
|
const onOpErr = (e: TalerErrorDetails): Promise<void> =>
|
2019-12-15 19:08:07 +01:00
|
|
|
incrementPurchaseQueryRefundRetry(ws, proposalId, e);
|
|
|
|
await guardOperationException(
|
|
|
|
() => processPurchaseQueryRefundImpl(ws, proposalId, forceNow),
|
|
|
|
onOpErr,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resetPurchaseQueryRefundRetry(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
proposalId: string,
|
2020-04-06 20:02:01 +02:00
|
|
|
): Promise<void> {
|
2020-03-30 12:34:16 +02:00
|
|
|
await ws.db.mutate(Stores.purchases, proposalId, (x) => {
|
2019-12-15 19:08:07 +01:00
|
|
|
if (x.refundStatusRetryInfo.active) {
|
|
|
|
x.refundStatusRetryInfo = initRetryInfo();
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processPurchaseQueryRefundImpl(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
proposalId: string,
|
|
|
|
forceNow: boolean,
|
|
|
|
): Promise<void> {
|
|
|
|
if (forceNow) {
|
|
|
|
await resetPurchaseQueryRefundRetry(ws, proposalId);
|
|
|
|
}
|
|
|
|
const purchase = await ws.db.get(Stores.purchases, proposalId);
|
|
|
|
if (!purchase) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-22 10:52:03 +02:00
|
|
|
|
2019-12-15 19:08:07 +01:00
|
|
|
if (!purchase.refundStatusRequested) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
const requestUrl = new URL(
|
2020-08-20 08:09:34 +02:00
|
|
|
`orders/${purchase.contractData.orderId}/refund`,
|
2020-07-23 14:05:17 +02:00
|
|
|
purchase.contractData.merchantBaseUrl,
|
|
|
|
);
|
2019-12-15 19:08:07 +01:00
|
|
|
|
2020-08-12 17:41:54 +02:00
|
|
|
logger.trace(`making refund request to ${requestUrl.href}`);
|
|
|
|
|
2020-08-20 08:09:34 +02:00
|
|
|
const request = await ws.http.postJson(requestUrl.href, {
|
|
|
|
h_contract: purchase.contractData.contractTermsHash,
|
|
|
|
});
|
2020-07-23 14:05:17 +02:00
|
|
|
|
2020-08-10 13:18:38 +02:00
|
|
|
logger.trace("got json", JSON.stringify(await request.json(), undefined, 2));
|
2020-07-23 14:05:17 +02:00
|
|
|
|
2020-07-22 10:52:03 +02:00
|
|
|
const refundResponse = await readSuccessResponseJsonOrThrow(
|
|
|
|
request,
|
2020-08-20 08:09:34 +02:00
|
|
|
codecForMerchantOrderRefundPickupResponse(),
|
2020-03-30 12:34:16 +02:00
|
|
|
);
|
2020-07-22 10:52:03 +02:00
|
|
|
|
2020-07-23 14:05:17 +02:00
|
|
|
await acceptRefunds(
|
2019-12-15 19:08:07 +01:00
|
|
|
ws,
|
|
|
|
proposalId,
|
2020-07-23 14:05:17 +02:00
|
|
|
refundResponse.refunds,
|
2019-12-15 19:08:07 +01:00
|
|
|
RefundReason.NormalRefund,
|
|
|
|
);
|
|
|
|
}
|