Skip to content

Commit

Permalink
feat: #11357 payments fields updates (#11360)
Browse files Browse the repository at this point in the history
* feat: #11357 payments fields updates

* feat: #11357 updates to authorizers to work in both worlds
  • Loading branch information
willmcvay authored Aug 8, 2024
1 parent 5a343f0 commit 9ff0866
Show file tree
Hide file tree
Showing 44 changed files with 581 additions and 66 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added .yarn/cache/fsevents-patch-19706e7e35-10.zip
Binary file not shown.
Binary file added .yarn/cache/fsevents-patch-6b67494872-10.zip
Binary file not shown.
7 changes: 3 additions & 4 deletions packages/payments-client/src/components/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export const AdminPage: FC = () => {
const { Modal: FilePreviewModal, openModal: openFilePreviewModal, closeModal: closeFilePreviewModal } = useModal()
const { config, refreshConfig, clearConfigCache, configLoading } = useConfigState()
const configNotConfigured = !config?.isConfigured

const clientCode = connectSession?.loginIdentity?.clientId ?? ''
const idToken = connectSession?.idToken ?? ''

Expand All @@ -141,8 +140,8 @@ export const AdminPage: FC = () => {
action: updateActions[UpdateActionNames.paymentsClientConfigDelete],
method: 'DELETE',
headers: {
Authorization: idToken,
'reapit-customer': clientCode,
'reapit-id-token': idToken,
'reapit-app-id': process.env.appId,
},
uriParams: {
Expand All @@ -155,8 +154,8 @@ export const AdminPage: FC = () => {
action: updateActions[UpdateActionNames.paymentsClientConfigCreate],
method: 'POST',
headers: {
Authorization: idToken,
'reapit-customer': clientCode,
'reapit-id-token': idToken,
'reapit-app-id': process.env.appId,
},
uriParams: {
Expand All @@ -169,8 +168,8 @@ export const AdminPage: FC = () => {
action: updateActions[UpdateActionNames.paymentsClientConfigUpdate],
method: 'PATCH',
headers: {
Authorization: idToken,
'reapit-customer': clientCode,
'reapit-id-token': idToken,
'reapit-app-id': process.env.appId,
},
uriParams: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,23 @@ exports[`Payment should match a snapshot with data 1`] = `
</mock-styled.label>
</mock-styled.div>
</mock-styled.div>
<mock-styled.div>
<mock-styled.div>
<mock-styled.input
classname=""
disabled="false"
id="test-static-id"
name="customerPhone"
placeholder="Phone Number here"
type="tel"
/>
<mock-styled.label
htmlfor="test-static-id"
>
Customer Phone Number
</mock-styled.label>
</mock-styled.div>
</mock-styled.div>
<mock-styled.div>
<mock-styled.div>
<mock-styled.input
Expand Down Expand Up @@ -2622,6 +2639,23 @@ exports[`Payment should match a snapshot with data 1`] = `
</mock-styled.label>
</mock-styled.div>
</mock-styled.div>
<mock-styled.div>
<mock-styled.div>
<mock-styled.input
classname=""
disabled="false"
id="test-static-id"
name="customerPhone"
placeholder="Phone Number here"
type="tel"
/>
<mock-styled.label
htmlfor="test-static-id"
>
Customer Phone Number
</mock-styled.label>
</mock-styled.div>
</mock-styled.div>
<mock-styled.div>
<mock-styled.div>
<mock-styled.input
Expand Down
19 changes: 9 additions & 10 deletions packages/payments-client/src/components/payment/payment.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useState, useEffect, FC, Dispatch, SetStateAction } from 'react'
import { useReapitConnect } from '@reapit/connect-session'
import { reapitConnectBrowserSession } from '../../core/connect-session'
import { PaymentModel, PropertyModel } from '@reapit/foundations-ts-definitions'
import { elFadeIn, Loader, PersistentNotification } from '@reapit/elements'
Expand Down Expand Up @@ -27,6 +26,7 @@ import {
} from '@reapit/use-reapit-data'
import { useConfigState } from '../../core/use-config-state'
import dayjs from 'dayjs'
import { useReapitConnect } from '@reapit/connect-session'

export interface PaymentProps {
paymentRequest: ReapitGetState<PaymentModel>
Expand Down Expand Up @@ -112,8 +112,7 @@ export const Payment: FC<PaymentProps> = ({ paymentRequest }) => {
const { connectSession } = useReapitConnect(reapitConnectBrowserSession)
const { config, configLoading } = useConfigState()
const configNotConfigured = !config?.isConfigured

const clientCode = connectSession?.loginIdentity?.clientId
const clientCode = config?.clientCode ?? ''
const idToken = connectSession?.idToken ?? ''

const [payment, paymentLoading, , refreshPayment] = paymentRequest
Expand All @@ -135,8 +134,8 @@ export const Payment: FC<PaymentProps> = ({ paymentRequest }) => {
action: updateActions[UpdateActionNames.submitPrivatePaymentReceipt],
method: 'POST',
headers: {
Authorization: idToken,
'reapit-customer': clientCode as string,
'reapit-customer': clientCode,
'reapit-id-token': idToken,
'reapit-app-id': process.env.appId,
},
uriParams: {
Expand All @@ -162,8 +161,8 @@ export const Payment: FC<PaymentProps> = ({ paymentRequest }) => {
method: 'POST',
returnType: UpdateReturnTypeEnum.RESPONSE,
headers: {
Authorization: idToken,
'reapit-customer': clientCode as string,
'reapit-customer': clientCode,
'reapit-id-token': idToken,
'reapit-app-id': process.env.appId,
},
})
Expand All @@ -174,13 +173,13 @@ export const Payment: FC<PaymentProps> = ({ paymentRequest }) => {
method: 'POST',
returnType: UpdateReturnTypeEnum.RESPONSE,
headers: {
Authorization: idToken,
'reapit-customer': clientCode as string,
'reapit-customer': clientCode,
'reapit-id-token': idToken,
'reapit-app-id': process.env.appId,
},
})

useEffect(handleGetMerchantKey(getMerchantKey, idToken, config), [config, idToken])
useEffect(handleGetMerchantKey(getMerchantKey, idToken, config), [idToken, config])

useEffect(handleMerchantKeyRefresh(merchantKey, getMerchantKey, payment, config), [merchantKey, payment, config])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export const PaymentRequestModal: FC<PaymentRequestModalProps> = ({
selectedPayment,
}) => {
const { connectSession } = useReapitConnect(reapitConnectBrowserSession)
const clientCode = connectSession?.loginIdentity.clientId
const clientCode = connectSession?.loginIdentity.clientId ?? ''
const idToken = connectSession?.idToken ?? ''

const {
register,
Expand All @@ -154,8 +155,8 @@ export const PaymentRequestModal: FC<PaymentRequestModalProps> = ({
method: 'POST',
returnType: UpdateReturnTypeEnum.RESPONSE,
headers: {
Authorization: connectSession?.idToken as string,
'reapit-customer': clientCode as string,
'reapit-customer': clientCode,
'reapit-id-token': idToken,
'reapit-app-id': process.env.appId,
},
})
Expand All @@ -165,8 +166,8 @@ export const PaymentRequestModal: FC<PaymentRequestModalProps> = ({
action: updateActions[UpdateActionNames.paymentRequestCreate],
method: 'POST',
headers: {
Authorization: connectSession?.idToken as string,
'reapit-customer': clientCode as string,
'reapit-customer': clientCode,
'reapit-id-token': idToken,
'reapit-app-id': process.env.appId,
},
uriParams: {
Expand All @@ -179,7 +180,7 @@ export const PaymentRequestModal: FC<PaymentRequestModalProps> = ({
action: updateActions[UpdateActionNames.privatePaymentUpdate],
method: 'PATCH',
headers: {
'if-match': selectedPayment?._eTag as string,
'if-match': selectedPayment?._eTag ?? '',
},
uriParams: {
paymentId: selectedPayment?.id,
Expand Down
6 changes: 3 additions & 3 deletions packages/payments-client/src/core/use-config-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const { Provider } = ConfigStateContext
export const ConfigProvider: FC<PropsWithChildren> = ({ children }) => {
const [configLoading, setConfigLoading] = useState<boolean>(true)
const { connectSession } = useReapitConnect(reapitConnectBrowserSession)
const clientCode = connectSession?.loginIdentity?.clientId
const clientCode = connectSession?.loginIdentity?.clientId ?? ''
const idToken = connectSession?.idToken ?? ''

const [config, , , refreshConfig, , clearConfigCache] = useReapitGet<ClientConfigModel>({
reapitConnectBrowserSession,
action: getActions[GetActionNames.getPaymentsClientConfig],
headers: {
Authorization: idToken,
'reapit-customer': clientCode as string,
'reapit-customer': clientCode,
'reapit-id-token': idToken,
'reapit-app-id': process.env.appId,
},
uriParams: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,23 @@ exports[`PaymentPage should match a snapshot when has a session and data 1`] = `
</mock-styled.label>
</mock-styled.div>
</mock-styled.div>
<mock-styled.div>
<mock-styled.div>
<mock-styled.input
classname=""
disabled="false"
id="test-static-id"
name="customerPhone"
placeholder="Phone Number here"
type="tel"
/>
<mock-styled.label
htmlfor="test-static-id"
>
Customer Phone Number
</mock-styled.label>
</mock-styled.div>
</mock-styled.div>
<mock-styled.div>
<mock-styled.div>
<mock-styled.input
Expand Down Expand Up @@ -2396,6 +2413,23 @@ exports[`PaymentPage should match a snapshot when has a session and data 1`] = `
</mock-styled.label>
</mock-styled.div>
</mock-styled.div>
<mock-styled.div>
<mock-styled.div>
<mock-styled.input
classname=""
disabled="false"
id="test-static-id"
name="customerPhone"
placeholder="Phone Number here"
type="tel"
/>
<mock-styled.label
htmlfor="test-static-id"
>
Customer Phone Number
</mock-styled.label>
</mock-styled.div>
</mock-styled.div>
<mock-styled.div>
<mock-styled.div>
<mock-styled.input
Expand Down
35 changes: 24 additions & 11 deletions packages/payments-service/cdk/cdk-stack.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import * as path from 'path'
import {
createApi,
createBaseStack,
createTable,
createFunction,
output,
getAuthorizer,
} from '@reapit/ts-scripts/src/cdk'
import { aws_apigateway as apigateway, aws_iam as iam, aws_lambda } from 'aws-cdk-lib'
import { createApi, createBaseStack, createTable, createFunction, output } from '@reapit/ts-scripts/src/cdk'
import { aws_apigateway as apigateway, aws_iam as iam, aws_lambda, Duration } from 'aws-cdk-lib'
import config from '../config.json'
import { PolicyStatement } from 'aws-cdk-lib/aws-iam'

Expand Down Expand Up @@ -39,6 +32,7 @@ export const createStack = async () => {
'reapit-customer',
'reapit-app-id',
'reapit-session',
'reapit-id-token',
])

const paymentsSessionTable = createTable(stack, config.DYNAMO_DB_PAYMENTS_SESSION_TABLE_NAME, 'id')
Expand All @@ -62,6 +56,24 @@ export const createStack = async () => {
aws_lambda.Runtime.NODEJS_18_X,
)

const lambdaAuthorizer = createFunction(
stack,
'payments-service-authorizer-lambda',
path.resolve(__dirname, '..', 'dist'),
'packages/payments-service/src/core/authorizer.handler',
env,
undefined,
undefined,
undefined,
aws_lambda.Runtime.NODEJS_18_X,
)

const authorizer = new apigateway.RequestAuthorizer(stack, 'payments-service-authorizer', {
handler: lambdaAuthorizer,
identitySources: [apigateway.IdentitySource.header('authorization')],
resultsCacheTtl: Duration.seconds(0),
})

paymentsSessionTable.grantReadWriteData(lambda)
paymentsConfigTable.grantReadWriteData(lambda)

Expand Down Expand Up @@ -172,13 +184,14 @@ export const createStack = async () => {

routes.forEach((route) => {
api.root.resourceForPath(route.path).addMethod(route.method, new apigateway.LambdaIntegration(lambda), {
authorizer: route.protected ? getAuthorizer(stack, config.CONNECT_USER_POOL) : undefined,
authorizationType: route.protected ? apigateway.AuthorizationType.COGNITO : undefined,
authorizer: route.protected ? authorizer : undefined,
authorizationType: route.protected ? apigateway.AuthorizationType.CUSTOM : undefined,
apiKeyRequired: !route.protected ? true : undefined,
requestParameters: {
'method.request.header.Content-Type': false,
'method.request.header.Authorization': route.protected ? true : false,
'method.request.header.X-Api-Key': !route.protected ? true : false,
'method.request.header.reapit-id-token': route.protected ? true : false,
'method.request.header.api-version': false,
'method.request.header.if-match': false,
'method.request.header.reapit-customer': true,
Expand Down
1 change: 1 addition & 0 deletions packages/payments-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"dayjs": "^1.11.10",
"express": "^4.18.3",
"express-session": "^1.18.0",
"idtoken-verifier": "^2.2.4",
"memorystore": "^1.6.7",
"morgan": "^1.10.0",
"rxjs": "^7.8.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ describe('ClientConfigPrivateController', () => {
})

const result = await controller.getConfig({
clientCode: 'SBOX',
'reapit-customer': 'SBOX',
'reapit-id-token': 'MOCK_TOKEN',
})

expect(result).toEqual(mockConfigCreateModel)
Expand All @@ -48,7 +49,8 @@ describe('ClientConfigPrivateController', () => {

const result = await controller.createConfig(
{
clientCode: 'SBOX',
'reapit-customer': 'SBOX',
'reapit-id-token': 'MOCK_TOKEN',
},
mockConfigCreateModel,
)
Expand All @@ -65,7 +67,8 @@ describe('ClientConfigPrivateController', () => {

const result = await controller.updateConfig(
{
clientCode: 'SBOX',
'reapit-customer': 'SBOX',
'reapit-id-token': 'MOCK_TOKEN',
},
mockConfigCreateModel,
)
Expand All @@ -82,7 +85,8 @@ describe('ClientConfigPrivateController', () => {

const result = await controller.deleteConfig(
{
clientCode: 'SBOX',
'reapit-customer': 'SBOX',
'reapit-id-token': 'MOCK_TOKEN',
},
{ configId: 'MOCK_ID' },
)
Expand Down
10 changes: 10 additions & 0 deletions packages/payments-service/src/client-config/dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ export class ClientConfigPublicHeaders {
'reapit-session': string
}

export class ClientConfigPrivateHeaders {
@IsString()
@IsNotEmpty()
'reapit-customer': string

@IsString()
@IsNotEmpty()
'reapit-id-token': string
}

export class ClientConfigPublicParams {
@IsString()
@IsNotEmpty()
Expand Down
Loading

0 comments on commit 9ff0866

Please sign in to comment.