forked from fsjuhl/stripe-to-discord
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
151 lines (126 loc) Β· 3.52 KB
/
index.js
File metadata and controls
151 lines (126 loc) Β· 3.52 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// includes
require('dotenv').config();
const app = require('express')();
const bodyParser = require('body-parser');
const Discord = require('discord.js');
const t = require('./tools');
// stripe settings & includes
// Find your endpoint's secret in your Dashboard's webhook settings
const stripe = require('stripe')(process.env.API_KEY);
const endpointSecret = process.env.ENDPOINT_SECRET;
// convert webhook link to id/token pair
const [webhookId, webhookSecret] = process.env.PAYMENT_HOOK.split('/').slice(5);
const hook = new Discord.WebhookClient(webhookId, webhookSecret);
app.get('/', (request, response) => {
response.status(200).json({
response: true,
description: 'Stripe to discord by @darroneggins',
});
});
// Match the raw body to content type application/json
app.post(
'/webhook',
bodyParser.raw({ type: 'application/json' }),
(request, response) => {
let event;
let email;
let paymentData;
const sig = request.headers['stripe-signature'];
try {
event = stripe.webhooks.constructEvent(
request.body,
sig,
endpointSecret,
);
} catch (err) {
response.status(400).send(`Webhook Error: ${err.message} `);
}
const embed = t.makeEmbed();
// Handle the event
switch (event.type) {
case 'charge.succeeded':
paymentData = event.data.object;
console.log(paymentData);
email =
paymentData.billing_details.email != null
? paymentData.billing_details.email
: paymentData.description;
embed
.setColor('#77dd77')
.setThumbnail(t.gravatar(paymentData.description))
.setURL(
`https://dashboard.stripe.com/payments/${paymentData.id}`,
)
.addField(
`New Payment`,
paymentData.billing_details.name,
true,
)
.addField(
`Amount`,
new Intl.NumberFormat('en-US', {
style: 'currency',
currency: paymentData.currency,
}).format(paymentData.amount / 100),
true,
)
.addField(`Email`, email);
hook.send(embed);
return response.status(200).send(paymentData);
case 'charge.failed':
paymentData = event.data.object;
email =
paymentData.billing_details.email != null
? paymentData.billing_details.email
: paymentData.description;
embed
.setColor('#ff6961')
.setThumbnail(t.gravatar(paymentData.description))
.setURL(
`https://dashboard.stripe.com/payments/${paymentData.id}`,
)
.addField(
`Failed Payment`,
paymentData.billing_details.name,
true,
)
.addField(
`Amount`,
new Intl.NumberFormat('en-US', {
style: 'currency',
currency: paymentData.currency,
}).format(paymentData.amount / 100),
true,
)
.addField(`Email`, email);
hook.send(embed);
return response.status(200).send(paymentData);
case 'transfer.paid':
case 'payout.paid':
paymentData = event.data.object;
embed
.setColor('#00bfff')
.setThumbnail(t.gravatar(paymentData.description))
.setURL(
`https://dashboard.stripe.com/payouts/${paymentData.id}`,
)
.addField(
`Payout Paid`,
new Intl.NumberFormat('en-US', {
style: 'currency',
currency: paymentData.currency,
}).format(paymentData.amount / 100),
true,
);
hook.send(embed);
return response.status(200).send(paymentData);
default:
// Unexpected event type
return response.status(400).end();
}
},
);
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Express Server is now running on port ${port} `);
});