Files
bunq/test/test.payments.simple.ts

251 lines
8.4 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as bunq from '../ts/index.js';
let testBunqAccount: bunq.BunqAccount;
let sandboxApiKey: string;
let primaryAccount: bunq.BunqMonetaryAccount;
tap.test('should setup payment test environment', async () => {
// Create sandbox user
const tempAccount = new bunq.BunqAccount({
apiKey: '',
deviceName: 'bunq-payment-test',
environment: 'SANDBOX',
});
sandboxApiKey = await tempAccount.createSandboxUser();
console.log('Generated sandbox API key for payment tests');
// Initialize bunq account
testBunqAccount = new bunq.BunqAccount({
apiKey: sandboxApiKey,
deviceName: 'bunq-payment-test',
environment: 'SANDBOX',
});
await testBunqAccount.init();
// Get primary account
const accounts = await testBunqAccount.getAccounts();
primaryAccount = accounts[0];
expect(primaryAccount).toBeInstanceOf(bunq.BunqMonetaryAccount);
console.log(`Primary account: ${primaryAccount.description} (${primaryAccount.balance.value} ${primaryAccount.balance.currency})`);
});
tap.test('should test payment builder creation', async () => {
// Test different payment builder configurations
// 1. Simple IBAN payment
const simplePayment = bunq.BunqPayment.builder(testBunqAccount, primaryAccount)
.amount('1.00', 'EUR')
.toIban('NL91ABNA0417164300', 'Simple Test')
.description('Simple payment test');
expect(simplePayment).toBeDefined();
expect(simplePayment['paymentData'].amount.value).toEqual('1.00');
expect(simplePayment['paymentData'].amount.currency).toEqual('EUR');
console.log('Simple payment builder created');
// 2. Payment with custom request ID
const customIdPayment = bunq.BunqPayment.builder(testBunqAccount, primaryAccount)
.amount('2.50', 'EUR')
.toIban('NL91ABNA0417164300', 'Custom ID Test')
.description('Payment with custom request ID')
.description('Payment with custom request ID');
expect(customIdPayment).toBeDefined();
expect(customIdPayment['paymentData'].description).toEqual('Payment with custom request ID');
console.log('Custom request ID payment builder created');
// 3. Payment to email
const emailPayment = bunq.BunqPayment.builder(testBunqAccount, primaryAccount)
.amount('3.00', 'EUR')
.toEmail('test@example.com', 'Email Test')
.description('Payment to email');
expect(emailPayment).toBeDefined();
expect(emailPayment['paymentData'].counterparty_alias.type).toEqual('EMAIL');
expect(emailPayment['paymentData'].counterparty_alias.value).toEqual('test@example.com');
console.log('Email payment builder created');
// 4. Payment to phone number
const phonePayment = bunq.BunqPayment.builder(testBunqAccount, primaryAccount)
.amount('4.00', 'EUR')
.toPhoneNumber('+31612345678', 'Phone Test')
.description('Payment to phone');
expect(phonePayment).toBeDefined();
expect(phonePayment['paymentData'].counterparty_alias.type).toEqual('PHONE_NUMBER');
expect(phonePayment['paymentData'].counterparty_alias.value).toEqual('+31612345678');
console.log('Phone payment builder created');
});
tap.test('should test draft payment operations', async () => {
const draft = new bunq.BunqDraftPayment(testBunqAccount);
try {
// Create a draft payment
const draftId = await draft.create(primaryAccount, {
entries: [{
amount: {
currency: 'EUR',
value: '5.00'
},
counterparty_alias: {
type: 'IBAN',
value: 'NL91ABNA0417164300',
name: 'Draft Test Recipient'
},
description: 'Test draft payment'
}]
});
expect(draftId).toBeTypeofNumber();
console.log(`Created draft payment with ID: ${draftId}`);
// List drafts
const drafts = await bunq.BunqDraftPayment.list(testBunqAccount, primaryAccount);
expect(drafts).toBeArray();
if (drafts.length > 0) {
const firstDraft = drafts[0];
expect(firstDraft).toHaveProperty('id');
console.log(`Found ${drafts.length} draft payments`);
}
} catch (error) {
console.log('Draft payment error (may not be fully supported in sandbox):', error.message);
}
});
tap.test('should test payment creation with insufficient funds', async () => {
try {
// Try to create a payment (will fail due to insufficient funds)
const payment = await bunq.BunqPayment.builder(testBunqAccount, primaryAccount)
.amount('10.00', 'EUR')
.toIban('NL91ABNA0417164300', 'Test Payment')
.description('This will fail due to insufficient funds')
.create();
console.log('Payment created (sandbox may not enforce balance):', payment.id);
} catch (error) {
console.log('Payment failed as expected:', error.message);
expect(error).toBeInstanceOf(Error);
}
});
tap.test('should test transaction retrieval after payment', async () => {
// Get recent transactions
const transactions = await primaryAccount.getTransactions(10);
expect(transactions).toBeArray();
console.log(`Found ${transactions.length} transactions`);
if (transactions.length > 0) {
const firstTx = transactions[0];
expect(firstTx).toBeInstanceOf(bunq.BunqTransaction);
expect(firstTx.amount).toHaveProperty('value');
expect(firstTx.amount).toHaveProperty('currency');
expect(firstTx.description).toBeTypeofString();
console.log(`Latest transaction: ${firstTx.amount.value} ${firstTx.amount.currency} - ${firstTx.description}`);
}
});
tap.test('should test request inquiry operations', async () => {
const requestInquiry = new bunq.BunqRequestInquiry(testBunqAccount, primaryAccount);
try {
// Create a payment request
const requestData = {
amount_inquired: {
currency: 'EUR',
value: '15.00'
},
counterparty_alias: {
type: 'EMAIL',
value: 'requester@example.com',
name: 'Request Sender'
},
description: 'Payment request test',
allow_bunqme: true
};
const request = await requestInquiry.create(requestData);
expect(request.id).toBeTypeofNumber();
console.log(`Created payment request with ID: ${request.id}`);
// List requests
const requests = await requestInquiry.list();
expect(requests).toBeArray();
console.log(`Found ${requests.length} payment requests`);
// Get specific request
if (request.id) {
const retrievedRequest = await requestInquiry.get(request.id);
expect(retrievedRequest.id).toEqual(request.id);
expect(retrievedRequest.amountInquired.value).toEqual('15.00');
}
} catch (error) {
console.log('Payment request error:', error.message);
}
});
tap.test('should test webhook operations', async () => {
const webhook = new bunq.BunqWebhook(testBunqAccount);
try {
// Create a webhook
const webhookUrl = 'https://example.com/webhook/bunq';
const webhookId = await webhook.create(primaryAccount, webhookUrl);
expect(webhookId).toBeTypeofNumber();
console.log(`Created webhook with ID: ${webhookId}`);
// List webhooks
const webhooks = await webhook.list(primaryAccount);
expect(webhooks).toBeArray();
const createdWebhook = webhooks.find(w => w.id === webhookId);
expect(createdWebhook).toBeDefined();
// Delete webhook
await webhook.delete(primaryAccount, webhookId);
console.log('Webhook deleted successfully');
} catch (error) {
console.log('Webhook error:', error.message);
}
});
tap.test('should test notification filters', async () => {
const notification = new bunq.BunqNotification(testBunqAccount);
try {
// Create URL notification filter
const filterId = await notification.createUrlFilter({
notification_target: 'https://example.com/notifications',
category: ['PAYMENT', 'MUTATION']
});
expect(filterId).toBeTypeofNumber();
console.log(`Created notification filter with ID: ${filterId}`);
// List URL filters
const urlFilters = await notification.listUrlFilters();
expect(urlFilters).toBeArray();
console.log(`Found ${urlFilters.length} URL notification filters`);
// Delete filter
await notification.deleteUrlFilter(filterId);
console.log('Notification filter deleted');
} catch (error) {
console.log('Notification filter error:', error.message);
}
});
tap.test('should cleanup payment test resources', async () => {
await testBunqAccount.stop();
console.log('Payment test cleanup completed');
});
export default tap.start();