UNPKG

2.55 kBJavaScriptView Raw
1const alertTypes = [
2 require('./alertTypes/high_risk_transaction_created'),
3 require('./alertTypes/high_risk_transaction_updated'),
4 require('./alertTypes/locker_processed'),
5 require('./alertTypes/new_audience_member'),
6 require('./alertTypes/payment_dispute_closed'),
7 require('./alertTypes/payment_dispute_created'),
8 require('./alertTypes/payment_refunded'),
9 require('./alertTypes/payment_succeeded'),
10 require('./alertTypes/subscription_cancelled'),
11 require('./alertTypes/subscription_created'),
12 require('./alertTypes/subscription_payment_failed'),
13 require('./alertTypes/subscription_payment_refunded'),
14 require('./alertTypes/subscription_payment_succeeded'),
15 require('./alertTypes/subscription_updated'),
16 require('./alertTypes/transfer_created'),
17 require('./alertTypes/transfer_paid'),
18 require('./alertTypes/update_audience_member')
19]
20
21function validate(payload){
22 return (req, _, next) => {
23 try {
24 const { body } = req
25 const alertName = body?.alert_name
26 if (!alertName) {
27 /* TODO: Store the payload in file or somewhere, to check and to be able to replicate the wrong request format*/
28 console.error(`Could not find the alert_name attribute in the request`)
29 next('Could not find the alert_name attribute in the request')
30 return false
31 }
32
33 /* TODO: implement some validation */
34 const alertType = alertTypes.find(alertType => alertType.alert_name?.default === alertName)
35
36 /*
37 TODO:
38 implement 'p_signature' validation https://developer.paddle.com/webhook-reference/ZG9jOjI1MzUzOTg2-verifying-webhooks
39 */
40
41 if (!alertType) {
42 /* TODO: Store the payload in file or somewhere, to check and to be able to replicate the wrong request format*/
43 console.error(`Could not find this alertType for alert with '${alertName}'.`)
44 console.error(`In case you need validation support for this hook please open an issue https://github.com/bozvary/paddle-webhooks-express/issues or create a PR at https://github.com/bozvary/paddle-webhooks-express.`)
45 next(`Could not find this alertType for alert with '${alertName}'.`)
46 return false
47 }else{
48 next()
49 return
50 }
51
52 } catch (err) {
53 /* TODO: Store the payload in file or somewhere, to check and to be able to replicate the wrong request format*/
54 console.error('catch')
55 next('catch', err)
56 }
57
58 next('Something went wrong')
59 }
60}
61
62/**
63 * Expose `validate`.
64 */
65
66module.exports.validate = validate
67