File tree Expand file tree Collapse file tree 5 files changed +460
-0
lines changed Expand file tree Collapse file tree 5 files changed +460
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ const paymentApi = require ( "./payment" ) ;
2+
3+ const configureRoutes = app => {
4+ paymentApi ( app ) ;
5+ } ;
6+
7+ module . exports = configureRoutes ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments