Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/server/plugins/engine/components/PaymentField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ export class PaymentField extends FormComponent {
)
}

const captured = await paymentService.capturePayment(paymentId)
const captured = await paymentService.capturePayment(
paymentId,
status.amount
)

if (!captured) {
throw new PaymentPreAuthError(
Expand Down
30 changes: 28 additions & 2 deletions src/server/plugins/payment/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export class PaymentService {
})

logger.info(
{
event: {
category: 'payment',
action: 'create-payment',
outcome: 'success',
reference: response.payment_id
}
},
`[payment] Created payment and user taken to enter pre-auth details for paymentId=${response.payment_id}`
)

Expand Down Expand Up @@ -83,7 +91,16 @@ export class PaymentService {

const state = response.payload.state
logger.info(
`[payment] Got payment status for paymentId=${paymentId}: ${state.status} message:${state.message ?? 'N/A'} code:${state.code ?? 'N/A'}`
{
event: {
category: 'payment',
action: 'get-payment-status',
outcome: state.status,
reason: `${state.code ?? 'N/A'} ${state.message ?? 'N/A'}`,
reference: paymentId
}
},
`[payment] Got payment status for paymentId=${paymentId}: status=${state.status}`
)

return {
Expand All @@ -106,9 +123,10 @@ export class PaymentService {
/**
* Captures a payment that is in 'capturable' status
* @param {string} paymentId
* @param {number} amount
* @returns {Promise<boolean>}
*/
async capturePayment(paymentId) {
async capturePayment(paymentId, amount) {
try {
const response = await post(
`${PAYMENT_BASE_URL}${PAYMENT_ENDPOINT}/${paymentId}/capture`,
Expand All @@ -124,6 +142,14 @@ export class PaymentService {
statusCode === StatusCodes.NO_CONTENT
) {
logger.info(
{
event: {
category: 'payment',
action: 'capture-payment',
outcome: `success amount=${amount}`,
reference: paymentId
}
},
`[payment] Successfully captured payment for paymentId=${paymentId}`
)
return true
Expand Down
17 changes: 13 additions & 4 deletions src/server/plugins/payment/service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ describe('payment service', () => {
error: undefined
})

const captureResult = await service.capturePayment('payment-id-12345')
const captureResult = await service.capturePayment(
'payment-id-12345',
100
)
expect(captureResult).toBe(true)
})

Expand All @@ -169,7 +172,10 @@ describe('payment service', () => {
error: undefined
})

const captureResult = await service.capturePayment('payment-id-12345')
const captureResult = await service.capturePayment(
'payment-id-12345',
100
)
expect(captureResult).toBe(true)
})

Expand All @@ -184,7 +190,10 @@ describe('payment service', () => {
error: undefined
})

const captureResult = await service.capturePayment('payment-id-12345')
const captureResult = await service.capturePayment(
'payment-id-12345',
100
)
expect(captureResult).toBe(false)
})

Expand All @@ -194,7 +203,7 @@ describe('payment service', () => {
.mockRejectedValueOnce(new Error('internal capture error'))

await expect(() =>
service.capturePayment('payment-id-12345')
service.capturePayment('payment-id-12345', 100)
).rejects.toThrow('internal capture error')
})
})
Expand Down
Loading