-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhelper.js
More file actions
63 lines (55 loc) · 2.13 KB
/
helper.js
File metadata and controls
63 lines (55 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { format } from 'date-fns'
import { PaymentService } from '~/src/server/plugins/payment/service.js'
export const DEFAULT_PAYMENT_HELP_URL =
'https://www.gov.uk/government/organisations/department-for-environment-food-rural-affairs'
/**
* Determine which payment API key value to use.
* If a draft preview form or a live preview form, read the TEST API key value specific to that form.
* If a live (non-preview) form, read the LIVE API key value specific to that form.
* @param {boolean} isLivePayment - true if this is a live payment (as opposed to a test one)
* @param {string} formId - id of the form
* @returns {string}
*/
export function getPaymentApiKey(isLivePayment, formId) {
const apiKeyValue = isLivePayment
? process.env[`PAYMENT_PROVIDER_API_KEY_LIVE_${formId}`]
: process.env[`PAYMENT_PROVIDER_API_KEY_TEST_${formId}`]
if (!apiKeyValue) {
throw new Error(
`[payment] Missing payment api key for ${isLivePayment ? 'live' : 'test'} form id ${formId}`
)
}
return apiKeyValue
}
/**
* Creates a PaymentService instance with the appropriate API key
* @param {boolean} isLivePayment - true if this is a live payment
* @param {string} formId - id of the form
* @returns {PaymentService}
*/
export function createPaymentService(isLivePayment, formId) {
const apiKey = getPaymentApiKey(isLivePayment, formId)
return new PaymentService(apiKey)
}
/**
* Formats a payment date for display
* @param {string} isoString - ISO date string
* @returns {string} Formatted date string (e.g., "26 January 2026 5:01pm")
*/
export function formatPaymentDate(isoString) {
return format(new Date(isoString), 'd MMMM yyyy h:mmaaa')
}
/**
* Formats a currency amount with thousand separators and two decimal places
* @param {number} amount - amount in pounds
* @param {'en-GB'} [locale] - locale for formatting
* @param {'GBP'} [currency] - currency code
* @returns {string} Formatted amount (e.g., "£1,234.56")
*/
export function formatCurrency(amount, locale = 'en-GB', currency = 'GBP') {
const formatter = new Intl.NumberFormat(locale, {
style: 'currency',
currency
})
return formatter.format(amount)
}