简体   繁体   English

使用Reactjs和Firebase获取Stripe注册的用户IP

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

I am building a create-react-app using Firebase and react-router. 我正在使用Firebase和react-router构建一个create-react-app。 I would like to integrate Stripe as payment service provider. 我想将Stripe整合为支付服务提供商。

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 : 作为条纹验证过程的一部分,我需要提供用户的IP地址,同意条款和条件,根据Stipe 文档

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. 因为我使用Firebase和react-router的组合,我似乎不能使用Express(req,res)来获取后端的IP:我不认为我可以将获得的IP地址链接到用户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): 此外,当尝试在客户端使用ipify这个世界做出反应时,我总是在生产中使用相同的IP(无论设备或位置):

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? 知道如何通过Firebase云功能或直接从客户端检索用户的IP地址吗?

Thanks 谢谢

You can retrieve client IP address using Firebase Cloud Functions. 您可以使用Firebase云功能检索客户端IP地址。 HTTP Triggered Cloud Functions works similar to Node/Express app and they trigger with response and request parameters. HTTP触发云功能与Node / Express应用程序类似,它们通过响应和请求参数触发。

Example

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

With that in mind you can implement your Stripe logic in this function. 考虑到这一点,您可以在此功能中实现Stripe逻辑。

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 . 有关如何更好地检索IP地址的更多详细信息,您可以查看此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM