Search code examples
node.jsfirebasegoogle-cloud-functionsstripe-paymentssinon

Can't export stripe API with firebase


I have a project using firebase. In functions/index.js I have the following code near the top with the imports. This initializes a stripe api instance with the production API key in my firebase config.

const stripeProd = require('stripe')(functions.config().stripe.secret)

In my test files I uses Sinon for stubbing and mocking.

At the very bottom of functions/index.js I export my express app and the instance of the stripe api shown above:

exports.app = functions.runWith(runtimeOptions).https.onRequest(app)

// export these so they can be stubbed in UTs
exports.stripeProd = stripeProd
//.... other exports

This export let's me successfully stub out stripe so my UTs don't make API calls.

However, when I try to start this with firebase serve I get the following error:

 ❮❮❮ firebase serve

// ... stuff
i  hosting: Serving hosting files from: public
✔  hosting: Local server: http://localhost:5000
⚠  functions: Maximum call stack size exceeded
⚠  Your function was killed because it raised an unhandled error.

I've found that if I comment out the line exporting the stripe api this error goes away. Why does this happen?


Solution

  • try this:

    const Stripe = require('stripe');
    const stripeProd = new Stripe(functions.config().stripe.key, {
      apiVersion: '2020-08-27' //this value should be whatever api version you are using
    });
    
    //...
    
    exports.stripeProd = stripeProd

    I hope it helps, I also recommend to import your stripe functions from other files, to work more organized.