UNPKG

4.25 kBJavaScriptView Raw
1/**
2 * IMPORTS
3 */
4
5var express = require('express');
6var router = express.Router();
7
8var validator = require('./validator');
9var WebhooksManager = require('./WebhooksManager')
10
11router.use(validator.validate());
12
13/*
14 * URLs for receiving webhooks
15 * You can add up to 5 endpoints to receive the events you've subscribed to.
16 * NOTE: Only the primary endpoint receives fields deemed to be sensitive, such as the subscription cancel URL.
17 * Check your subscribed events in your Seller Dashboard
18 * https://sandbox-vendors.paddle.com/alerts-webhooks
19 * https://vendors.paddle.com/alerts-webhooks
20 * All platform events that you are subscribed to are sent to all of the URL endpoints that you have defined, but we will only send sensitive fields to your primary endpoint.
21 * This prevents any third party integrations you may be using from having access to data that they may misuse.
22 * Sensitive Fields: cancel_url, update_url
23 * Check the log of fulfillment and alert webhooks sent from your account by going to Developer Tools > Events > Notification History
24 * https://sandbox-vendors.paddle.com/webhook/alerts
25 * https://vendors.paddle.com/webhook/alerts
26 * Currently, 17 events supported by Paddle.com
27 * Method: HTTP POST
28 * For webhook events / alerts, the server should respond within 10 seconds with a HTTP 200 status code to indicate the server has successfully received the message.
29 * If Paddle receive anything other than a HTTP 200 (or if there is no response within 10 seconds) Paddle's server will retry the call to the webhook URL every 15 minutes for a maximum of 3 days.
30 */
31router.post('/webhook', async function(req, res){
32 /* Idea: You can store the Webhook Payload */
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 /* We should not arrive here */
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 /*finally {
109 console.log('this')
110 }*/
111
112
113
114
115});
116
117/**
118 * Expose `router`.
119 */
120
121module.exports = router;
122