diff --git a/tests/integration/payments.test.ts b/tests/integration/payments.test.ts index 06c1c7c6..f5e58644 100644 --- a/tests/integration/payments.test.ts +++ b/tests/integration/payments.test.ts @@ -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 @@ -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); + }); }); diff --git a/tests/unit/resources/payments/captures.test.ts b/tests/unit/resources/payments/captures.test.ts index 17c9e05a..d34bf3e9 100644 --- a/tests/unit/resources/payments/captures.test.ts +++ b/tests/unit/resources/payments/captures.test.ts @@ -1,3 +1,4 @@ +import { CaptureStatus } from '../../../..'; import NetworkMocker, { getApiKeyClientProvider } from '../../../NetworkMocker'; function composeCaptureResponse(paymentId = 'tr_WDqYK6vllg', captureId = 'cpt_4qqhO89gsT') { @@ -5,6 +6,7 @@ function composeCaptureResponse(paymentId = 'tr_WDqYK6vllg', captureId = 'cpt_4q resource: 'capture', id: captureId, mode: 'live', + description: 'Capture for cart #12345', amount: { value: '1027.99', currency: 'EUR', @@ -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', @@ -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', }); @@ -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' });