Search code examples
reactjsfirebaseip-addressstripe-connect

Get the user IP for Stripe registration, using Reactjs and Firebase


I am building a create-react-app using Firebase and react-router. I would like to integrate Stripe as payment service provider.

As part of the Stripe verification process, I need to provide the IP address of the user agreeing to the Terms and Conditions, as per the Stipe docs:

stripe.accounts.update(
  {CONNECTED_STRIPE_ACCOUNT_ID},
  {
    tos_acceptance: {
      date: Math.floor(Date.now() / 1000),
      ip: request.connection.remoteAddress // Assumes you're not using a proxy
    }
  }
});

Because I am using a combination of Firebase and react-router, I does not seem like I can use an Express like (req, res) to get the IP in the backend: I dont think I could link the IP address obtained to the user uid. Also when trying to use the ipify of this world on the client with react, I always end up with the same IP in production (no matter the device or location):

import http from 'http';
componentDidMount () {
    http.get({'host': 'api.ipify.org', 'port': 80, 'path': '/'}, function(resp) {
      resp.on('data', ip => {
        alert("My public IP address is: " + ip);
      });
    });
  }

Any idea how I could retrieve the user's IP address either through Firebase Cloud Functions or directly from the client?

Thanks


Solution

  • You can retrieve client IP address using Firebase Cloud Functions. HTTP Triggered Cloud Functions works similar to Node/Express app and they trigger with response and request parameters.

    Example

    exports.someFunctionName = functions.https.onRequest((req, res) => {
      // ...
    });
    

    With that in mind you can implement your Stripe logic in this function.

    exports.someFunctionName = functions.https.onRequest((request, response) = > {
        // ...
        stripe.accounts.update({
            CONNECTED_STRIPE_ACCOUNT_ID
          }, {
            tos_acceptance: {
              date: Math.floor(Date.now() / 1000),
              ip: request.ip // Assumes you're not using a proxy
            }
          }
        });
    });
    

    For more detail on how to retrieve IP address better you can check this answer.