Skip to content

Commit

Permalink
add tests for captures
Browse files Browse the repository at this point in the history
  • Loading branch information
janpaepke committed Sep 26, 2024
1 parent 6e1ed75 commit 783d195
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 22 deletions.
34 changes: 33 additions & 1 deletion tests/integration/payments.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import dotenv from 'dotenv';
import { fail } from 'node:assert';

import createMollieClient, { PaymentStatus } from '../..';
import createMollieClient, { CaptureMethod, PaymentMethod, PaymentStatus } from '../..';
import getHead from '../getHead';

/**
* Load the API_KEY environment variable
Expand Down Expand Up @@ -121,4 +122,35 @@ describe('payments', () => {
expect(payments.length).toEqual(2);
expect(payments[0].id).toEqual(nextPageCursor);
});

it.skip('should create a capture', async () => {
// Create a payment.
const payment = await mollieClient.payments.create({
amount: { value: '10.00', currency: 'EUR' },
description: 'Original description',
redirectUrl: 'https://example.com/redirect',
captureMode: CaptureMethod.manual,
method: PaymentMethod.creditcard,
});
expect(payment.captureDelay).toBeUndefined();
expect(payment.captureMode).toBe('manual');
expect(payment.authorizedAt).toBeUndefined();

expect(payment.captureBefore).toBeUndefined();

// TODO: the payment needs to be authorized here, but there doesn't seem to be a way to do this currently...

payment.refresh();
expect(payment.captureBefore).not.toBeUndefined();

// Create a capture for this payment.
const capture = await mollieClient.paymentCaptures.create({
paymentId: payment.id,
amount: { value: '10.00', currency: 'EUR' },
});
// check if the capture was created and assigned to the payment.
payment.refresh();
const captureOnPayment = await getHead(payment.getCaptures());
expect(capture.id).toBe(captureOnPayment.id);
});
});
66 changes: 45 additions & 21 deletions tests/unit/resources/payments/captures.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { CaptureStatus } from '../../../..';
import NetworkMocker, { getApiKeyClientProvider } from '../../../NetworkMocker';

function composeCaptureResponse(paymentId = 'tr_WDqYK6vllg', captureId = 'cpt_4qqhO89gsT') {
return {
resource: 'capture',
id: captureId,
mode: 'live',
description: 'Capture for cart #12345',
amount: {
value: '1027.99',
currency: 'EUR',
Expand All @@ -13,6 +15,8 @@ function composeCaptureResponse(paymentId = 'tr_WDqYK6vllg', captureId = 'cpt_4q
value: '399.00',
currency: 'EUR',
},
status: CaptureStatus.pending,
metadata: '{"bookkeeping_id":12345}',
paymentId: paymentId,
shipmentId: 'shp_3wmsgCJN4U',
settlementId: 'stl_jDk30akdN',
Expand Down Expand Up @@ -42,26 +46,30 @@ function composeCaptureResponse(paymentId = 'tr_WDqYK6vllg', captureId = 'cpt_4q
};
}

function testCapture(capture) {
function testCapture(capture, paymentId = 'tr_WDqYK6vllg', captureId = 'cpt_4qqhO89gsT') {
expect(capture.resource).toBe('capture');
expect(capture.id).toBe('cpt_4qqhO89gsT');
expect(capture.id).toBe(captureId);
expect(capture.mode).toBe('live');
expect(capture.paymentId).toBe('tr_WDqYK6vllg');
expect(capture.description).toBe('Capture for cart #12345');
expect(capture.paymentId).toBe(paymentId);
expect(capture.shipmentId).toBe('shp_3wmsgCJN4U');
expect(capture.settlementId).toBe('stl_jDk30akdN');

expect(capture.amount).toEqual({ value: '1027.99', currency: 'EUR' });
expect(capture.settlementAmount).toEqual({ value: '399.00', currency: 'EUR' });

expect(capture.status).toBe('pending');
expect(capture.metadata).toBe('{"bookkeeping_id":12345}');

expect(capture.createdAt).toBe('2018-08-02T09:29:56+00:00');

expect(capture._links.self).toEqual({
href: 'https://api.mollie.com/v2/payments/tr_WDqYK6vllg/captures/cpt_4qqhO89gsT',
href: `https://api.mollie.com/v2/payments/${paymentId}/captures/${captureId}`,
type: 'application/hal+json',
});

expect(capture._links.payment).toEqual({
href: 'https://api.mollie.com/v2/payments/tr_WDqYK6vllg',
href: `https://api.mollie.com/v2/payments/${paymentId}`,
type: 'application/hal+json',
});

Expand All @@ -81,36 +89,52 @@ function testCapture(capture) {
});
}

test('createCapture', () => {
return new NetworkMocker(getApiKeyClientProvider()).use(async ([mollieClient, networkMocker]) => {
networkMocker.intercept('POST', '/payments/tr_WDqYK6vllg/captures', 200, composeCaptureResponse('tr_7UhSN1zuXS', 'cpt_mNepDkEtco6ah3QNPUGYH')).twice();

const capture = await bluster(mollieClient.paymentCaptures.create.bind(mollieClient.paymentCaptures))({ paymentId: 'tr_WDqYK6vllg' });

testCapture(capture, 'tr_7UhSN1zuXS', 'cpt_mNepDkEtco6ah3QNPUGYH');
});
});

test('getCapture', () => {
return new NetworkMocker(getApiKeyClientProvider()).use(async ([mollieClient, networkMocker]) => {
networkMocker.intercept('GET', '/payments/tr_WDqYK6vllg/captures/cpt_4qqhO89gsT', 200, composeCaptureResponse('tr_WDqYK6vllg', 'cpt_4qqhO89gsT')).twice();

const capture = await bluster(mollieClient.paymentCaptures.get.bind(mollieClient.paymentCaptures))('cpt_4qqhO89gsT', { paymentId: 'tr_WDqYK6vllg' });

expect(typeof capture.getPayment).toBe('function');
expect(typeof capture.getSettlement).toBe('function');
expect(typeof capture.getShipment).toBe('function');

testCapture(capture);
});
});

test('listCaptures', () => {
return new NetworkMocker(getApiKeyClientProvider()).use(async ([mollieClient, networkMocker]) => {
networkMocker.intercept('GET', '/payments/tr_WDqYK6vllg/captures', 200, {
_embedded: {
captures: [composeCaptureResponse('tr_WDqYK6vllg', 'cpt_4qqhO89gsT')],
},
count: 1,
_links: {
documentation: {
href: 'https://docs.mollie.com/reference/v2/captures-api/list-captures',
type: 'text/html',
networkMocker
.intercept('GET', '/payments/tr_WDqYK6vllg/captures', 200, {
_embedded: {
captures: [composeCaptureResponse('tr_WDqYK6vllg', 'cpt_4qqhO89gsT')],
},
self: {
href: 'https://api.mollie.dev/v2/payments/tr_WDqYK6vllg/captures?limit=50',
type: 'application/hal+json',
count: 1,
_links: {
documentation: {
href: 'https://docs.mollie.com/reference/v2/captures-api/list-captures',
type: 'text/html',
},
self: {
href: 'https://api.mollie.dev/v2/payments/tr_WDqYK6vllg/captures?limit=50',
type: 'application/hal+json',
},
previous: null,
next: null,
},
previous: null,
next: null,
},
}).twice();
})
.twice();

const captures = await bluster(mollieClient.paymentCaptures.page.bind(mollieClient.paymentCaptures))({ paymentId: 'tr_WDqYK6vllg' });

Expand Down

0 comments on commit 783d195

Please sign in to comment.