Search code examples
node.jsfirebasegoogle-cloud-functionsbraintree

Braintree integration - gateway.transaction.sale never reaches err, result function


I'm using Firebase functions to integrate Braintree server-side. I've checked Firebase cloud functions with other functions and they are working fine. So, this is definitely something to do with Braintree. I checked Braintree nodejs implementation guide in github and my code is fine. https://github.com/braintree/braintree_node

The problem is the function always goes to catch with unexpectedError. The code never reaches then part of the code.

exports.setNonce = functions.https.onRequest((req, res) => {

// Grab the parameters.
if (req.body.nonce === undefined) {
    // This is an error case, as "message" is required
    return res.status(400).send('No nonce defined!');
} else {
    return gateway.transaction.sale({
        amount: '5.00',
        paymentMethodNonce: 'fake-valid-nonce',
        options: {
          submitForSettlement: true
        }
      }).then((err, result) => {
        if(err) {
            return res.status(406).send(err);
        }
        else {
            return res.status(200).json({"hash" : "Successful"});
        }
      }).catch((error) => {
        return res.status(406).send(error);
      });
}
});

Things I've already tried -

  1. Convert gateway.transaction.sale into promise by using bluebird. It still gives the same error - How to promisify a braintree method?

  2. Change amount and give another nonce by selecting testing cards - https://developers.braintreepayments.com/reference/general/testing/node#test-value-2223000048400011

  3. Remove options - Nothing changes


Solution

  • This maybe because you are on a free tier (Spark), thus outgoing socket connections are blocked. You will have to upgrade your project to Blaze plan to enable outgoing connections. The Blaze plan has generous quota limits so you probably won't be charged.