Skip to content

Commit 7ce65b0

Browse files
committed
ix: payment formatting and add test
1 parent a9d7ba6 commit 7ce65b0

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/server/plugins/engine/components/PaymentField.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import {
2929
type FormSubmissionError,
3030
type FormSubmissionState
3131
} from '~/src/server/plugins/engine/types.js'
32-
import { createPaymentService } from '~/src/server/plugins/payment/helper.js'
32+
import {
33+
createPaymentService,
34+
formatPaymentAmount
35+
} from '~/src/server/plugins/payment/helper.js'
3336

3437
export class PaymentField extends FormComponent {
3538
declare options: PaymentFieldComponent['options']
@@ -91,7 +94,7 @@ export class PaymentField extends FormComponent {
9194
return ''
9295
}
9396

94-
return `£${value.amount.toFixed(2)} - ${value.description}`
97+
return `${formatPaymentAmount(value.amount)} - ${value.description}`
9598
}
9699

97100
getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {
@@ -105,7 +108,10 @@ export class PaymentField extends FormComponent {
105108
// When user initially visits the payment page, there is no payment state yet so the amount is read form the form definition.
106109
const amount = paymentState?.amount ?? this.options.amount
107110

108-
const formattedAmount = amount.toFixed(2)
111+
const formattedAmount = new Intl.NumberFormat('en-GB', {
112+
minimumFractionDigits: 2,
113+
maximumFractionDigits: 2
114+
}).format(amount)
109115

110116
return {
111117
...viewModel,
@@ -153,7 +159,7 @@ export class PaymentField extends FormComponent {
153159

154160
getContextValueFromState(state: FormSubmissionState) {
155161
return this.isPaymentState(state)
156-
? `Reference: ${state.reference}\nAmount: ${state.amount.toFixed(2)}`
162+
? `Reference: ${state.reference}\nAmount: ${formatPaymentAmount(state.amount)}`
157163
: ''
158164
}
159165

src/server/plugins/payment/helper.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ export function formatPaymentDate(isoString) {
4747
}
4848

4949
/**
50-
* Formats a payment amount with two decimal places
50+
* Formats a payment amount with thousand separators and two decimal places
5151
* @param {number} amount - amount in pounds
52-
* @returns {string} Formatted amount (e.g., "£10.00")
52+
* @returns {string} Formatted amount (e.g., "£1,234.56")
5353
*/
5454
export function formatPaymentAmount(amount) {
55-
return ${amount.toFixed(2)}`
55+
return new Intl.NumberFormat('en-GB', {
56+
style: 'currency',
57+
currency: 'GBP'
58+
}).format(amount)
5659
}

src/server/plugins/payment/helper.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,12 @@ describe('formatPaymentAmount', () => {
4949
it('should format decimal amount', () => {
5050
expect(formatPaymentAmount(99.5)).toBe('£99.50')
5151
})
52+
53+
it('should format large amounts with thousand separators', () => {
54+
expect(formatPaymentAmount(1234.56)).toBe('£1,234.56')
55+
})
56+
57+
it('should format very large amounts with thousand separators', () => {
58+
expect(formatPaymentAmount(20000)).toBe('£20,000.00')
59+
})
5260
})

0 commit comments

Comments
 (0)