-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (109 loc) · 3.23 KB
/
index.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
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
const express = require('express')
const { randomBytes } = require('crypto')
const { promisify } = require('util')
const app = express()
require('dotenv').config({ path: './.env' })
const opts = {
maxNetworkRetries: 2,
telemetry: true
}
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY, opts)
const { create } = require('./db')
const generateKey = async (n) => {
const rB = promisify(randomBytes)
const out = await rB(n).toString('base64').replace(/\W/g, '')
return out
}
app.use(
express.json({
verify: (req, res, buf) => {
if (req.originalUrl.startsWith('/webhook')) {
req.rawBody = buf.toString()
}
}
})
)
app.get('/', (req, res) => {
res.json({ status: 'ok' })
})
app.post('/create-customer', async (req, res) => {
const customer = await stripe.customers.create({
payment_method: req.body.payment_method,
email: req.body.email,
invoice_settings: {
default_payment_method: req.body.payment_method
}
}).catch((e) => res.json({ error: e.message }))
// generate api key with appropriate access
const key = await generateKey(96)
await create('customers', customer.id, customer).catch((e) => console.log(e))
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [{ plan: req.body.planId }],
expand: ['latest_invoice.payment_intent']
}).catch((e) => res.json({ error: e.message }))
res.send(subscription)
})
app.post('/get-subscription', async (req, res) => {
const subscription = await stripe.subscriptions.retrieve(req.body.subscriptionId)
.catch((e) => res.json({ error: e.message }))
res.send(subscription)
})
app.post('/cancel-subscription', async (req, res) => {
const confirmation = await stripe.subscriptions.del(req.body.subscriptionId)
.catch((e) => res.json({ error: e.message }))
res.send(confirmation)
})
app.post('/webhook', async (req, res) => {
let dataObject
let eventType
if (process.env.STRIPE_WEBHOOK_SECRET) {
let event
const signature = req.headers['stripe-signature']
try {
event = stripe.webhooks.constructEvent(
req.rawBody,
signature,
process.env.STRIPE_WEBHOOK_SECRET
)
} catch (err) {
console.log('Webhook signature verification failed.')
return res.sendStatus(400)
}
dataObject = event.data.object
eventType = event.type
switch (event.type) {
case 'customer.created':
console.log(dataObject)
break
case 'customer.updated':
console.log(dataObject)
break
case 'invoice.upcoming':
console.log(dataObject)
break
case 'invoice.created':
console.log(dataObject)
break
case 'invoice.finalized':
console.log(dataObject)
break
case 'invoice.payment_succeeded':
console.log(dataObject)
break
case 'invoice.payment_failed':
console.log(dataObject)
break
case 'customer.subscription.created':
console.log(dataObject)
break
default:
return res.status(400).end()
}
} else {
dataObject = req.body.data
eventType = req.body.type
}
res.sendStatus(200)
})
app.listen(3000, () => console.log('Node server listening on port 3000!'))