@@ -25,6 +25,7 @@ import {
2525 type FormSubmissionError ,
2626 type FormSubmissionState
2727} from '~/src/server/plugins/engine/types.js'
28+ import { getPaymentApiKey } from '~/src/server/plugins/payment/helper.js'
2829import { PaymentService } from '~/src/server/plugins/payment/service.js'
2930
3031export class PaymentField extends FormComponent {
@@ -48,7 +49,8 @@ export class PaymentField extends FormComponent {
4849 amount : joi . number ( ) . required ( ) ,
4950 description : joi . string ( ) . required ( ) ,
5051 uuid : joi . string ( ) . uuid ( ) . required ( ) ,
51- isLive : joi . boolean ( ) . required ( ) ,
52+ formId : joi . string ( ) . required ( ) ,
53+ isLivePayment : joi . boolean ( ) . required ( ) ,
5254 preAuth : joi
5355 . object ( {
5456 status : joi
@@ -157,8 +159,11 @@ export class PaymentField extends FormComponent {
157159 h : FormResponseToolkit ,
158160 args : PaymentDispatcherArgs
159161 ) : Promise < unknown > {
160- const { isLive } = args
161- const paymentService = new PaymentService ( { isLive } )
162+ const isLivePayment = args . isLive && ! args . isPreview
163+ const formId = args . controller . model . formId
164+ const apiKeyValue = getPaymentApiKey ( isLivePayment , formId )
165+
166+ const paymentService = new PaymentService ( apiKeyValue )
162167
163168 // 1. Generate UUID token
164169 const uuid = randomUUID ( )
@@ -171,7 +176,6 @@ export class PaymentField extends FormComponent {
171176 const amount = options . amount ?? 0
172177 const description = options . description ?? ''
173178
174- const formId = model . formId
175179 const slug = `/${ model . basePath } `
176180
177181 // 2. Build the return URL for GOV.UK Pay
@@ -196,14 +200,15 @@ export class PaymentField extends FormComponent {
196200 // 4. Store session data for the return route to use
197201 const sessionData : PaymentSessionData = {
198202 uuid,
203+ formId,
199204 reference,
200205 amount,
201206 description,
202207 paymentId : payment . paymentId ,
203208 componentName,
204209 returnUrl : summaryUrl ,
205210 failureUrl : paymentPageUrl ,
206- isLive
211+ isLivePayment
207212 }
208213
209214 request . yar . set ( `payment-${ uuid } ` , sessionData )
@@ -237,8 +242,9 @@ export class PaymentField extends FormComponent {
237242 return
238243 }
239244
240- const { paymentId, isLive } = paymentState
241- const paymentService = new PaymentService ( { isLive } )
245+ const { paymentId, isLivePayment, formId } = paymentState
246+ const apiKey = getPaymentApiKey ( isLivePayment , formId )
247+ const paymentService = new PaymentService ( apiKey )
242248
243249 // Verify payment is still in capturable state
244250 const status = await paymentService . getPaymentStatus ( paymentId )
@@ -309,19 +315,21 @@ export interface PaymentDispatcherArgs {
309315 component : PaymentField
310316 sourceUrl : string
311317 isLive : boolean
318+ isPreview : boolean
312319}
313320
314321/**
315322 * Session data stored when dispatching to GOV.UK Pay
316323 */
317324export interface PaymentSessionData {
318325 uuid : string
326+ formId : string
319327 reference : string
320328 amount : number
321329 description : string
322330 paymentId : string
323331 componentName : string
324332 returnUrl : string
325333 failureUrl : string
326- isLive : boolean
334+ isLivePayment : boolean
327335}
0 commit comments