-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
51 lines (41 loc) · 1.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
const express = require("express")
const app = express()
const basicAuth = require("express-basic-auth")
const port = process.env.PORT || 3000
const stripeDomain = process.env.STRIPE_API_DOMAIN
if (!stripeDomain) {
throw new Error("Env STRIPE_API_DOMAIN is missing")
}
const stripeKey = process.env.STRIPE_API_KEY
if (!stripeKey) {
throw new Error("Env STRIPE_API_KEY is missing")
}
// This is a demo app, please don't actually move any money around, even in “production”
if (!stripeKey.startsWith("sk_test_")) {
throw new Error("ENV STRIPE_API_KEY must be a test key")
}
const stripeAccount = process.env.STRIPE_API_ACCOUNT
if (!stripeAccount) {
throw new Error("Env STRIPE_API_ACCOUNT is missing")
}
if (!stripeAccount.startsWith("acct_")) {
throw new Error("Env STRIPE_API_ACCOUNT must start with `acct_`")
}
const stripe = require("stripe")(stripeKey)
// Authentication middleware
app.use(
basicAuth({
users: { "me@example.com": "bicycle-whammy-snack-potato" },
}),
)
app.get("/", (req, res) => {
res.send("Hello world!")
})
app.get("/balance", async (req, res) => {
const balance = await stripe.balance.retrieve()
res.type("json")
res.send(balance)
})
app.listen(port, () => {
console.log("Demo app listening on port " + port)
})