forked from vermilion-tech/stripe-charge-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (36 loc) · 1.08 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
// load dependencies
const dotenv = require("dotenv").config();
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
// configure variables from environments variables
const port = process.env.PORT || 8080;
const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
const stripeChargeDescription = process.env.STRIPE_CHARGE_DESCRIPTION;
// initialize express and stripe
const app = express();
const stripe = require("stripe")(stripeSecretKey);
// configure our middleware
app.use(morgan("combined"));
app.use(cors());
app.use(express.json());
// charge endpoint
app.post("/charge", async function(req, res) {
const { id, amount } = req.body;
try {
let { status } = await stripe.charges.create({
amount: amount * 100, // *100 because we sent dollar amount
currency: "usd",
description: stripeChargeDescription,
source: id
});
res.json({ status });
} catch (err) {
console.log(err);
res.status(500).end();
}
});
// listen on port
app.listen(port, function() {
console.log("Listening on port: " + port);
});