Skip to content

Commit 3cc88fa

Browse files
committed
Rework after review - part 1
1 parent eaafa9f commit 3cc88fa

7 files changed

Lines changed: 27 additions & 28 deletions

File tree

jest.setup.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ process.env.UPLOADER_BUCKET_NAME = 'dummy-bucket'
1313
process.env.GOOGLE_ANALYTICS_TRACKING_ID = 'G-123456789'
1414
process.env.SUBMISSION_EMAIL_ADDRESS = 'dummy@defra.gov.uk'
1515
process.env.ORDNANCE_SURVEY_API_KEY = 'dummy'
16-
process.env.PAYMENT_PROVIDER_API_KEY_TEST = 'test-api-key'
16+
process.env.PAYMENT_PROVIDER_API_KEY_TEST_formid = 'test-api-key'

src/config/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,6 @@ export const config = convict({
260260
nullable: true,
261261
default: undefined,
262262
env: 'ORDNANCE_SURVEY_API_KEY'
263-
} as SchemaObj<string | undefined>,
264-
265-
paymentProviderApiKeyTest: {
266-
doc: 'A test API key for integrating with a payment provider',
267-
format: String,
268-
nullable: true,
269-
default: undefined,
270-
env: 'PAYMENT_PROVIDER_API_KEY_TEST'
271263
} as SchemaObj<string | undefined>
272264
})
273265

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '@defra/forms-model'
66

77
import { ComponentBase } from '~/src/server/plugins/engine/components/ComponentBase.js'
8+
import { type PaymentState } from '~/src/server/plugins/engine/components/PaymentField.types.js'
89
import { optionalText } from '~/src/server/plugins/engine/components/constants.js'
910
import {
1011
type FormContext,
@@ -179,7 +180,7 @@ export class FormComponent extends ComponentBase {
179180

180181
getContextValueFromFormValue(
181182
value: FormValue | FormPayload
182-
): Item['value'] | Item['value'][] | null {
183+
): Item['value'] | Item['value'][] | PaymentState | null {
183184
// Filter object field values
184185
if (this.isState(value)) {
185186
const values = Object.values(value).filter(isFormValue)
@@ -191,12 +192,12 @@ export class FormComponent extends ComponentBase {
191192
return value.filter(isFormValue)
192193
}
193194

194-
return this.isValue(value) ? (value as Item['value']) : null
195+
return this.isValue(value) ? value : null
195196
}
196197

197198
getContextValueFromState(
198199
state: FormSubmissionState
199-
): Item['value'] | Item['value'][] | null {
200+
): Item['value'] | Item['value'][] | PaymentState | null {
200201
const value = this.getFormValueFromState(state)
201202

202203
return this.getContextValueFromFormValue(value)

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('PaymentField', () => {
170170
amount: 150,
171171
description: 'payment description',
172172
uuid: 'ee501106-4ce1-4947-91a7-7cc1a335ccd8',
173-
formId: 'form-id',
173+
formId: 'formid',
174174
isLivePayment: false
175175
}
176176
it('returns text from state', () => {
@@ -206,7 +206,7 @@ describe('PaymentField', () => {
206206
paymentId: 'payment-id',
207207
reference: 'payment-ref',
208208
uuid: 'ee501106-4ce1-4947-91a7-7cc1a335ccd8',
209-
formId: 'form-id',
209+
formId: 'formid',
210210
amount: 100,
211211
description: 'Test payment description',
212212
isLivePayment: false
@@ -273,7 +273,7 @@ describe('PaymentField', () => {
273273
const args = {
274274
controller: {
275275
model: {
276-
formId: 'form-id',
276+
formId: 'formid',
277277
basePath: 'base-path',
278278
name: 'PaymentModel'
279279
},
@@ -308,7 +308,7 @@ describe('PaymentField', () => {
308308
componentName: 'myComponent',
309309
description: 'Test payment description',
310310
failureUrl: 'http://localhost:3009/test-payment',
311-
formId: 'form-id',
311+
formId: 'formid',
312312
isLivePayment: false,
313313
paymentId: 'new-payment-id',
314314
reference: 'pay-ref-123',
@@ -338,7 +338,7 @@ describe('PaymentField', () => {
338338
const args = {
339339
controller: {
340340
model: {
341-
formId: 'form-id',
341+
formId: 'formid',
342342
basePath: 'base-path',
343343
name: 'PaymentModel'
344344
},
@@ -432,7 +432,7 @@ describe('PaymentField', () => {
432432
amount: 123,
433433
description: 'Payment desc',
434434
isLivePayment: false,
435-
formId: 'form-id'
435+
formId: 'formid'
436436
}
437437
}
438438
} as unknown as FormContext
@@ -458,7 +458,7 @@ describe('PaymentField', () => {
458458
amount: 123,
459459
description: 'Payment desc',
460460
isLivePayment: false,
461-
formId: 'form-id'
461+
formId: 'formid'
462462
}
463463
}
464464
} as unknown as FormContext
@@ -495,7 +495,7 @@ describe('PaymentField', () => {
495495
amount: 123,
496496
description: 'Payment desc',
497497
isLivePayment: false,
498-
formId: 'form-id'
498+
formId: 'formid'
499499
}
500500
}
501501
} as unknown as FormContext
@@ -531,7 +531,7 @@ describe('PaymentField', () => {
531531
amount: 123,
532532
description: 'Payment desc',
533533
isLivePayment: false,
534-
formId: 'form-id'
534+
formId: 'formid'
535535
}
536536
}
537537
} as unknown as FormContext

src/server/plugins/engine/routes/payment-helper.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('payment helper', () => {
3535
get: jest.fn().mockReturnValueOnce({
3636
paymentId: 'payment-id',
3737
isLivePayment: false,
38-
formId: 'form-id'
38+
formId: 'formid'
3939
})
4040
}
4141
}
@@ -76,7 +76,7 @@ describe('payment helper', () => {
7676
}
7777
},
7878
session: {
79-
formId: 'form-id',
79+
formId: 'formid',
8080
isLivePayment: false,
8181
paymentId: 'payment-id'
8282
},

src/server/plugins/payment/helper.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import { format } from 'date-fns'
22

3-
import { config } from '~/src/config/index.js'
43
import { PaymentService } from '~/src/server/plugins/payment/service.js'
54

65
export const DEFAULT_PAYMENT_HELP_URL =
76
'https://www.gov.uk/government/organisations/department-for-environment-food-rural-affairs'
87

98
/**
109
* Determine which payment API key value to use.
11-
* If a non-live non-preview form, use the TEST API key value.
12-
* If a live (non-preview) form, read the API key value specific to that form.
10+
* If a draft preview form or a live preview form, read the TEST API key value specific to that form.
11+
* If a live (non-preview) form, read the LIVE API key value specific to that form.
1312
* @param {boolean} isLivePayment - true if this is a live payment (as opposed to a test one)
1413
* @param {string} formId - id of the form
1514
* @returns {string}
1615
*/
1716
export function getPaymentApiKey(isLivePayment, formId) {
1817
const apiKeyValue = isLivePayment
1918
? process.env[`PAYMENT_PROVIDER_API_KEY_LIVE_${formId}`]
20-
: config.get('paymentProviderApiKeyTest')
19+
: process.env[`PAYMENT_PROVIDER_API_KEY_TEST_${formId}`]
2120

2221
if (!apiKeyValue) {
2322
throw new Error(

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('getPaymentApiKey', () => {
99
config.set('paymentProviderApiKeyTest', 'TEST-API-KEY')
1010
const formId = 'form-id'
1111
process.env['PAYMENT_PROVIDER_API_KEY_LIVE_form-id'] = 'LIVE-API-KEY'
12+
process.env['PAYMENT_PROVIDER_API_KEY_TEST_form-id'] = 'TEST-API-KEY'
1213

1314
it('should read test key when non-live form', () => {
1415
const apiKey = getPaymentApiKey(false, formId)
@@ -20,7 +21,13 @@ describe('getPaymentApiKey', () => {
2021
expect(apiKey).toBe('LIVE-API-KEY')
2122
})
2223

23-
it('should throw if key is missing', () => {
24+
it('should throw if TEST key is missing', () => {
25+
expect(() => getPaymentApiKey(false, 'form-id-missing')).toThrow(
26+
'Missing payment api key for test form id form-id-missing'
27+
)
28+
})
29+
30+
it('should throw if LIVE key is missing', () => {
2431
expect(() => getPaymentApiKey(true, 'form-id-missing')).toThrow(
2532
'Missing payment api key for live form id form-id-missing'
2633
)

0 commit comments

Comments
 (0)