1
+ const express = require ( 'express' )
2
+ const bodyParser = require ( 'body-parser' ) ;
3
+ const nunjucks = require ( 'nunjucks' ) ;
4
+ const stripe = require ( 'stripe' ) ( 'sk_test_5wRrRkW71NRhb4cIHdRL9Z32' ) ;
5
+ const app = express ( ) ;
6
+
7
+ app . get ( '/' , ( req , res ) => {
8
+ res . send ( 'Hello World!' )
9
+ } ) ;
10
+
11
+ app . listen ( 8000 , ( ) => {
12
+ console . log ( 'Example app listening on port 8000!' )
13
+ } ) ;
14
+
15
+ // have res.render work with html files
16
+ app . set ( 'view engine' , 'html' ) ;
17
+ // when res.render works with html files, have it use nunjucks to do so
18
+
19
+ app . use ( express . static ( __dirname ) )
20
+ app . use ( bodyParser ( ) ) ;
21
+
22
+ app . engine ( 'html' , nunjucks . render ) ;
23
+ nunjucks . configure ( 'views' , { noCache : true } ) ;
24
+
25
+ const router = express . Router ( ) ;
26
+
27
+ app . use ( '/' , router )
28
+
29
+ router . get ( '/stripe-form' , function ( req , res , next ) {
30
+ res . render ( 'stripeForm' , { title : 'Stripe Form Title' } ) ;
31
+ } )
32
+
33
+ router . post ( '/stripe-information' , function ( req , res , next ) {
34
+ console . log ( 'Stripe token received: ' , req . body )
35
+ res . send ( "Stripe token received" )
36
+ } )
37
+
38
+ router . post ( '/stripe-information' , function ( req , res , next ) {
39
+ console . log ( 'stripe information received: ' , req . body )
40
+ stripe . customers . create ( {
41
+ description : 'My new customer' ,
42
+ source : req . body . stripeToken ,
43
+ } , function ( err , customer ) {
44
+ console . log ( 'err: ' , err , '|customer: ' , customer )
45
+ if ( err ) {
46
+ res . json ( err )
47
+ return
48
+ }
49
+ res . json ( customer )
50
+ return
51
+ } )
52
+ } )
0 commit comments