2020-05-12 10:38:58 +02:00
|
|
|
/*
|
|
|
|
This file is part of GNU Taler
|
|
|
|
(C) 2019 Taler Systems S.A.
|
|
|
|
|
|
|
|
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/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
|
|
|
import { InternalWalletState } from "./state";
|
2020-05-15 12:33:52 +02:00
|
|
|
import { Stores, ReserveRecordStatus, PurchaseRecord } from "../types/dbTypes";
|
2020-05-12 12:14:48 +02:00
|
|
|
import { Amounts, AmountJson } from "../util/amounts";
|
2020-05-12 10:38:58 +02:00
|
|
|
import { timestampCmp } from "../util/time";
|
|
|
|
import {
|
|
|
|
TransactionsRequest,
|
|
|
|
TransactionsResponse,
|
|
|
|
Transaction,
|
|
|
|
TransactionType,
|
2020-05-12 12:49:40 +02:00
|
|
|
PaymentStatus,
|
2020-05-12 10:38:58 +02:00
|
|
|
} from "../types/transactions";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an event ID from the type and the primary key for the event.
|
|
|
|
*/
|
|
|
|
function makeEventId(type: TransactionType, ...args: string[]): string {
|
|
|
|
return type + ";" + args.map((x) => encodeURIComponent(x)).join(";");
|
|
|
|
}
|
|
|
|
|
2020-05-12 12:14:48 +02:00
|
|
|
interface RefundStats {
|
|
|
|
amountInvalid: AmountJson;
|
|
|
|
amountEffective: AmountJson;
|
|
|
|
amountRaw: AmountJson;
|
|
|
|
}
|
|
|
|
|
2020-05-12 12:49:40 +02:00
|
|
|
function getRefundStats(
|
|
|
|
pr: PurchaseRecord,
|
|
|
|
refundGroupId: string,
|
|
|
|
): RefundStats {
|
2020-05-12 12:14:48 +02:00
|
|
|
let amountEffective = Amounts.getZero(pr.contractData.amount.currency);
|
|
|
|
let amountInvalid = Amounts.getZero(pr.contractData.amount.currency);
|
|
|
|
let amountRaw = Amounts.getZero(pr.contractData.amount.currency);
|
|
|
|
|
|
|
|
for (const rk of Object.keys(pr.refundsDone)) {
|
|
|
|
const perm = pr.refundsDone[rk].perm;
|
|
|
|
if (pr.refundsDone[rk].refundGroupId !== refundGroupId) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-12 12:49:40 +02:00
|
|
|
amountEffective = Amounts.add(
|
|
|
|
amountEffective,
|
|
|
|
Amounts.parseOrThrow(perm.refund_amount),
|
|
|
|
).amount;
|
|
|
|
amountRaw = Amounts.add(amountRaw, Amounts.parseOrThrow(perm.refund_amount))
|
|
|
|
.amount;
|
2020-05-12 12:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const rk of Object.keys(pr.refundsDone)) {
|
|
|
|
const perm = pr.refundsDone[rk].perm;
|
|
|
|
if (pr.refundsDone[rk].refundGroupId !== refundGroupId) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-12 12:49:40 +02:00
|
|
|
amountEffective = Amounts.sub(
|
|
|
|
amountEffective,
|
|
|
|
Amounts.parseOrThrow(perm.refund_fee),
|
|
|
|
).amount;
|
2020-05-12 12:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const rk of Object.keys(pr.refundsFailed)) {
|
|
|
|
const perm = pr.refundsDone[rk].perm;
|
|
|
|
if (pr.refundsDone[rk].refundGroupId !== refundGroupId) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-12 12:49:40 +02:00
|
|
|
amountInvalid = Amounts.add(
|
|
|
|
amountInvalid,
|
|
|
|
Amounts.parseOrThrow(perm.refund_fee),
|
|
|
|
).amount;
|
2020-05-12 10:38:58 +02:00
|
|
|
}
|
2020-05-12 12:14:48 +02:00
|
|
|
|
2020-05-12 10:38:58 +02:00
|
|
|
return {
|
2020-05-12 12:14:48 +02:00
|
|
|
amountEffective,
|
|
|
|
amountInvalid,
|
|
|
|
amountRaw,
|
2020-05-12 12:49:40 +02:00
|
|
|
};
|
2020-05-12 10:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrive the full event history for this wallet.
|
|
|
|
*/
|
|
|
|
export async function getTransactions(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
transactionsRequest?: TransactionsRequest,
|
|
|
|
): Promise<TransactionsResponse> {
|
|
|
|
const transactions: Transaction[] = [];
|
|
|
|
|
|
|
|
await ws.db.runWithReadTransaction(
|
|
|
|
[
|
|
|
|
Stores.currencies,
|
|
|
|
Stores.coins,
|
|
|
|
Stores.denominations,
|
|
|
|
Stores.proposals,
|
|
|
|
Stores.purchases,
|
|
|
|
Stores.refreshGroups,
|
|
|
|
Stores.reserves,
|
|
|
|
Stores.reserveHistory,
|
|
|
|
Stores.tips,
|
|
|
|
Stores.withdrawalGroups,
|
|
|
|
Stores.payEvents,
|
|
|
|
Stores.planchets,
|
|
|
|
Stores.refundEvents,
|
|
|
|
Stores.reserveUpdatedEvents,
|
|
|
|
Stores.recoupGroups,
|
|
|
|
],
|
|
|
|
async (tx) => {
|
|
|
|
tx.iter(Stores.withdrawalGroups).forEach((wsr) => {
|
2020-05-12 12:14:48 +02:00
|
|
|
if (
|
|
|
|
transactionsRequest?.currency &&
|
|
|
|
wsr.rawWithdrawalAmount.currency != transactionsRequest.currency
|
|
|
|
) {
|
|
|
|
return;
|
2020-05-12 10:38:58 +02:00
|
|
|
}
|
2020-05-12 12:14:48 +02:00
|
|
|
if (wsr.rawWithdrawalAmount.currency)
|
|
|
|
if (wsr.timestampFinish) {
|
|
|
|
transactions.push({
|
|
|
|
type: TransactionType.Withdrawal,
|
2020-05-15 12:33:52 +02:00
|
|
|
amountEffective: Amounts.stringify(wsr.denomsSel.totalCoinValue),
|
|
|
|
amountRaw: Amounts.stringify(wsr.denomsSel.totalWithdrawCost),
|
2020-05-12 12:14:48 +02:00
|
|
|
confirmed: true,
|
|
|
|
exchangeBaseUrl: wsr.exchangeBaseUrl,
|
|
|
|
pending: !wsr.timestampFinish,
|
|
|
|
timestamp: wsr.timestampStart,
|
|
|
|
transactionId: makeEventId(
|
|
|
|
TransactionType.Withdrawal,
|
|
|
|
wsr.withdrawalGroupId,
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
2020-05-12 10:38:58 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
tx.iter(Stores.reserves).forEach((r) => {
|
2020-05-12 12:14:48 +02:00
|
|
|
if (
|
|
|
|
transactionsRequest?.currency &&
|
|
|
|
r.currency != transactionsRequest.currency
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-12 10:38:58 +02:00
|
|
|
if (r.reserveStatus !== ReserveRecordStatus.WAIT_CONFIRM_BANK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!r.bankInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
transactions.push({
|
|
|
|
type: TransactionType.Withdrawal,
|
|
|
|
confirmed: false,
|
2020-05-15 12:33:52 +02:00
|
|
|
amountRaw: Amounts.stringify(r.bankInfo.denomSel.totalWithdrawCost),
|
|
|
|
amountEffective: Amounts.stringify(r.bankInfo.denomSel.totalCoinValue),
|
|
|
|
exchangeBaseUrl: r.exchangeBaseUrl,
|
2020-05-12 10:38:58 +02:00
|
|
|
pending: true,
|
|
|
|
timestamp: r.timestampCreated,
|
|
|
|
bankConfirmationUrl: r.bankInfo.confirmUrl,
|
|
|
|
transactionId: makeEventId(
|
|
|
|
TransactionType.Withdrawal,
|
|
|
|
r.bankInfo.bankWithdrawalGroupId,
|
|
|
|
),
|
|
|
|
});
|
|
|
|
});
|
2020-05-12 12:14:48 +02:00
|
|
|
|
|
|
|
tx.iter(Stores.purchases).forEachAsync(async (pr) => {
|
|
|
|
if (
|
|
|
|
transactionsRequest?.currency &&
|
|
|
|
pr.contractData.amount.currency != transactionsRequest.currency
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const proposal = await tx.get(Stores.proposals, pr.proposalId);
|
|
|
|
if (!proposal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
transactions.push({
|
|
|
|
type: TransactionType.Payment,
|
|
|
|
amountRaw: Amounts.stringify(pr.contractData.amount),
|
2020-05-12 12:34:28 +02:00
|
|
|
amountEffective: Amounts.stringify(pr.payCostInfo.totalCost),
|
2020-05-12 12:49:40 +02:00
|
|
|
status: pr.timestampFirstSuccessfulPay
|
|
|
|
? PaymentStatus.Paid
|
|
|
|
: PaymentStatus.Accepted,
|
2020-05-12 12:14:48 +02:00
|
|
|
pending: !pr.timestampFirstSuccessfulPay,
|
|
|
|
timestamp: pr.timestampAccept,
|
|
|
|
transactionId: makeEventId(TransactionType.Payment, pr.proposalId),
|
|
|
|
info: {
|
|
|
|
fulfillmentUrl: pr.contractData.fulfillmentUrl,
|
2020-05-15 12:33:52 +02:00
|
|
|
merchant: pr.contractData.merchant,
|
2020-05-12 12:14:48 +02:00
|
|
|
orderId: pr.contractData.orderId,
|
2020-05-15 12:33:52 +02:00
|
|
|
products: pr.contractData.products,
|
2020-05-12 12:14:48 +02:00
|
|
|
summary: pr.contractData.summary,
|
2020-05-15 12:33:52 +02:00
|
|
|
summary_i18n: pr.contractData.summaryI18n,
|
2020-05-12 12:14:48 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const rg of pr.refundGroups) {
|
|
|
|
const pending = Object.keys(pr.refundsDone).length > 0;
|
|
|
|
|
|
|
|
const stats = getRefundStats(pr, rg.refundGroupId);
|
2020-05-12 12:49:40 +02:00
|
|
|
|
2020-05-12 12:14:48 +02:00
|
|
|
transactions.push({
|
|
|
|
type: TransactionType.Refund,
|
|
|
|
pending,
|
|
|
|
info: {
|
|
|
|
fulfillmentUrl: pr.contractData.fulfillmentUrl,
|
|
|
|
merchant: {},
|
|
|
|
orderId: pr.contractData.orderId,
|
|
|
|
products: [],
|
|
|
|
summary: pr.contractData.summary,
|
|
|
|
summary_i18n: {},
|
|
|
|
},
|
|
|
|
timestamp: rg.timestampQueried,
|
2020-05-12 12:49:40 +02:00
|
|
|
transactionId: makeEventId(
|
|
|
|
TransactionType.Refund,
|
2020-05-15 12:33:52 +02:00
|
|
|
pr.proposalId,
|
|
|
|
`${rg.timestampQueried.t_ms}`,
|
2020-05-12 12:49:40 +02:00
|
|
|
),
|
|
|
|
refundedTransactionId: makeEventId(
|
|
|
|
TransactionType.Payment,
|
|
|
|
pr.proposalId,
|
|
|
|
),
|
2020-05-12 12:14:48 +02:00
|
|
|
amountEffective: Amounts.stringify(stats.amountEffective),
|
|
|
|
amountInvalid: Amounts.stringify(stats.amountInvalid),
|
|
|
|
amountRaw: Amounts.stringify(stats.amountRaw),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-05-12 10:38:58 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
transactions.sort((h1, h2) => timestampCmp(h1.timestamp, h2.timestamp));
|
|
|
|
|
|
|
|
return { transactions };
|
|
|
|
}
|