forked from Adyen/adyen-commercetools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.lambda.js
52 lines (48 loc) · 1.51 KB
/
index.lambda.js
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
import notificationHandler from './src/handler/notification/notification.handler.js'
import { getLogger } from './src/utils/logger.js'
import utils from './src/utils/commons.js'
import { getErrorCause, isRecoverableError } from './src/utils/error-utils.js'
import { getCtpProjectConfig, getAdyenConfig } from './src/utils/parser.js'
const logger = getLogger()
export const handler = async (event) => {
// Reason for this check: if AWS API Gateway is used then event.body is provided as a string payload.
const body = event.body ? JSON.parse(event.body) : event
const { notificationItems } = body
if (!notificationItems) {
const error = new Error('No notification received.')
logger.error(
{
notification: undefined,
err: error,
},
'Unexpected error when processing event'
)
throw error
}
try {
for (const notification of notificationItems) {
const ctpProjectConfig = getCtpProjectConfig(notification, event.rawPath)
const adyenConfig = getAdyenConfig(notification)
await notificationHandler.processNotification(
notification,
adyenConfig.enableHmacSignature,
ctpProjectConfig
)
}
} catch (err) {
const cause = getErrorCause(err)
logger.error(
{
notification: utils.getNotificationForTracking(notificationItems),
cause,
},
'Unexpected exception occurred.'
)
if (isRecoverableError(err)) {
throw err
}
}
return {
notificationResponse: '[accepted]',
}
}