Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error connecting to Stripe from Firebase Cloud Function

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.

like image 530
Patrick Keogh Avatar asked Sep 27 '17 12:09

Patrick Keogh


1 Answers

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.

like image 51
Prisoner Avatar answered Oct 15 '22 09:10

Prisoner