Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (83 loc) · 2.88 KB
/
Copy pathserver.js
File metadata and controls
95 lines (83 loc) · 2.88 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
const express = require("express");
const app = express();
const { resolve } = require("path");
// Replace if using a different env file or config
const ENV_PATH = "../../.env";
const envPath = resolve(ENV_PATH);
const env = require("dotenv").config({ path: envPath });
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
app.use(express.static(process.env.STATIC_DIR));
app.use(
express.json({
// We need the raw body to verify webhook signatures.
// Let's compute it only when hitting the Stripe webhook endpoint.
verify: function(req, res, buf) {
if (req.originalUrl.startsWith("/webhook")) {
req.rawBody = buf.toString();
}
}
})
);
app.get("/", (req, res) => {
const path = resolve(process.env.STATIC_DIR + "/index.html");
res.sendFile(path);
});
app.get("/public-key", (req, res) => {
res.send({ publicKey: process.env.STRIPE_PUBLIC_KEY });
});
app.post("/create-setup-intent", async (req, res) => {
res.send(await stripe.setupIntents.create());
});
app.post("/create-customer", async (req, res) => {
// This creates a new Customer and attaches the PaymentMethod in one API call.
const customer = await stripe.customers.create({
payment_method: req.body.payment_method
});
// At this point, associate the ID of the Customer object with your
// own internal representation of a customer, if you have one.
res.send(customer);
});
// Webhook handler for asynchronous events.
app.post("/webhook", async (req, res) => {
let data;
let eventType;
// Check if webhook signing is configured.
if (process.env.STRIPE_WEBHOOK_SECRET) {
// Retrieve the event by verifying the signature using the raw body and secret.
let event;
let 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);
}
// Extract the object from the event.
data = event.data;
eventType = event.type;
} else {
// Webhook signing is recommended, but if the secret is not configured in `config.js`,
// retrieve the event data directly from the request body.
data = req.body.data;
eventType = req.body.type;
}
if (eventType === "setup_intent.setup_failed") {
console.log(
`🔔 Occurs when a SetupIntent has failed the attempt to setup a payment method. ${data}`
);
}
if (eventType === "setup_intent.succeeded") {
console.log(
`🔔 Occurs when an SetupIntent has successfully setup a payment method. ${data}`
);
}
if (eventType === "setup_intent.created") {
console.log(`🔔 Occurs when a new SetupIntent is created. ${data}`);
}
res.sendStatus(200);
});
app.listen(4242, () => console.log(`Node server listening on port ${4242}!`));