wallet-core/packages/taler-wallet-core/src/operations/transactions.ts

1263 lines
38 KiB
TypeScript
Raw Normal View History

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 {
2022-03-18 15:32:41 +01:00
AbsoluteTime,
2022-09-16 17:04:32 +02:00
AmountJson,
Amounts,
constructPayPullUri,
constructPayPushUri,
ExtendedStatus,
Logger,
2022-09-16 17:04:32 +02:00
OrderShortInfo,
PaymentStatus,
2022-11-02 17:42:14 +01:00
PeerContractTerms,
RefundInfoShort,
TalerErrorCode,
TalerProtocolTimestamp,
Transaction,
TransactionByIdRequest,
TransactionsRequest,
TransactionsResponse,
TransactionType,
2022-09-16 17:04:32 +02:00
WithdrawalType,
} from "@gnu-taler/taler-util";
import {
DepositGroupRecord,
ExchangeDetailsRecord,
OperationRetryRecord,
PeerPullPaymentIncomingRecord,
PeerPushPaymentInitiationRecord,
2022-10-08 23:45:49 +02:00
PurchaseStatus,
PurchaseRecord,
RefundState,
TipRecord,
WalletRefundItem,
WithdrawalGroupRecord,
2022-09-16 17:04:32 +02:00
WithdrawalRecordType,
WalletContractData,
2022-11-02 17:42:14 +01:00
PeerPushPaymentInitiationStatus,
PeerPullPaymentIncomingStatus,
2023-01-15 21:48:41 +01:00
TransactionStatus,
2021-03-17 17:56:37 +01:00
} from "../db.js";
2022-09-14 13:37:33 +02:00
import { InternalWalletState } from "../internal-wallet-state.js";
2022-11-02 12:50:34 +01:00
import { assertUnreachable } from "../util/assertUnreachable.js";
import { checkDbInvariant } from "../util/invariants.js";
2022-09-14 13:37:33 +02:00
import { RetryTags } from "../util/retries.js";
2022-10-17 18:36:39 +02:00
import {
makeTombstoneId,
makeTransactionId,
2022-10-17 18:50:17 +02:00
parseId,
2022-10-17 18:36:39 +02:00
TombstoneTag,
} from "./common.js";
import { processDepositGroup } from "./deposits.js";
import { getExchangeDetails } from "./exchanges.js";
import {
abortPay,
expectProposalDownload,
extractContractData,
processPurchasePay,
} from "./pay-merchant.js";
2021-06-14 19:37:35 +02:00
import { processRefreshGroup } from "./refresh.js";
import { processTip } from "./tip.js";
import {
augmentPaytoUrisForWithdrawal,
processWithdrawalGroup,
} from "./withdraw.js";
2020-05-12 10:38:58 +02:00
const logger = new Logger("taler-wallet-core:transactions.ts");
2020-05-15 13:28:15 +02:00
function shouldSkipCurrency(
transactionsRequest: TransactionsRequest | undefined,
currency: string,
): boolean {
if (!transactionsRequest?.currency) {
return false;
}
return transactionsRequest.currency.toLowerCase() !== currency.toLowerCase();
}
function shouldSkipSearch(
transactionsRequest: TransactionsRequest | undefined,
fields: string[],
): boolean {
if (!transactionsRequest?.search) {
return false;
}
const needle = transactionsRequest.search.trim();
for (const f of fields) {
if (f.indexOf(needle) >= 0) {
return false;
}
}
return true;
}
/**
* Fallback order of transactions that have the same timestamp.
*/
const txOrder: { [t in TransactionType]: number } = {
[TransactionType.Withdrawal]: 1,
[TransactionType.Tip]: 2,
[TransactionType.Payment]: 3,
[TransactionType.PeerPullCredit]: 4,
[TransactionType.PeerPullDebit]: 5,
[TransactionType.PeerPushCredit]: 6,
[TransactionType.PeerPushDebit]: 7,
[TransactionType.Refund]: 8,
[TransactionType.Deposit]: 9,
[TransactionType.Refresh]: 10,
[TransactionType.Tip]: 11,
};
export async function getTransactionById(
ws: InternalWalletState,
req: TransactionByIdRequest,
): Promise<Transaction> {
2022-10-17 18:50:17 +02:00
const { type, args: rest } = parseId("txn", req.transactionId);
if (
type === TransactionType.Withdrawal ||
type === TransactionType.PeerPullCredit ||
type === TransactionType.PeerPushCredit
) {
const withdrawalGroupId = rest[0];
return await ws.db
2022-09-16 17:04:32 +02:00
.mktx((x) => [
x.withdrawalGroups,
x.exchangeDetails,
x.exchanges,
x.operationRetries,
])
.runReadWrite(async (tx) => {
const withdrawalGroupRecord = await tx.withdrawalGroups.get(
withdrawalGroupId,
);
2022-09-16 17:04:32 +02:00
if (!withdrawalGroupRecord) throw Error("not found");
const opId = RetryTags.forWithdrawal(withdrawalGroupRecord);
const ort = await tx.operationRetries.get(opId);
2022-09-16 17:04:32 +02:00
if (
withdrawalGroupRecord.wgInfo.withdrawalType ===
WithdrawalRecordType.BankIntegrated
) {
return buildTransactionForBankIntegratedWithdraw(
withdrawalGroupRecord,
ort,
);
}
2022-09-16 17:04:32 +02:00
if (
withdrawalGroupRecord.wgInfo.withdrawalType ===
WithdrawalRecordType.PeerPullCredit
) {
return buildTransactionForPullPaymentCredit(
withdrawalGroupRecord,
ort,
);
}
2022-09-16 17:04:32 +02:00
if (
withdrawalGroupRecord.wgInfo.withdrawalType ===
WithdrawalRecordType.PeerPushCredit
) {
return buildTransactionForPushPaymentCredit(
withdrawalGroupRecord,
ort,
);
}
2022-09-16 17:04:32 +02:00
const exchangeDetails = await getExchangeDetails(
tx,
withdrawalGroupRecord.exchangeBaseUrl,
);
if (!exchangeDetails) throw Error("not exchange details");
2022-09-16 17:04:32 +02:00
return buildTransactionForManualWithdraw(
withdrawalGroupRecord,
exchangeDetails,
ort,
);
});
} else if (type === TransactionType.Payment) {
const proposalId = rest[0];
return await ws.db
.mktx((x) => [
x.purchases,
x.tombstones,
x.operationRetries,
x.contractTerms,
])
.runReadWrite(async (tx) => {
const purchase = await tx.purchases.get(proposalId);
2022-09-16 17:04:32 +02:00
if (!purchase) throw Error("not found");
const filteredRefunds = await Promise.all(
Object.values(purchase.refunds).map(async (r) => {
const t = await tx.tombstones.get(
makeTombstoneId(
2022-09-16 17:04:32 +02:00
TombstoneTag.DeleteRefund,
purchase.proposalId,
`${r.executionTime.t_s}`,
),
);
if (!t) return r;
return undefined;
}),
);
2022-10-21 15:11:41 +02:00
const download = await expectProposalDownload(ws, purchase, tx);
2022-09-16 17:04:32 +02:00
const cleanRefunds = filteredRefunds.filter(
(x): x is WalletRefundItem => !!x,
);
2022-10-21 15:11:41 +02:00
const contractData = download.contractData;
2022-09-16 17:04:32 +02:00
const refunds = mergeRefundByExecutionTime(
cleanRefunds,
2022-11-02 17:42:14 +01:00
Amounts.zeroOfAmount(contractData.amount),
2022-09-16 17:04:32 +02:00
);
const payOpId = RetryTags.forPay(purchase);
const payRetryRecord = await tx.operationRetries.get(payOpId);
return buildTransactionForPurchase(
purchase,
contractData,
refunds,
payRetryRecord,
);
});
} else if (type === TransactionType.Refresh) {
const refreshGroupId = rest[0];
throw Error(`no tx for refresh`);
} else if (type === TransactionType.Tip) {
const tipId = rest[0];
return await ws.db
.mktx((x) => [x.tips, x.operationRetries])
.runReadWrite(async (tx) => {
const tipRecord = await tx.tips.get(tipId);
2022-09-16 17:04:32 +02:00
if (!tipRecord) throw Error("not found");
2022-09-16 17:04:32 +02:00
const retries = await tx.operationRetries.get(
RetryTags.forTipPickup(tipRecord),
);
return buildTransactionForTip(tipRecord, retries);
});
} else if (type === TransactionType.Deposit) {
const depositGroupId = rest[0];
return await ws.db
.mktx((x) => [x.depositGroups, x.operationRetries])
.runReadWrite(async (tx) => {
const depositRecord = await tx.depositGroups.get(depositGroupId);
2022-09-16 17:04:32 +02:00
if (!depositRecord) throw Error("not found");
2022-09-16 17:04:32 +02:00
const retries = await tx.operationRetries.get(
RetryTags.forDeposit(depositRecord),
);
return buildTransactionForDeposit(depositRecord, retries);
});
} else if (type === TransactionType.Refund) {
const proposalId = rest[0];
const executionTimeStr = rest[1];
return await ws.db
.mktx((x) => [
x.operationRetries,
x.purchases,
x.tombstones,
x.contractTerms,
])
.runReadWrite(async (tx) => {
const purchase = await tx.purchases.get(proposalId);
2022-09-16 17:04:32 +02:00
if (!purchase) throw Error("not found");
2022-09-16 17:04:32 +02:00
const theRefund = Object.values(purchase.refunds).find(
(r) => `${r.executionTime.t_s}` === executionTimeStr,
);
if (!theRefund) throw Error("not found");
2022-09-16 17:04:32 +02:00
const t = await tx.tombstones.get(
makeTombstoneId(
2022-09-16 17:04:32 +02:00
TombstoneTag.DeleteRefund,
purchase.proposalId,
executionTimeStr,
),
);
if (t) throw Error("deleted");
2022-10-21 15:11:41 +02:00
const download = await expectProposalDownload(ws, purchase, tx);
const contractData = download.contractData;
2022-09-16 17:04:32 +02:00
const refunds = mergeRefundByExecutionTime(
[theRefund],
2022-11-02 17:42:14 +01:00
Amounts.zeroOfAmount(contractData.amount),
2022-09-16 17:04:32 +02:00
);
return buildTransactionForRefund(
purchase,
contractData,
refunds[0],
undefined,
);
});
} else if (type === TransactionType.PeerPullDebit) {
const peerPullPaymentIncomingId = rest[0];
return await ws.db
.mktx((x) => [x.peerPullPaymentIncoming])
.runReadWrite(async (tx) => {
const debit = await tx.peerPullPaymentIncoming.get(
peerPullPaymentIncomingId,
);
if (!debit) throw Error("not found");
2022-09-16 17:04:32 +02:00
return buildTransactionForPullPaymentDebit(debit);
});
} else if (type === TransactionType.PeerPushDebit) {
const pursePub = rest[0];
return await ws.db
2022-11-02 17:42:14 +01:00
.mktx((x) => [x.peerPushPaymentInitiations, x.contractTerms])
.runReadWrite(async (tx) => {
const debit = await tx.peerPushPaymentInitiations.get(pursePub);
if (!debit) throw Error("not found");
2022-11-02 17:42:14 +01:00
const ct = await tx.contractTerms.get(debit.contractTermsHash);
checkDbInvariant(!!ct);
return buildTransactionForPushPaymentDebit(debit, ct.contractTermsRaw);
});
} else {
const unknownTxType: never = type;
throw Error(`can't delete a '${unknownTxType}' transaction`);
}
}
2022-09-16 17:04:32 +02:00
function buildTransactionForPushPaymentDebit(
pi: PeerPushPaymentInitiationRecord,
2022-11-02 17:42:14 +01:00
contractTerms: PeerContractTerms,
2022-09-16 17:04:32 +02:00
ort?: OperationRetryRecord,
): Transaction {
return {
type: TransactionType.PeerPushDebit,
amountEffective: pi.totalCost,
amountRaw: pi.amount,
exchangeBaseUrl: pi.exchangeBaseUrl,
info: {
2022-11-02 17:42:14 +01:00
expiration: contractTerms.purse_expiration,
summary: contractTerms.summary,
},
frozen: false,
extendedStatus:
pi.status != PeerPushPaymentInitiationStatus.PurseCreated
? ExtendedStatus.Pending
: ExtendedStatus.Done,
2022-11-02 17:42:14 +01:00
pending: pi.status != PeerPushPaymentInitiationStatus.PurseCreated,
timestamp: pi.timestampCreated,
talerUri: constructPayPushUri({
exchangeBaseUrl: pi.exchangeBaseUrl,
contractPriv: pi.contractPriv,
}),
transactionId: makeTransactionId(
TransactionType.PeerPushDebit,
pi.pursePub,
),
...(ort?.lastError ? { error: ort.lastError } : {}),
};
}
2022-09-16 17:04:32 +02:00
function buildTransactionForPullPaymentDebit(
pi: PeerPullPaymentIncomingRecord,
ort?: OperationRetryRecord,
): Transaction {
return {
type: TransactionType.PeerPullDebit,
amountEffective: Amounts.stringify(pi.contractTerms.amount),
amountRaw: Amounts.stringify(pi.contractTerms.amount),
exchangeBaseUrl: pi.exchangeBaseUrl,
frozen: false,
pending: false,
extendedStatus: ExtendedStatus.Done,
info: {
expiration: pi.contractTerms.purse_expiration,
summary: pi.contractTerms.summary,
},
timestamp: pi.timestampCreated,
transactionId: makeTransactionId(
TransactionType.PeerPullDebit,
pi.peerPullPaymentIncomingId,
),
...(ort?.lastError ? { error: ort.lastError } : {}),
2022-09-16 17:04:32 +02:00
};
}
2022-09-16 17:04:32 +02:00
function buildTransactionForPullPaymentCredit(
wsr: WithdrawalGroupRecord,
ort?: OperationRetryRecord,
): Transaction {
if (wsr.wgInfo.withdrawalType !== WithdrawalRecordType.PeerPullCredit) {
throw Error(`Unexpected withdrawalType: ${wsr.wgInfo.withdrawalType}`);
}
/**
* FIXME: this should be handled in the withdrawal process.
* PeerPull withdrawal fails until reserve have funds but it is not
* an error from the user perspective.
*/
const silentWithdrawalErrorForInvoice =
ort?.lastError &&
ort.lastError.code === TalerErrorCode.WALLET_WITHDRAWAL_GROUP_INCOMPLETE &&
Object.values(ort.lastError.errorsPerCoin ?? {}).every((e) => {
return (
e.code === TalerErrorCode.WALLET_UNEXPECTED_REQUEST_ERROR &&
e.httpStatusCode === 409
);
});
return {
type: TransactionType.PeerPullCredit,
amountEffective: Amounts.stringify(wsr.denomsSel.totalCoinValue),
amountRaw: Amounts.stringify(wsr.instructedAmount),
exchangeBaseUrl: wsr.exchangeBaseUrl,
2023-01-18 16:36:49 +01:00
extendedStatus: wsr.timestampFinish
? ExtendedStatus.Done
: ExtendedStatus.Pending,
pending: !wsr.timestampFinish,
timestamp: wsr.timestampStart,
info: {
expiration: wsr.wgInfo.contractTerms.purse_expiration,
summary: wsr.wgInfo.contractTerms.summary,
},
talerUri: constructPayPullUri({
exchangeBaseUrl: wsr.exchangeBaseUrl,
contractPriv: wsr.wgInfo.contractPriv,
}),
transactionId: makeTransactionId(
TransactionType.PeerPullCredit,
wsr.withdrawalGroupId,
),
frozen: false,
...(ort?.lastError
? { error: silentWithdrawalErrorForInvoice ? undefined : ort.lastError }
: {}),
2022-09-16 17:04:32 +02:00
};
}
2022-09-16 17:04:32 +02:00
function buildTransactionForPushPaymentCredit(
wsr: WithdrawalGroupRecord,
ort?: OperationRetryRecord,
): Transaction {
if (wsr.wgInfo.withdrawalType !== WithdrawalRecordType.PeerPushCredit)
throw Error("");
return {
type: TransactionType.PeerPushCredit,
amountEffective: Amounts.stringify(wsr.denomsSel.totalCoinValue),
amountRaw: Amounts.stringify(wsr.instructedAmount),
exchangeBaseUrl: wsr.exchangeBaseUrl,
info: {
expiration: wsr.wgInfo.contractTerms.purse_expiration,
summary: wsr.wgInfo.contractTerms.summary,
},
extendedStatus: wsr.timestampFinish
? ExtendedStatus.Done
: ExtendedStatus.Pending,
pending: !wsr.timestampFinish,
timestamp: wsr.timestampStart,
transactionId: makeTransactionId(
TransactionType.PeerPushCredit,
wsr.withdrawalGroupId,
),
frozen: false,
...(ort?.lastError ? { error: ort.lastError } : {}),
2022-09-16 17:04:32 +02:00
};
}
2022-09-16 17:04:32 +02:00
function buildTransactionForBankIntegratedWithdraw(
wsr: WithdrawalGroupRecord,
ort?: OperationRetryRecord,
): Transaction {
if (wsr.wgInfo.withdrawalType !== WithdrawalRecordType.BankIntegrated)
throw Error("");
return {
type: TransactionType.Withdrawal,
amountEffective: Amounts.stringify(wsr.denomsSel.totalCoinValue),
amountRaw: Amounts.stringify(wsr.instructedAmount),
withdrawalDetails: {
type: WithdrawalType.TalerBankIntegrationApi,
2022-09-16 17:04:32 +02:00
confirmed: wsr.wgInfo.bankInfo.timestampBankConfirmed ? true : false,
reservePub: wsr.reservePub,
bankConfirmationUrl: wsr.wgInfo.bankInfo.confirmUrl,
},
exchangeBaseUrl: wsr.exchangeBaseUrl,
extendedStatus: wsr.timestampFinish
? ExtendedStatus.Done
: ExtendedStatus.Pending,
pending: !wsr.timestampFinish,
timestamp: wsr.timestampStart,
transactionId: makeTransactionId(
TransactionType.Withdrawal,
wsr.withdrawalGroupId,
),
frozen: false,
...(ort?.lastError ? { error: ort.lastError } : {}),
2022-09-16 17:04:32 +02:00
};
}
2022-09-16 17:04:32 +02:00
function buildTransactionForManualWithdraw(
withdrawalGroup: WithdrawalGroupRecord,
2022-09-16 17:04:32 +02:00
exchangeDetails: ExchangeDetailsRecord,
ort?: OperationRetryRecord,
): Transaction {
if (withdrawalGroup.wgInfo.withdrawalType !== WithdrawalRecordType.BankManual)
2022-09-16 17:04:32 +02:00
throw Error("");
const plainPaytoUris =
exchangeDetails.wireInfo?.accounts.map((x) => x.payto_uri) ?? [];
const exchangePaytoUris = augmentPaytoUrisForWithdrawal(
plainPaytoUris,
withdrawalGroup.reservePub,
withdrawalGroup.instructedAmount,
);
return {
type: TransactionType.Withdrawal,
amountEffective: Amounts.stringify(
withdrawalGroup.denomsSel.totalCoinValue,
),
amountRaw: Amounts.stringify(withdrawalGroup.instructedAmount),
withdrawalDetails: {
type: WithdrawalType.ManualTransfer,
reservePub: withdrawalGroup.reservePub,
exchangePaytoUris,
},
exchangeBaseUrl: withdrawalGroup.exchangeBaseUrl,
extendedStatus: withdrawalGroup.timestampFinish
? ExtendedStatus.Done
: ExtendedStatus.Pending,
pending: !withdrawalGroup.timestampFinish,
timestamp: withdrawalGroup.timestampStart,
transactionId: makeTransactionId(
TransactionType.Withdrawal,
withdrawalGroup.withdrawalGroupId,
),
frozen: false,
...(ort?.lastError ? { error: ort.lastError } : {}),
2022-09-16 17:04:32 +02:00
};
}
2022-09-16 17:04:32 +02:00
function buildTransactionForDeposit(
dg: DepositGroupRecord,
ort?: OperationRetryRecord,
): Transaction {
return {
type: TransactionType.Deposit,
amountRaw: Amounts.stringify(dg.effectiveDepositAmount),
amountEffective: Amounts.stringify(dg.totalPayCost),
extendedStatus: dg.timestampFinished
? ExtendedStatus.Done
: ExtendedStatus.Pending,
pending: !dg.timestampFinished,
frozen: false,
timestamp: dg.timestampCreated,
targetPaytoUri: dg.wire.payto_uri,
2022-11-17 21:07:24 +01:00
wireTransferDeadline: dg.contractTermsRaw.wire_transfer_deadline,
transactionId: makeTransactionId(
TransactionType.Deposit,
dg.depositGroupId,
),
2023-01-15 21:48:41 +01:00
wireTransferProgress:
(100 *
dg.transactionPerCoin.reduce(
(prev, cur) => prev + (cur === TransactionStatus.Wired ? 1 : 0),
0,
)) /
dg.transactionPerCoin.length,
depositGroupId: dg.depositGroupId,
...(ort?.lastError ? { error: ort.lastError } : {}),
2022-09-16 17:04:32 +02:00
};
}
2022-09-16 17:04:32 +02:00
function buildTransactionForTip(
tipRecord: TipRecord,
ort?: OperationRetryRecord,
): Transaction {
if (!tipRecord.acceptedTimestamp) throw Error("");
return {
type: TransactionType.Tip,
amountEffective: Amounts.stringify(tipRecord.tipAmountEffective),
amountRaw: Amounts.stringify(tipRecord.tipAmountRaw),
extendedStatus: tipRecord.pickedUpTimestamp
? ExtendedStatus.Done
: ExtendedStatus.Pending,
pending: !tipRecord.pickedUpTimestamp,
frozen: false,
timestamp: tipRecord.acceptedTimestamp,
transactionId: makeTransactionId(
TransactionType.Tip,
tipRecord.walletTipId,
),
merchantBaseUrl: tipRecord.merchantBaseUrl,
...(ort?.lastError ? { error: ort.lastError } : {}),
2022-09-16 17:04:32 +02:00
};
}
/**
* For a set of refund with the same executionTime.
*/
interface MergedRefundInfo {
executionTime: TalerProtocolTimestamp;
amountAppliedRaw: AmountJson;
amountAppliedEffective: AmountJson;
firstTimestamp: TalerProtocolTimestamp;
}
2022-09-16 17:04:32 +02:00
function mergeRefundByExecutionTime(
rs: WalletRefundItem[],
zero: AmountJson,
): MergedRefundInfo[] {
const refundByExecTime = rs.reduce((prev, refund) => {
const key = `${refund.executionTime.t_s}`;
// refunds count if applied
2022-09-16 17:04:32 +02:00
const effective =
refund.type === RefundState.Applied
? Amounts.sub(
refund.refundAmount,
refund.refundFee,
refund.totalRefreshCostBound,
).amount
: zero;
const raw =
refund.type === RefundState.Applied ? refund.refundAmount : zero;
const v = prev.get(key);
if (!v) {
prev.set(key, {
executionTime: refund.executionTime,
amountAppliedEffective: effective,
2022-11-02 17:42:14 +01:00
amountAppliedRaw: Amounts.parseOrThrow(raw),
2022-09-16 17:04:32 +02:00
firstTimestamp: refund.obtainedTime,
});
} else {
//v.executionTime is the same
2022-09-16 17:04:32 +02:00
v.amountAppliedEffective = Amounts.add(
v.amountAppliedEffective,
effective,
).amount;
v.amountAppliedRaw = Amounts.add(
v.amountAppliedRaw,
refund.refundAmount,
).amount;
2022-09-16 17:04:32 +02:00
v.firstTimestamp = TalerProtocolTimestamp.min(
v.firstTimestamp,
refund.obtainedTime,
);
}
2022-09-16 17:04:32 +02:00
return prev;
}, new Map<string, MergedRefundInfo>());
return Array.from(refundByExecTime.values());
}
async function buildTransactionForRefund(
2022-09-16 17:04:32 +02:00
purchaseRecord: PurchaseRecord,
contractData: WalletContractData,
2022-09-16 17:04:32 +02:00
refundInfo: MergedRefundInfo,
ort?: OperationRetryRecord,
): Promise<Transaction> {
const info: OrderShortInfo = {
merchant: contractData.merchant,
orderId: contractData.orderId,
products: contractData.products,
summary: contractData.summary,
summary_i18n: contractData.summaryI18n,
contractTermsHash: contractData.contractTermsHash,
};
if (contractData.fulfillmentUrl !== "") {
info.fulfillmentUrl = contractData.fulfillmentUrl;
}
return {
type: TransactionType.Refund,
info,
refundedTransactionId: makeTransactionId(
TransactionType.Payment,
purchaseRecord.proposalId,
),
transactionId: makeTransactionId(
TransactionType.Refund,
purchaseRecord.proposalId,
`${refundInfo.executionTime.t_s}`,
),
timestamp: refundInfo.firstTimestamp,
amountEffective: Amounts.stringify(refundInfo.amountAppliedEffective),
amountRaw: Amounts.stringify(refundInfo.amountAppliedRaw),
refundPending:
purchaseRecord.refundAmountAwaiting === undefined
? undefined
: Amounts.stringify(purchaseRecord.refundAmountAwaiting),
extendedStatus: ExtendedStatus.Done,
pending: false,
frozen: false,
...(ort?.lastError ? { error: ort.lastError } : {}),
2022-09-16 17:04:32 +02:00
};
}
async function buildTransactionForPurchase(
2022-09-16 17:04:32 +02:00
purchaseRecord: PurchaseRecord,
contractData: WalletContractData,
2022-09-16 17:04:32 +02:00
refundsInfo: MergedRefundInfo[],
ort?: OperationRetryRecord,
): Promise<Transaction> {
2022-11-02 17:42:14 +01:00
const zero = Amounts.zeroOfAmount(contractData.amount);
const info: OrderShortInfo = {
merchant: contractData.merchant,
orderId: contractData.orderId,
products: contractData.products,
summary: contractData.summary,
summary_i18n: contractData.summaryI18n,
contractTermsHash: contractData.contractTermsHash,
};
if (contractData.fulfillmentUrl !== "") {
info.fulfillmentUrl = contractData.fulfillmentUrl;
}
2022-09-16 17:04:32 +02:00
const totalRefund = refundsInfo.reduce(
(prev, cur) => {
return {
raw: Amounts.add(prev.raw, cur.amountAppliedRaw).amount,
effective: Amounts.add(prev.effective, cur.amountAppliedEffective)
.amount,
};
},
{
raw: zero,
effective: zero,
} as { raw: AmountJson; effective: AmountJson },
);
2022-09-16 17:04:32 +02:00
const refunds: RefundInfoShort[] = refundsInfo.map((r) => ({
amountEffective: Amounts.stringify(r.amountAppliedEffective),
amountRaw: Amounts.stringify(r.amountAppliedRaw),
timestamp: r.executionTime,
transactionId: makeTransactionId(
TransactionType.Refund,
purchaseRecord.proposalId,
2022-09-16 17:04:32 +02:00
`${r.executionTime.t_s}`,
),
2022-09-16 17:04:32 +02:00
}));
const timestamp = purchaseRecord.timestampAccept;
checkDbInvariant(!!timestamp);
checkDbInvariant(!!purchaseRecord.payInfo);
let status: ExtendedStatus;
switch (purchaseRecord.purchaseStatus) {
case PurchaseStatus.AbortingWithRefund:
status = ExtendedStatus.Aborting;
break;
case PurchaseStatus.Paid:
case PurchaseStatus.RepurchaseDetected:
status = ExtendedStatus.Done;
break;
case PurchaseStatus.DownloadingProposal:
case PurchaseStatus.QueryingRefund:
case PurchaseStatus.Proposed:
case PurchaseStatus.Paying:
status = ExtendedStatus.Pending;
break;
case PurchaseStatus.ProposalDownloadFailed:
status = ExtendedStatus.Failed;
break;
case PurchaseStatus.PaymentAbortFinished:
status = ExtendedStatus.Aborted;
break;
default:
// FIXME: Should we have some unknown status?
status = ExtendedStatus.Pending;
}
return {
type: TransactionType.Payment,
amountRaw: Amounts.stringify(contractData.amount),
amountEffective: Amounts.stringify(purchaseRecord.payInfo.totalPayCost),
totalRefundRaw: Amounts.stringify(totalRefund.raw),
totalRefundEffective: Amounts.stringify(totalRefund.effective),
refundPending:
purchaseRecord.refundAmountAwaiting === undefined
? undefined
: Amounts.stringify(purchaseRecord.refundAmountAwaiting),
status: purchaseRecord.timestampFirstSuccessfulPay
? PaymentStatus.Paid
: PaymentStatus.Accepted,
extendedStatus: status,
2022-10-08 23:45:49 +02:00
pending: purchaseRecord.purchaseStatus === PurchaseStatus.Paying,
refunds,
timestamp,
transactionId: makeTransactionId(
TransactionType.Payment,
purchaseRecord.proposalId,
),
proposalId: purchaseRecord.proposalId,
info,
frozen:
purchaseRecord.purchaseStatus === PurchaseStatus.PaymentAbortFinished ??
false,
...(ort?.lastError ? { error: ort.lastError } : {}),
2022-09-16 17:04:32 +02:00
};
}
2020-05-12 10:38:58 +02:00
/**
2021-04-27 23:42:25 +02:00
* Retrieve the full event history for this wallet.
2020-05-12 10:38:58 +02:00
*/
export async function getTransactions(
ws: InternalWalletState,
transactionsRequest?: TransactionsRequest,
): Promise<TransactionsResponse> {
const transactions: Transaction[] = [];
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [
x.coins,
x.denominations,
x.depositGroups,
x.exchangeDetails,
x.exchanges,
x.operationRetries,
x.peerPullPaymentIncoming,
x.peerPushPaymentInitiations,
x.planchets,
x.purchases,
x.contractTerms,
x.recoupGroups,
x.tips,
x.tombstones,
x.withdrawalGroups,
])
.runReadOnly(async (tx) => {
tx.peerPushPaymentInitiations.iter().forEachAsync(async (pi) => {
const amount = Amounts.parseOrThrow(pi.amount);
if (shouldSkipCurrency(transactionsRequest, amount.currency)) {
return;
}
if (shouldSkipSearch(transactionsRequest, [])) {
return;
}
2022-11-02 17:42:14 +01:00
const ct = await tx.contractTerms.get(pi.contractTermsHash);
checkDbInvariant(!!ct);
transactions.push(
buildTransactionForPushPaymentDebit(pi, ct.contractTermsRaw),
);
});
2020-05-15 13:28:15 +02:00
tx.peerPullPaymentIncoming.iter().forEachAsync(async (pi) => {
const amount = Amounts.parseOrThrow(pi.contractTerms.amount);
if (shouldSkipCurrency(transactionsRequest, amount.currency)) {
return;
}
if (shouldSkipSearch(transactionsRequest, [])) {
return;
}
2022-11-02 17:42:14 +01:00
if (
pi.status !== PeerPullPaymentIncomingStatus.Accepted &&
pi.status !== PeerPullPaymentIncomingStatus.Paid
) {
return;
}
transactions.push(buildTransactionForPullPaymentDebit(pi));
});
tx.withdrawalGroups.iter().forEachAsync(async (wsr) => {
if (
shouldSkipCurrency(
transactionsRequest,
2022-11-02 17:42:14 +01:00
Amounts.currencyOf(wsr.rawWithdrawalAmount),
)
) {
return;
}
if (shouldSkipSearch(transactionsRequest, [])) {
return;
}
2022-09-05 18:12:30 +02:00
const opId = RetryTags.forWithdrawal(wsr);
const ort = await tx.operationRetries.get(opId);
2022-11-02 12:50:34 +01:00
switch (wsr.wgInfo.withdrawalType) {
case WithdrawalRecordType.PeerPullCredit:
transactions.push(buildTransactionForPullPaymentCredit(wsr, ort));
2021-06-09 15:14:17 +02:00
return;
2022-11-02 12:50:34 +01:00
case WithdrawalRecordType.PeerPushCredit:
transactions.push(buildTransactionForPushPaymentCredit(wsr, ort));
return;
case WithdrawalRecordType.BankIntegrated:
transactions.push(
buildTransactionForBankIntegratedWithdraw(wsr, ort),
);
return;
case WithdrawalRecordType.BankManual: {
const exchangeDetails = await getExchangeDetails(
tx,
wsr.exchangeBaseUrl,
);
if (!exchangeDetails) {
// FIXME: report somehow
return;
}
2022-11-02 12:50:34 +01:00
transactions.push(
buildTransactionForManualWithdraw(wsr, exchangeDetails, ort),
);
return;
}
case WithdrawalRecordType.Recoup:
// FIXME: Do we also report a transaction here?
return;
}
});
tx.depositGroups.iter().forEachAsync(async (dg) => {
const amount = Amounts.parseOrThrow(dg.contractTermsRaw.amount);
if (shouldSkipCurrency(transactionsRequest, amount.currency)) {
return;
}
2022-09-05 18:12:30 +02:00
const opId = RetryTags.forDeposit(dg);
const retryRecord = await tx.operationRetries.get(opId);
transactions.push(buildTransactionForDeposit(dg, retryRecord));
});
tx.purchases.iter().forEachAsync(async (purchase) => {
const download = purchase.download;
if (!download) {
return;
}
if (!purchase.payInfo) {
return;
}
if (shouldSkipCurrency(transactionsRequest, download.currency)) {
return;
}
const contractTermsRecord = await tx.contractTerms.get(
download.contractTermsHash,
);
if (!contractTermsRecord) {
return;
}
if (
shouldSkipSearch(transactionsRequest, [
contractTermsRecord?.contractTermsRaw?.summary || "",
])
) {
return;
}
const contractData = extractContractData(
contractTermsRecord?.contractTermsRaw,
download.contractTermsHash,
download.contractTermsMerchantSig,
);
2022-09-16 17:04:32 +02:00
const filteredRefunds = await Promise.all(
Object.values(purchase.refunds).map(async (r) => {
2022-09-16 17:04:32 +02:00
const t = await tx.tombstones.get(
makeTombstoneId(
2022-09-16 17:04:32 +02:00
TombstoneTag.DeleteRefund,
purchase.proposalId,
2022-09-16 17:04:32 +02:00
`${r.executionTime.t_s}`,
),
);
if (!t) return r;
return undefined;
}),
);
2022-09-16 17:04:32 +02:00
const cleanRefunds = filteredRefunds.filter(
(x): x is WalletRefundItem => !!x,
);
2022-09-16 17:04:32 +02:00
const refunds = mergeRefundByExecutionTime(
cleanRefunds,
2022-11-02 17:42:14 +01:00
Amounts.zeroOfCurrency(download.currency),
2022-09-16 17:04:32 +02:00
);
refunds.forEach(async (refundInfo) => {
transactions.push(
await buildTransactionForRefund(
purchase,
contractData,
refundInfo,
undefined,
),
2022-09-16 17:04:32 +02:00
);
});
const payOpId = RetryTags.forPay(purchase);
2022-09-05 18:12:30 +02:00
const payRetryRecord = await tx.operationRetries.get(payOpId);
transactions.push(
await buildTransactionForPurchase(
purchase,
contractData,
refunds,
payRetryRecord,
),
2022-09-05 18:12:30 +02:00
);
});
tx.tips.iter().forEachAsync(async (tipRecord) => {
if (
shouldSkipCurrency(
transactionsRequest,
2022-11-02 17:42:14 +01:00
Amounts.parseOrThrow(tipRecord.tipAmountRaw).currency,
)
) {
return;
}
if (!tipRecord.acceptedTimestamp) {
return;
}
2022-09-05 18:12:30 +02:00
const opId = RetryTags.forTipPickup(tipRecord);
const retryRecord = await tx.operationRetries.get(opId);
transactions.push(buildTransactionForTip(tipRecord, retryRecord));
});
});
2020-05-12 10:38:58 +02:00
2020-05-15 20:22:58 +02:00
const txPending = transactions.filter((x) => x.pending);
const txNotPending = transactions.filter((x) => !x.pending);
const txCmp = (h1: Transaction, h2: Transaction) => {
const tsCmp = AbsoluteTime.cmp(
2022-03-18 15:32:41 +01:00
AbsoluteTime.fromTimestamp(h1.timestamp),
AbsoluteTime.fromTimestamp(h2.timestamp),
);
if (tsCmp === 0) {
return Math.sign(txOrder[h1.type] - txOrder[h2.type]);
}
return tsCmp;
};
txPending.sort(txCmp);
txNotPending.sort(txCmp);
2020-05-12 10:38:58 +02:00
return { transactions: [...txNotPending, ...txPending] };
2020-05-12 10:38:58 +02:00
}
2021-06-14 19:37:35 +02:00
/**
* Immediately retry the underlying operation
* of a transaction.
*/
export async function retryTransaction(
ws: InternalWalletState,
transactionId: string,
): Promise<void> {
logger.info(`retrying transaction ${transactionId}`);
2022-10-17 18:50:17 +02:00
const { type, args: rest } = parseId("any", transactionId);
2021-06-14 19:37:35 +02:00
switch (type) {
2022-05-14 23:09:33 +02:00
case TransactionType.Deposit: {
2021-06-14 19:37:35 +02:00
const depositGroupId = rest[0];
2022-03-28 23:59:16 +02:00
processDepositGroup(ws, depositGroupId, {
forceNow: true,
});
2021-06-14 19:37:35 +02:00
break;
2022-05-14 23:09:33 +02:00
}
case TransactionType.Withdrawal: {
2021-06-14 19:37:35 +02:00
const withdrawalGroupId = rest[0];
2022-08-09 15:00:45 +02:00
await processWithdrawalGroup(ws, withdrawalGroupId, { forceNow: true });
2021-06-14 19:37:35 +02:00
break;
2022-05-14 23:09:33 +02:00
}
case TransactionType.Payment: {
const proposalId = rest[0];
2022-03-29 13:50:45 +02:00
await processPurchasePay(ws, proposalId, { forceNow: true });
2021-06-14 19:37:35 +02:00
break;
2022-05-14 23:09:33 +02:00
}
case TransactionType.Tip: {
2021-06-14 19:37:35 +02:00
const walletTipId = rest[0];
2022-03-29 13:50:45 +02:00
await processTip(ws, walletTipId, { forceNow: true });
2021-06-14 19:37:35 +02:00
break;
2022-05-14 23:09:33 +02:00
}
case TransactionType.Refresh: {
2021-06-14 19:37:35 +02:00
const refreshGroupId = rest[0];
2022-03-29 13:50:45 +02:00
await processRefreshGroup(ws, refreshGroupId, { forceNow: true });
2021-06-14 19:37:35 +02:00
break;
2022-05-14 23:09:33 +02:00
}
2021-06-14 19:37:35 +02:00
default:
break;
}
}
/**
2021-05-21 13:32:49 +02:00
* Permanently delete a transaction based on the transaction ID.
*/
export async function deleteTransaction(
ws: InternalWalletState,
transactionId: string,
): Promise<void> {
2022-10-17 18:50:17 +02:00
const { type, args: rest } = parseId("txn", transactionId);
2022-09-05 18:12:30 +02:00
if (
type === TransactionType.Withdrawal ||
type === TransactionType.PeerPullCredit ||
type === TransactionType.PeerPushCredit
) {
const withdrawalGroupId = rest[0];
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.withdrawalGroups, x.tombstones])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const withdrawalGroupRecord = await tx.withdrawalGroups.get(
withdrawalGroupId,
);
if (withdrawalGroupRecord) {
2021-06-09 15:14:17 +02:00
await tx.withdrawalGroups.delete(withdrawalGroupId);
await tx.tombstones.put({
2021-05-20 17:11:44 +02:00
id: TombstoneTag.DeleteWithdrawalGroup + ":" + withdrawalGroupId,
});
return;
}
2021-06-09 15:14:17 +02:00
});
2021-05-20 17:11:44 +02:00
} else if (type === TransactionType.Payment) {
const proposalId = rest[0];
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.purchases, x.tombstones])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
2021-05-20 17:11:44 +02:00
let found = false;
2021-06-09 15:14:17 +02:00
const purchase = await tx.purchases.get(proposalId);
2021-05-20 17:11:44 +02:00
if (purchase) {
found = true;
await tx.purchases.delete(proposalId);
2021-05-20 17:11:44 +02:00
}
if (found) {
2021-06-09 15:14:17 +02:00
await tx.tombstones.put({
2021-05-20 17:11:44 +02:00
id: TombstoneTag.DeletePayment + ":" + proposalId,
});
}
2021-06-09 15:14:17 +02:00
});
2021-05-20 17:11:44 +02:00
} else if (type === TransactionType.Refresh) {
const refreshGroupId = rest[0];
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.refreshGroups, x.tombstones])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const rg = await tx.refreshGroups.get(refreshGroupId);
2021-05-20 17:11:44 +02:00
if (rg) {
2021-06-09 15:14:17 +02:00
await tx.refreshGroups.delete(refreshGroupId);
await tx.tombstones.put({
2021-05-20 17:11:44 +02:00
id: TombstoneTag.DeleteRefreshGroup + ":" + refreshGroupId,
});
}
2021-06-09 15:14:17 +02:00
});
2021-05-20 17:11:44 +02:00
} else if (type === TransactionType.Tip) {
const tipId = rest[0];
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.tips, x.tombstones])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const tipRecord = await tx.tips.get(tipId);
2021-05-20 17:11:44 +02:00
if (tipRecord) {
2021-06-09 15:14:17 +02:00
await tx.tips.delete(tipId);
await tx.tombstones.put({
2021-05-20 17:11:44 +02:00
id: TombstoneTag.DeleteTip + ":" + tipId,
});
}
2021-06-09 15:14:17 +02:00
});
2021-05-20 17:11:44 +02:00
} else if (type === TransactionType.Deposit) {
const depositGroupId = rest[0];
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.depositGroups, x.tombstones])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const tipRecord = await tx.depositGroups.get(depositGroupId);
2021-05-20 17:11:44 +02:00
if (tipRecord) {
2021-06-09 15:14:17 +02:00
await tx.depositGroups.delete(depositGroupId);
await tx.tombstones.put({
2021-05-20 17:11:44 +02:00
id: TombstoneTag.DeleteDepositGroup + ":" + depositGroupId,
});
}
2021-06-09 15:14:17 +02:00
});
} else if (type === TransactionType.Refund) {
const proposalId = rest[0];
const executionTimeStr = rest[1];
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.purchases, x.tombstones])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const purchase = await tx.purchases.get(proposalId);
if (purchase) {
// This should just influence the history view,
// but won't delete any actual refund information.
2021-06-09 15:14:17 +02:00
await tx.tombstones.put({
id: makeTombstoneId(
TombstoneTag.DeleteRefund,
proposalId,
executionTimeStr,
),
});
}
2021-06-09 15:14:17 +02:00
});
} else if (type === TransactionType.PeerPullDebit) {
const peerPullPaymentIncomingId = rest[0];
await ws.db
.mktx((x) => [x.peerPullPaymentIncoming, x.tombstones])
.runReadWrite(async (tx) => {
2022-09-05 18:12:30 +02:00
const debit = await tx.peerPullPaymentIncoming.get(
peerPullPaymentIncomingId,
);
if (debit) {
await tx.peerPullPaymentIncoming.delete(peerPullPaymentIncomingId);
await tx.tombstones.put({
id: makeTombstoneId(
TombstoneTag.DeletePeerPullDebit,
peerPullPaymentIncomingId,
),
});
}
});
} else if (type === TransactionType.PeerPushDebit) {
const pursePub = rest[0];
await ws.db
.mktx((x) => [x.peerPushPaymentInitiations, x.tombstones])
.runReadWrite(async (tx) => {
const debit = await tx.peerPushPaymentInitiations.get(pursePub);
if (debit) {
await tx.peerPushPaymentInitiations.delete(pursePub);
await tx.tombstones.put({
id: makeTombstoneId(TombstoneTag.DeletePeerPushDebit, pursePub),
});
}
});
} else {
const unknownTxType: never = type;
throw Error(`can't delete a '${unknownTxType}' transaction`);
}
}
export async function abortTransaction(
ws: InternalWalletState,
transactionId: string,
forceImmediateAbort?: boolean,
): Promise<void> {
const { type, args: rest } = parseId("txn", transactionId);
if (type === TransactionType.Payment) {
const proposalId = rest[0];
await abortPay(ws, proposalId, forceImmediateAbort);
} else {
const unknownTxType: any = type;
2023-01-13 20:05:17 +01:00
throw Error(
`can't abort a '${unknownTxType}' transaction: not yet implemented`,
);
}
}