10

I am trying to process a credit card payment from my Android App using Firebase and Stripe. I have retrieved a Stripe token on my client, and I am using a database trigger in my Firebase Cloud Function to catch when a new order is posted. Here is my function code.

const stripe = require('stripe')('sk_test_XXXXXXXXXXXXXXXXXXXXXXXX');

return admin.database()
    .ref()
    .child('orders')
    .child(userId)
    .child(orderId)
    .child('token')
    .once('value')
    .then(snapshot => {
        return snapshot.val();
    })
    .then(token => {

        const amount = order.amount;
        console.log('Amount:', amount);
        console.log('token:', token.id);

        const idempotency_key = orderId;
        const source = token.id;
        const currency = 'usd';
        const charge = {amount, currency, source};

        return stripe.charges.create(charge, { idempotency_key });
    })
    .then(charge => {
        console.log('Success:', charge);
        // If the result is successful, write it back to the database
        return event.data.adminRef.set(charge);
    }, error => {
        console.log('Error:', error);

        return;
    }
);

enter code here

This is generating the following error:

Error: An error occurred with our connection to Stripe at Error._Error (/user_code/node_modules/stripe/lib/Error.js:12:17) at Error.Constructor (/user_code/node_modules/stripe/lib/utils.js:120:13) at Error.Constructor (/user_code/node_modules/stripe/lib/utils.js:120:13) at ClientRequest. (/user_code/node_modules/stripe/lib/StripeResource.js:206:9) at emitOne (events.js:96:13) at ClientRequest.emit (events.js:188:7) at TLSSocket.socketErrorListener (_http_client.js:309:9) at emitOne (events.js:96:13) at TLSSocket.emit (events.js:188:7) at connectErrorNT (net.js:1021:8) at _combinedTickCallback (internal/process/next_tick.js:80:11) at process._tickDomainCallback (internal/process/next_tick.js:128:9)

I can't find any documentation on this error. And I have tried everything I can think of. All the variables contain valid data.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Patrick Keogh
  • 227
  • 4
  • 11

1 Answers1

23

The most likely cause for something like this has nothing to do with your code - it has to do with the Firebase plan you're subscribed to.

The "Spark plan", which is the completely free tier (and the one most people start off with), does not allow outbound networking connections except to Google's HTTPS services.

Even for development and testing, I suggest going with the "Blaze plan". It allows for network connections. Although it does have utilization prices listed (both for processing and network connections), the Info button indicates that there is a free usage level each month before these prices kick in, and that level is generally enough to do development and testing.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • By any chance do you know how to capture a failed charge request? Everything works with the above code as long until I use a good card. If I use a declined card, the flow continues into the bottom error section but the error variable is empty. I would of thought it would just return a failed charge object – Patrick Keogh Sep 27 '17 at 17:30
  • I know nothing about Stripe itself, sorry. {: Sounds like you might be best served by posting that as a separate, targeted, question. – Prisoner Sep 27 '17 at 17:39
  • 1
    I got this error sometimes even I use the "Blaze plan". – JIE WANG Feb 23 '19 at 19:01
  • @JIEWANG That happens to me too. Did you find out what is happening? – Leo Nov 09 '19 at 23:37
  • 1
    @Leo I think this is the bug of firebase, it works for now. – JIE WANG Nov 10 '19 at 18:22
  • @JIEWANG Thanks. I finally found out what was going on in my case. The project is on the "Blaze plan", but billing was not working as I thought it was. (The error messages were quite confusing to me so I was a bit mislead.) – Leo Nov 10 '19 at 22:55