Skip to content

Commit 010eb50

Browse files
backend of simple stripe
1 parent 6c8ae29 commit 010eb50

File tree

5 files changed

+460
-0
lines changed

5 files changed

+460
-0
lines changed

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "stripe-server",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"cors": "^2.8.5",
8+
"dotenv": "^8.0.0",
9+
"express": "^4.17.1",
10+
"stripe": "^7.1.0"
11+
}
12+
}

routes/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const paymentApi = require("./payment");
2+
3+
const configureRoutes = app => {
4+
paymentApi(app);
5+
};
6+
7+
module.exports = configureRoutes;

routes/payment.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
2+
3+
const stripeChargeCallback = res => (stripeErr, stripeRes) => {
4+
if (stripeErr) {
5+
res.status(500).send({ error: stripeErr });
6+
} else {
7+
res.status(200).send({ success: stripeRes });
8+
}
9+
};
10+
11+
const paymentApi = app => {
12+
app.get("/payment", (req, res) => {
13+
res.send({
14+
message: "Hello Stripe checkout server!",
15+
timestamp: new Date().toISOString()
16+
});
17+
});
18+
19+
app.post("/payment", (req, res) => {
20+
const body = {
21+
source: req.body.token.id,
22+
amount: req.body.amount,
23+
currency: "usd"
24+
};
25+
stripe.charges.create(body, stripeChargeCallback(res));
26+
});
27+
return app;
28+
};
29+
30+
module.exports = paymentApi;

server.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const express = require("express");
2+
const cors = require("cors");
3+
const bodyParser = require("body-parser");
4+
const dotenv = require("dotenv");
5+
6+
dotenv.config()
7+
8+
const app = express();
9+
10+
app.use(bodyParser.json());
11+
app.use(bodyParser.urlencoded({ extended: true }));
12+
app.use(cors());
13+
14+
const configureRoutes = require("./routes")
15+
16+
configureRoutes(app);
17+
18+
app.listen(8000, error => {
19+
if (error) throw error;
20+
console.log("Server running on port " + 8000);
21+
});

0 commit comments

Comments
 (0)