Files
bunq/ts/bunq.classes.paymentbatch.ts
Juergen Kunz be09571604 update
2025-07-18 12:10:29 +00:00

166 lines
4.1 KiB
TypeScript

import * as plugins from './bunq.plugins.js';
import { BunqAccount } from './bunq.classes.account.js';
import { BunqMonetaryAccount } from './bunq.classes.monetaryaccount.js';
import type {
IBunqAmount,
IBunqAlias,
IBunqPaymentBatch,
IBunqPayment
} from './bunq.interfaces.js';
export interface IBatchPaymentEntry {
amount: IBunqAmount;
counterparty_alias: IBunqAlias;
description: string;
attachment_id?: number;
merchant_reference?: string;
}
export class BunqPaymentBatch {
private bunqAccount: BunqAccount;
constructor(bunqAccount: BunqAccount) {
this.bunqAccount = bunqAccount;
}
/**
* Create a batch payment
*/
public async create(
monetaryAccount: BunqMonetaryAccount,
payments: IBatchPaymentEntry[]
): Promise<number> {
await this.bunqAccount.apiContext.ensureValidSession();
const response = await this.bunqAccount.getHttpClient().post(
`/v1/user/${this.bunqAccount.userId}/monetary-account/${monetaryAccount.id}/payment-batch`,
{
payments: payments
}
);
if (response.Response && response.Response[0] && response.Response[0].Id) {
return response.Response[0].Id.id;
}
throw new Error('Failed to create batch payment');
}
/**
* Get batch payment details
*/
public async get(
monetaryAccount: BunqMonetaryAccount,
batchId: number
): Promise<{
id: number;
status: string;
payments: IBunqPayment[];
}> {
await this.bunqAccount.apiContext.ensureValidSession();
const response = await this.bunqAccount.getHttpClient().get(
`/v1/user/${this.bunqAccount.userId}/monetary-account/${monetaryAccount.id}/payment-batch/${batchId}`
);
if (response.Response && response.Response[0] && response.Response[0].PaymentBatch) {
const batch = response.Response[0].PaymentBatch;
return {
id: batch.id,
status: batch.status,
payments: batch.payments || []
};
}
throw new Error('Batch payment not found');
}
/**
* List batch payments
*/
public async list(
monetaryAccount: BunqMonetaryAccount,
options?: {
count?: number;
older_id?: number;
newer_id?: number;
}
): Promise<IBunqPaymentBatch[]> {
await this.bunqAccount.apiContext.ensureValidSession();
const params = {
count: options?.count || 10,
older_id: options?.older_id,
newer_id: options?.newer_id
};
const response = await this.bunqAccount.getHttpClient().list(
`/v1/user/${this.bunqAccount.userId}/monetary-account/${monetaryAccount.id}/payment-batch`,
params
);
const batches: IBunqPaymentBatch[] = [];
if (response.Response) {
for (const item of response.Response) {
if (item.PaymentBatch) {
batches.push(item.PaymentBatch);
}
}
}
return batches;
}
/**
* Update batch payment status
*/
public async update(
monetaryAccount: BunqMonetaryAccount,
batchId: number,
status: 'CANCELLED'
): Promise<void> {
await this.bunqAccount.apiContext.ensureValidSession();
await this.bunqAccount.getHttpClient().put(
`/v1/user/${this.bunqAccount.userId}/monetary-account/${monetaryAccount.id}/payment-batch/${batchId}`,
{
status: status
}
);
}
}
/**
* Batch payment builder
*/
export class BatchPaymentBuilder {
private bunqAccount: BunqAccount;
private monetaryAccount: BunqMonetaryAccount;
private payments: IBatchPaymentEntry[] = [];
constructor(bunqAccount: BunqAccount, monetaryAccount: BunqMonetaryAccount) {
this.bunqAccount = bunqAccount;
this.monetaryAccount = monetaryAccount;
}
/**
* Add a payment to the batch
*/
public addPayment(payment: IBatchPaymentEntry): BatchPaymentBuilder {
this.payments.push(payment);
return this;
}
/**
* Create the batch payment
*/
public async create(): Promise<number> {
if (this.payments.length === 0) {
throw new Error('No payments added to batch');
}
const batch = new BunqPaymentBatch(this.bunqAccount);
return batch.create(this.monetaryAccount, this.payments);
}
}