-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.ts
More file actions
134 lines (122 loc) · 3.77 KB
/
Copy pathtransfer.ts
File metadata and controls
134 lines (122 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { connectAndSync, logError, promptTan, type BankOptions } from './connection.js';
import { generatePain001, generatePain001Instant } from './pain001.js';
import { validateIBAN } from './iban.js';
import type { CreditTransferResponse } from './fints-ext/interaction.js';
export interface TransferOptions extends BankOptions {
recipient: string;
iban: string;
bic?: string;
amount: string;
purpose?: string;
sourceIban?: string;
instant?: boolean;
}
export async function executeTransfer(opts: TransferOptions): Promise<void> {
// Validate recipient IBAN
const ibanResult = validateIBAN(opts.iban);
if (!ibanResult.valid) {
logError(`Invalid recipient IBAN: ${ibanResult.error}`);
process.exit(1);
}
// Validate source IBAN if provided
if (opts.sourceIban) {
const srcResult = validateIBAN(opts.sourceIban);
if (!srcResult.valid) {
logError(`Invalid source IBAN: ${srcResult.error}`);
process.exit(1);
}
}
// Validate amount
const amountNum = parseFloat(opts.amount);
if (isNaN(amountNum) || amountNum <= 0) {
logError('Amount must be a positive number');
process.exit(1);
}
const amount = amountNum.toFixed(2);
const { client, accounts } = await connectAndSync(opts);
// Find source account
let sourceAccount;
if (opts.sourceIban) {
const sourceIbanClean = opts.sourceIban.replace(/\s/g, '').toUpperCase();
sourceAccount = accounts.find((a: any) => a.iban === sourceIbanClean);
if (!sourceAccount) {
logError(`Source account with IBAN ${sourceIbanClean} not found`, {
availableAccounts: accounts.map((a: any) => ({
accountNumber: a.accountNumber,
iban: a.iban,
})),
});
process.exit(1);
}
} else {
sourceAccount = accounts[0];
}
process.stderr.write(
`Source account: ${sourceAccount.iban || sourceAccount.accountNumber}\n`,
);
// Build pain.001 XML
const messageId = `MSG-${Date.now()}`;
const painParams = {
messageId,
debtorName: opts.user,
debtorIBAN: sourceAccount.iban || opts.sourceIban!,
debtorBIC: sourceAccount.bic || '',
creditorName: opts.recipient,
creditorIBAN: opts.iban.replace(/\s/g, '').toUpperCase(),
creditorBIC: opts.bic,
amount,
purpose: opts.purpose,
};
const painXml = opts.instant
? generatePain001Instant(painParams)
: generatePain001(painParams);
// Initiate credit transfer
const transferType = opts.instant ? 'instant payment' : 'transfer';
process.stderr.write(
`Initiating ${transferType} of ${amount} EUR to ${opts.recipient}...\n`,
);
let transferResponse: CreditTransferResponse;
if (opts.instant) {
transferResponse = await client.initiateInstantPayment(
sourceAccount.accountNumber,
painXml,
);
} else {
transferResponse = await client.initiateCreditTransfer(
sourceAccount.accountNumber,
painXml,
);
}
// Handle TAN if required
if (transferResponse.requiresTan) {
const tan = await promptTan(
transferResponse.tanChallenge || 'Please approve the transfer in your TAN app',
);
if (opts.instant) {
transferResponse = await client.initiateInstantPaymentWithTan(
transferResponse.tanReference!,
tan,
);
} else {
transferResponse = await client.initiateCreditTransferWithTan(
transferResponse.tanReference!,
tan,
);
}
}
// Output result
if (transferResponse.success) {
process.stdout.write(JSON.stringify({
status: 'OK',
amount: `${amount} EUR`,
recipient: opts.recipient,
iban: painParams.creditorIBAN,
jobReference: transferResponse.jobReference,
}, null, 2) + '\n');
} else {
logError('Transfer failed', {
bankAnswers: transferResponse.bankAnswers,
});
process.exit(1);
}
}