How to use Firebase cloud functions as a backend for Stripe Connect in an Angular Project.
$ firebase init
Then select Firebase functions.
Move into the newly created functions directory and install the stripe package.
$ cd functions
$ npm install --save stripe
Adding the stripe import and creating our first function.
import * as cors from 'cors';
const corsHandler = cors({origin: true}); //make more secure in production
const stripe = require('stripe')('sk_test_YOUR_KEY_HERE');
export const createExpressAccount = functions.https.onRequest(async (request, response) => {
corsHandler(request, response, async () => {
const account = await stripe.accounts.create({
type: 'express',
});
response.send(account);
});
});
$ firebase deploy --only functions
You'll see the endpoint after uploading the function in the console. It's also accessible in the Firebase console.
createStripeAccount(){
console.log('sending request for account creation');
this.http.get('https://YOUR-APP-HERE.cloudfunctions.net/createExpressAccount')
.toPromise().then(result=>{
console.log(result);
//do something with result
}).catch(error =>{
console.log(error);
});
}