include refund fees in effective refund amount calculation

This commit is contained in:
Florian Dold 2020-05-15 16:39:40 +05:30
parent 3eb88574bc
commit 79d0c2f928
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 45 additions and 0 deletions

View File

@ -456,6 +456,7 @@ async function recordConfirmPay(
refundsDone: {},
refundsFailed: {},
refundsPending: {},
refundsRefreshCost: {},
};
await ws.db.runWithWriteTransaction(

View File

@ -144,6 +144,8 @@ async function acceptRefundResponse(
const unfinishedRefunds: MerchantRefundDetails[] = [];
const failedRefunds: MerchantRefundDetails[] = [];
const refundsRefreshCost: { [refundKey: string]: AmountJson } = {};
for (const rd of refunds) {
if (rd.exchange_http_status === 200) {
// FIXME: also verify signature if necessary.
@ -158,6 +160,33 @@ async function acceptRefundResponse(
}
}
for (const rd of [...finishedRefunds, ...unfinishedRefunds]) {
const key = getRefundKey(rd);
const coin = await ws.db.get(Stores.coins, rd.coin_pub);
if (!coin) {
continue;
}
const denom = await ws.db.getIndexed(
Stores.denominations.denomPubHashIndex,
coin.denomPubHash,
);
if (!denom) {
throw Error("inconsistent database");
}
const amountLeft = Amounts.sub(
Amounts.add(coin.currentAmount, Amounts.parseOrThrow(rd.refund_amount))
.amount,
Amounts.parseOrThrow(rd.refund_fee),
).amount;
const allDenoms = await ws.db
.iterIndex(
Stores.denominations.exchangeBaseUrlIndex,
coin.exchangeBaseUrl,
)
.toArray();
refundsRefreshCost[key] = getTotalRefreshCost(allDenoms, denom, amountLeft);
}
const now = getTimestampNow();
await ws.db.runWithWriteTransaction(
@ -282,6 +311,8 @@ async function acceptRefundResponse(
console.log("refund query not done");
}
p.refundsRefreshCost = {...p.refundsRefreshCost, ...refundsRefreshCost };
await tx.put(Stores.purchases, p);
const coinsPubsToBeRefreshed = Object.values(refreshCoinsMap);

View File

@ -63,6 +63,8 @@ function getRefundStats(
.amount;
}
// Subtract fees from effective refund amount
for (const rk of Object.keys(pr.refundsDone)) {
const perm = pr.refundsDone[rk].perm;
if (pr.refundsDone[rk].refundGroupId !== refundGroupId) {
@ -72,6 +74,12 @@ function getRefundStats(
amountEffective,
Amounts.parseOrThrow(perm.refund_fee),
).amount;
if (pr.refundsRefreshCost[rk]) {
amountEffective = Amounts.sub(
amountEffective,
pr.refundsRefreshCost[rk],
).amount;
}
}
for (const rk of Object.keys(pr.refundsFailed)) {

View File

@ -1269,6 +1269,11 @@ export interface PurchaseRecord {
*/
refundsFailed: { [refundKey: string]: RefundInfo };
/**
* Refresh cost for each refund permission.
*/
refundsRefreshCost: { [refundKey: string]: AmountJson };
/**
* When was the last refund made?
* Set to 0 if no refund was made on the purchase.