import * as rp from 'request-promise' ; import * as functions from 'firebase-functions' ; import { stripe , db , auth , stripeClientId , stripeSecret } from './config' ; import * as CORS from 'cors' ; import * as qs from 'querystring' ; const cors = CORS ( { origin : true } ) ; export const redirect = functions . https . onRequest ( ( req , res ) => { const base = 'https://connect.stripe.com/oauth/authorize?' ; const queryParams = { client_id : stripeClientId , response_type : 'code' , scope : 'read_write' , } ; const endpoint = base + qs . stringify ( queryParams ) ; res . redirect ( endpoint ) ; } ) ; export const callback = functions . https . onRequest ( ( req , res ) => { cors ( req , res , ( ) => { const code = req . query . code ; mintFirebaseToken ( code ) . then ( token => res . status ( 200 ) . send ( { token } ) ) . catch ( err => res . status ( 400 ) . send ( err ) ) ; } ) ; } ) ; export async function mintFirebaseToken ( code ) { const options = { uri : 'https://connect.stripe.com/oauth/token' , form : { grant_type : 'authorization_code' , client_id : stripeClientId , code : code , client_secret : stripeSecret } } ; let stripeCredentials = await rp . post ( options ) ; stripeCredentials = JSON . parse ( stripeCredentials ) ; const accountId = stripeCredentials . stripe_user_id ; const uid = 'stripe:' + accountId ; const refreshToken = stripeCredentials . refresh_token ; const firebaseToken = await auth . createCustomToken ( uid ) ; const account = await stripe . accounts . retrieve ( accountId ) ; const userData = { uid , accountId , refreshToken , email : account . email , displayName : account . display_name , } ; await db . doc ( `users/${ uid }` ) . set ( userData , { merge : true } ) ; return firebaseToken ; }