@@ -21,11 +21,11 @@ const keySecret = stripePrivateKey
2121
2222const stripeApp = stripe ( keySecret )
2323
24- billings . get ( '/' , function ( req , res ) {
24+ billings . get ( '/' , function ( req , res ) {
2525 return noEndpoint ( res )
2626} )
2727
28- billings . post ( '/' , function ( req , res ) {
28+ billings . post ( '/' , function ( req , res ) {
2929 const data = req . body
3030 if ( ! data || ! data . stripeCard ) return res . sendStatus ( 400 )
3131
@@ -40,58 +40,67 @@ billings.post('/', function (req, res) {
4040 console . log ( '@billings:data' , data )
4141 console . log ( '@billings:newCustomer' , newCustomer )
4242
43- stripeApp . customers . create ( newCustomer ) . then ( customer => {
44- console . log ( '@customers.create:customer' , customer )
45-
46- const charge = {
47- customer : customer . id ,
48- source : customer . default_source ,
49- amount : 1400 ,
50- currency : 'eur' ,
51- description : 'Radio4000 Premium'
52- }
53-
54- stripeApp . charges . create ( charge ) . then ( answer => {
55- console . log ( '@charges.charge:charge' , charge )
56- console . log ( '@charges.charge:answer' , answer )
57-
58- if ( answer . paid ) {
59- const db = admin . database ( )
60- const ref = db . ref ( `channels/${ radio4000ChannelId } ` )
61-
62- console . log ( 'radio4000ChannelId' , radio4000ChannelId )
43+ stripeApp . customers
44+ . create ( newCustomer )
45+ . then ( customer => {
46+ console . log ( '@customers.create:customer' , customer )
47+
48+ const charge = {
49+ customer : customer . id ,
50+ source : customer . default_source ,
51+ amount : 1400 ,
52+ currency : 'eur' ,
53+ description : 'Radio4000 Premium'
54+ }
6355
64- ref . child ( 'isPremium' )
65- . set ( true )
66- . then ( ( ) => {
67- res . status ( 200 ) . json ( {
68- message : 'charge sucess && channel.isPremium = true'
69- } )
70- } ) . catch ( completionError => {
71- console . log ( '@firebase:isPremium-c-error' , completionError )
56+ stripeApp . charges
57+ . create ( charge )
58+ . then ( answer => {
59+ console . log ( '@charges.charge:charge' , charge )
60+ console . log ( '@charges.charge:answer' , answer )
61+
62+ if ( answer . paid ) {
63+ const db = admin . database ( )
64+ const ref = db . ref ( `channels/${ radio4000ChannelId } ` )
65+
66+ console . log ( 'radio4000ChannelId' , radio4000ChannelId )
67+
68+ ref
69+ . child ( 'isPremium' )
70+ . set ( true )
71+ . then ( ( ) => {
72+ res . status ( 200 ) . json ( {
73+ message : 'charge sucess && channel.isPremium = true'
74+ } )
75+ } )
76+ . catch ( completionError => {
77+ console . log ( '@firebase:isPremium-c-error' , completionError )
78+ res . status ( 500 ) . json ( {
79+ message :
80+ 'charge error: card charged, but channel not upgraded to premium'
81+ } )
82+ } )
83+ } else {
84+ // send error response
85+ console . log ( 'answer.paid' , answer . paid )
7286 res . status ( 500 ) . json ( {
73- message : 'charge error: card charged, but channel not upgraded to premium '
87+ message : 'charge error, answer.paid = false '
7488 } )
89+ }
90+ } )
91+ . catch ( error => {
92+ console . log ( 'error charges.create' , error )
93+ res . status ( 500 ) . json ( {
94+ message : 'charge create error'
7595 } )
76- } else {
77- // send error response
78- console . log ( 'answer.paid' , answer . paid )
79- res . status ( 500 ) . json ( {
80- message : 'charge error, answer.paid = false'
8196 } )
82- }
83- } ) . catch ( error => {
84- console . log ( 'error charges .create' , error )
97+ } )
98+ . catch ( error => {
99+ console . log ( 'error customers .create' , error )
85100 res . status ( 500 ) . json ( {
86- message : 'charge create error'
101+ message : 'customer create error'
87102 } )
88103 } )
89- } ) . catch ( error => {
90- console . log ( 'error customers.create' , error )
91- res . status ( 500 ) . json ( {
92- message : 'customer create error'
93- } )
94- } )
95104} )
96105
97106/* Card object received
0 commit comments