11const express = require ( 'express' )
2- const stripe = require ( 'stripe' )
32const admin = require ( 'firebase-admin' )
43const functions = require ( 'firebase-functions' )
5- const config = require ( '../utils/config' )
4+ const stripe = require ( 'stripe' )
5+ const config = require ( '../config' )
6+ const noEndpoint = require ( '../utils/no-endpoint' )
67
78const billings = express . Router ( )
89
9- const keyPublishable = config . stripePublicKey ;
10- const keySecret = config . stripePrivateKey ;
10+ let stripePrivateKey = functions . config ( ) . stripe . private_key
11+ let stripePublicKey = functions . config ( ) . stripe . public_key
12+ if ( process . env . NODE_ENV === 'production' ) {
13+ stripePrivateKey = functions . config ( ) . stripe . production_private_key
14+ stripePublicKey = functions . config ( ) . stripe . production_public_key
15+ }
16+
17+ const keySecret = stripePrivateKey
18+ // const keyPublishable = stripePublicKey
1119
1220const stripeApp = stripe ( keySecret )
1321
14- billings . get ( '/' , function ( req , res ) {
15- res . json ( {
16- error : 'this endpoint does not exist, check usage on the documentation'
17- } )
18- } ) ;
22+ billings . get ( '/' , function ( req , res ) {
23+ return noEndpoint ( res )
24+ } )
1925
2026billings . post ( '/' , function ( req , res ) {
2127 const data = req . body
22- if ( ! data || ! data . stripeCard ) return res . sendStatus ( 400 )
28+ if ( ! data || ! data . stripeCard ) return res . sendStatus ( 400 )
2329
24- const amount = 1400 ;
25- const { stripeCard, radio4000ChannelId } = data ;
30+ const amount = 1400
31+ const { stripeCard, radio4000ChannelId } = data
2632
2733 const newCustomer = {
28- email : stripeCard . email ,
29- source : stripeCard . id
30- }
34+ email : stripeCard . email ,
35+ source : stripeCard . id
36+ }
3137
3238 console . log ( '@billings:data' , data )
33- console . log ( '@billings:newCustomer' , newCustomer ) ;
39+ console . log ( '@billings:newCustomer' , newCustomer )
3440
35- stripeApp . customers . create ( newCustomer ) . then ( customer => {
36- console . log ( '@customers.create:customer' , customer ) ;
41+ stripeApp . customers . create ( newCustomer ) . then ( customer => {
42+ console . log ( '@customers.create:customer' , customer )
3743
3844 const charge = {
3945 customer : customer . id ,
4046 source : customer . default_source ,
4147 amount : 1400 ,
42- currency : " eur" ,
43- description : " Radio4000 Premium" ,
48+ currency : ' eur' ,
49+ description : ' Radio4000 Premium'
4450 }
4551
4652 stripeApp . charges . create ( charge ) . then ( answer => {
4753 console . log ( '@charges.charge:charge' , charge )
4854 console . log ( '@charges.charge:answer' , answer )
4955
50- if ( answer . paid ) {
51- var db = admin . database ( ) ;
52- var ref = db . ref ( `channels/${ radio4000ChannelId } ` ) ;
56+ if ( answer . paid ) {
57+ const db = admin . database ( )
58+ const ref = db . ref ( `channels/${ radio4000ChannelId } ` )
5359
5460 console . log ( 'radio4000ChannelId' , radio4000ChannelId )
5561
5662 ref . child ( 'isPremium' )
57- . set ( true )
58- . then ( completion => {
59-
60- res . status ( 200 ) . json ( {
61- message : 'charge sucess && channel.isPremium = true'
62- } )
63-
64- } ) . catch ( completionError => {
65- console . log ( '@firebase:isPremium-c-error' , completionError )
66- res . status ( 500 ) . json ( {
67- message : 'charge error: card charged, but channel not upgraded to premium'
68- } )
69- } )
63+ . set ( true )
64+ . then ( ( ) => {
65+ res . status ( 200 ) . json ( {
66+ message : 'charge sucess && channel.isPremium = true'
67+ } )
68+ } ) . catch ( completionError => {
69+ console . log ( '@firebase:isPremium-c-error' , completionError )
70+ res . status ( 500 ) . json ( {
71+ message : 'charge error: card charged, but channel not upgraded to premium'
72+ } )
73+ } )
7074 } else {
7175 // send error response
7276 console . log ( 'answer.paid' , answer . paid )
@@ -75,13 +79,13 @@ billings.post('/', function (req, res) {
7579 } )
7680 }
7781 } ) . catch ( error => {
78- console . log ( 'error charges.create' , error ) ;
82+ console . log ( 'error charges.create' , error )
7983 res . status ( 500 ) . json ( {
8084 message : 'charge create error'
8185 } )
82- } ) ;
86+ } )
8387 } ) . catch ( error => {
84- console . log ( 'error customers.create' , error ) ;
88+ console . log ( 'error customers.create' , error )
8589 res . status ( 500 ) . json ( {
8690 message : 'customer create error'
8791 } )
@@ -112,6 +116,6 @@ billings.post('/', function (req, res) {
112116 name: 'hu@hu.hu',
113117 tokenization_method: null
114118 }
115- */
119+ */
116120
117121module . exports = billings
0 commit comments