1 |
|
2 |
|
3 |
|
4 |
|
5 | var express = require('express');
|
6 | var router = express.Router();
|
7 |
|
8 | var validator = require('./validator');
|
9 | var WebhooksManager = require('./WebhooksManager')
|
10 |
|
11 | router.use(validator.validate());
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | router.post('/webhook', async function(req, res){
|
32 |
|
33 | try {
|
34 | await new WebhooksManager().default(req.body)
|
35 | switch (req.body.alert_name) {
|
36 | case 'high_risk_transaction_created':
|
37 | await new WebhooksManager().high_risk_transaction_created(req.body)
|
38 | break;
|
39 | case 'high_risk_transaction_updated':
|
40 | await new WebhooksManager().high_risk_transaction_updated(req.body)
|
41 | break;
|
42 | case 'locker_processed':
|
43 | await new WebhooksManager().locker_processed(req.body)
|
44 | break;
|
45 | case 'new_audience_member':
|
46 | await new WebhooksManager().new_audience_member(req.body)
|
47 | break;
|
48 | case 'payment_dispute_closed':
|
49 | await new WebhooksManager().payment_dispute_closed(req.body)
|
50 | break;
|
51 | case 'payment_dispute_created':
|
52 | await new WebhooksManager().payment_dispute_created(req.body)
|
53 | break;
|
54 | case 'payment_refunded':
|
55 | await new WebhooksManager().payment_refunded(req.body)
|
56 | break;
|
57 | case 'payment_succeeded':
|
58 | await new WebhooksManager().payment_succeeded(req.body)
|
59 | break;
|
60 | case 'subscription_cancelled':
|
61 | await new WebhooksManager().subscription_cancelled(req.body)
|
62 | break;
|
63 | case 'subscription_created':
|
64 | await new WebhooksManager().subscription_created(req.body)
|
65 | break;
|
66 | case 'subscription_payment_failed':
|
67 | await new WebhooksManager().subscription_payment_failed(req.body)
|
68 | break;
|
69 | case 'subscription_payment_refunded':
|
70 | await new WebhooksManager().subscription_payment_refunded(req.body)
|
71 | break;
|
72 | case 'subscription_payment_succeeded':
|
73 | await new WebhooksManager().subscription_payment_succeeded(req.body)
|
74 | break;
|
75 | case 'subscription_updated':
|
76 | await new WebhooksManager().subscription_updated(req.body)
|
77 | break;
|
78 | case 'transfer_created':
|
79 | await new WebhooksManager().transfer_created(req.body)
|
80 | break;
|
81 | case 'transfer_paid':
|
82 | await new WebhooksManager().transfer_paid(req.body)
|
83 | break;
|
84 | case 'update_audience_member':
|
85 | await new WebhooksManager().update_audience_member(req.body)
|
86 | break;
|
87 | default:
|
88 |
|
89 | console.error('Paddle.com Router. We should not arrive here')
|
90 | break;
|
91 | }
|
92 |
|
93 | res.json({
|
94 | message: 'Validation was successful'
|
95 | });
|
96 |
|
97 | }
|
98 | catch(err) {
|
99 | console.error(err.message)
|
100 | res.json({
|
101 | error: {
|
102 | em: err.message,
|
103 | ec: err.code
|
104 | },
|
105 | message: 'Validation was not successful'
|
106 | });
|
107 | }
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | });
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | module.exports = router;
|
122 |
|