import { Request , Response , NextFunction } from 'express' ; import { getAllAddressInfoFromDatabase , getAllVerifiedAddressInfoFromDatabase , getIdentityKeyFromDatabase , } from '../data-access/payIds' ; import createMemo from '../hooks/memo' ; import { formatPaymentInfo , getPreferredAddressHeaderPair , } from '../services/basePayId' ; import { parseAcceptHeaders } from '../services/headers' ; import metrics from '../services/metrics' ; import { urlToPayId , constructUrl } from '../services/urls' ; import { LookupError , LookupErrorType } from '../utils/errors' ; param req param res param next returns throws export default async function getPaymentInfo ( req , res , next ) { const payIdUrl = constructUrl ( req . protocol , req . hostname , req . url ) ; const payId = urlToPayId ( payIdUrl ) ; const parsedAcceptHeaders = parseAcceptHeaders ( req . accepts ( ) ) ; const [ allAddressInfo , allVerifiedAddressInfo , identityKey , ] = await Promise . all ( [ getAllAddressInfoFromDatabase ( payId ) , getAllVerifiedAddressInfoFromDatabase ( payId ) , getIdentityKeyFromDatabase ( payId ) . catch ( ( _err ) => { throw new LookupError ( `PayID ${ payId } could not be found.` , LookupErrorType . MissingPayId , parsedAcceptHeaders ) ; } ) , ] ) ; const [ preferredHeader , preferredAddresses , verifiedPreferredAddresses , ] = getPreferredAddressHeaderPair ( allAddressInfo , allVerifiedAddressInfo , parsedAcceptHeaders ) ; if ( ! preferredHeader ) { throw new LookupError ( `Payment information for ${ payId } could not be found.` , LookupErrorType . MissingAddress , parsedAcceptHeaders ) ; } const formattedPaymentInfo = formatPaymentInfo ( preferredAddresses , verifiedPreferredAddresses , identityKey , res . get ( 'PayID-Version' ) , payId , createMemo ) ; res . set ( 'Content-Type' , preferredHeader . mediaType ) ; res . locals . payId = payId ; res . locals . paymentInformation = formattedPaymentInfo ; res . locals . response = formattedPaymentInfo ; metrics . recordPayIdLookupResult ( true , preferredHeader . paymentNetwork , preferredHeader . environment ) ; return next ( ) ; }