11import { StatusCodes } from 'http-status-codes'
22
33import { createLogger } from '~/src/server/common/helpers/logging/logger.js'
4+ import {
5+ buildPaymentInfo ,
6+ convertPenceToPounds
7+ } from '~/src/server/plugins/engine/routes/payment-helper.js'
48import { get , post , postJson } from '~/src/server/services/httpService.js'
59
610const PAYMENT_BASE_URL = 'https://publicapi.payments.service.gov.uk'
@@ -35,9 +39,17 @@ export class PaymentService {
3539 * @param {string } description
3640 * @param {string } returnUrl
3741 * @param {string } reference
42+ * @param {boolean } isLivePayment
3843 * @param {{ formId: string, slug: string } } metadata
3944 */
40- async createPayment ( amount , description , returnUrl , reference , metadata ) {
45+ async createPayment (
46+ amount ,
47+ description ,
48+ returnUrl ,
49+ reference ,
50+ isLivePayment ,
51+ metadata
52+ ) {
4153 const response = await this . postToPayProvider ( {
4254 amount,
4355 description,
@@ -48,15 +60,13 @@ export class PaymentService {
4860 } )
4961
5062 logger . info (
51- {
52- event : {
53- category : 'payment' ,
54- action : 'create-payment' ,
55- outcome : 'success' ,
56- reason : `amount=${ amount } ` ,
57- reference : response . payment_id
58- }
59- } ,
63+ buildPaymentInfo (
64+ 'create-payment' ,
65+ 'success' ,
66+ `amount=${ convertPenceToPounds ( amount ) } ` ,
67+ isLivePayment ,
68+ response . payment_id
69+ ) ,
6070 `[payment] Created payment and user taken to enter pre-auth details for paymentId=${ response . payment_id } `
6171 )
6272
@@ -68,9 +78,10 @@ export class PaymentService {
6878
6979 /**
7080 * @param {string } paymentId
81+ * @param {boolean } isLivePayment
7182 * @returns {Promise<GetPaymentResponse> }
7283 */
73- async getPaymentStatus ( paymentId ) {
84+ async getPaymentStatus ( paymentId , isLivePayment ) {
7485 const getByType = /** @type {typeof get<GetPaymentApiResponse> } */ ( get )
7586
7687 try {
@@ -92,18 +103,15 @@ export class PaymentService {
92103
93104 const state = response . payload . state
94105 logger . info (
95- {
96- event : {
97- category : 'payment' ,
98- action : 'get-payment-status' ,
99- outcome :
100- state . status === 'capturable' || state . status === 'success'
101- ? 'success'
102- : 'failure' ,
103- reason : `status:${ state . status } code:${ state . code ?? 'N/A' } message:${ state . message ?? 'N/A' } ` ,
104- reference : paymentId
105- }
106- } ,
106+ buildPaymentInfo (
107+ 'get-payment-status' ,
108+ state . status === 'capturable' || state . status === 'success'
109+ ? 'success'
110+ : 'failure' ,
111+ `status:${ state . status } code:${ state . code ?? 'N/A' } message:${ state . message ?? 'N/A' } ` ,
112+ isLivePayment ,
113+ paymentId
114+ ) ,
107115 `[payment] Got payment status for paymentId=${ paymentId } : status=${ state . status } `
108116 )
109117
@@ -151,7 +159,7 @@ export class PaymentService {
151159 category : 'payment' ,
152160 action : 'capture-payment' ,
153161 outcome : 'success' ,
154- reason : `amount=${ amount } ` ,
162+ reason : `amount=${ convertPenceToPounds ( amount ) } ` ,
155163 reference : paymentId
156164 }
157165 } ,
0 commit comments