@@ -2,12 +2,18 @@ import Boom from '@hapi/boom'
22import { StatusCodes } from 'http-status-codes'
33import Joi from 'joi'
44
5+ import { createLogger } from '~/src/server/common/helpers/logging/logger.js'
56import { EXTERNAL_STATE_APPENDAGE } from '~/src/server/constants.js'
6- import { getPaymentContext } from '~/src/server/plugins/engine/routes/payment-helper.js'
7+ import {
8+ buildPaymentInfo ,
9+ getPaymentContext
10+ } from '~/src/server/plugins/engine/routes/payment-helper.js'
711
812export const PAYMENT_RETURN_PATH = '/payment-callback'
913export const PAYMENT_SESSION_PREFIX = 'payment-'
1014
15+ const logger = createLogger ( )
16+
1117/**
1218 * Flash form component state after successful payment
1319 * @param {Request } request - the request
@@ -48,6 +54,42 @@ export function getRoutes() {
4854 return [ getReturnRoute ( ) ]
4955}
5056
57+ /**
58+ * Logs successful payment
59+ * @param {PaymentSessionData } session - the session data
60+ * @param {GetPaymentResponse } paymentStatus - the payment status from GOV.UK Pay
61+ */
62+ function logPaymentSuccess ( session , paymentStatus ) {
63+ logger . info (
64+ buildPaymentInfo (
65+ 'pre-auth' ,
66+ 'success' ,
67+ `${ paymentStatus . state . status } amount=${ paymentStatus . amount } ` ,
68+ session . isLivePayment ,
69+ paymentStatus . paymentId
70+ ) ,
71+ `[payment] Successful pre-auth for paymentId=${ paymentStatus . paymentId } `
72+ )
73+ }
74+
75+ /**
76+ * Logs failed/cancelled payment
77+ * @param {PaymentSessionData } session - the session data
78+ * @param {GetPaymentResponse } paymentStatus - the payment status from GOV.UK Pay
79+ */
80+ function logPaymentFailure ( session , paymentStatus ) {
81+ logger . info (
82+ buildPaymentInfo (
83+ 'pre-auth' ,
84+ 'failed/cancelled' ,
85+ `${ paymentStatus . state . status } amount=${ paymentStatus . amount } ` ,
86+ session . isLivePayment ,
87+ paymentStatus . paymentId
88+ ) ,
89+ `[payment] Failed/cancelled pre-auth for paymentId=${ paymentStatus . paymentId } `
90+ )
91+ }
92+
5193/**
5294 * Handles successful payment states (capturable/success)
5395 * @param {Request } request - the request
@@ -98,6 +140,7 @@ function getReturnRoute() {
98140 switch ( status ) {
99141 case 'capturable' :
100142 case 'success' :
143+ logPaymentSuccess ( session , paymentStatus )
101144 return handlePaymentSuccess (
102145 request ,
103146 h ,
@@ -109,6 +152,7 @@ function getReturnRoute() {
109152 case 'cancelled' :
110153 case 'failed' :
111154 case 'error' :
155+ logPaymentFailure ( session , paymentStatus )
112156 return handlePaymentFailure ( request , h , session , sessionKey )
113157
114158 case 'created' :
0 commit comments