3

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

1 Answers1

2

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.

bennygenel
  • 20,219
  • 5
  • 51
  • 71
  • I have tried this with Cloud Functions, but how can I then link the request to a specific user (be it stripe ID or uid), since I cannot pass any parameter to the "someFunctionName" http trigger? – Freddy Jabali Oct 10 '17 at 09:43
  • You can pass any parameter to Http Functions with GET/POST parameters. For more info read [`fetch` documentation](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) – bennygenel Oct 10 '17 at 09:47
  • @bennygenel Can I do do `onCreate` inside the https request? – Tarvo Mäesepp Dec 23 '19 at 12:53