gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:19:38 26 4
gpt4 key购买 nike

我正在使用 Firebase 和 React-router 构建一个 create-react-app。我想将 Stripe 整合为支付服务提供商。

作为 Stripe 验证过程的一部分,我需要根据 Stipe docs 提供同意条款和条件的用户的 IP 地址。 :

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
}
}
});

因为我使用的是Firebase和react-router的组合,所以我似乎无法使用像(req, res)这样的Express在后端获取IP:我不认为我可以链接获得的IP地址到用户 uid。此外,当尝试使用 ipify 时在客户端上使用 React 来了解这个世界的情况,我总是在生产中得到相同的 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);
});
});
}

知道如何通过 Firebase Cloud Functions 或直接从客户端检索用户的 IP 地址吗?

谢谢

最佳答案

您可以使用 Firebase Cloud Functions 检索客户端 IP 地址。 HTTP 触发的云函数的工作原理与 Node/Express 应用程序类似,它们通过响应和请求参数触发。

示例

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

考虑到这一点,您可以在此函数中实现您的 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
}
}
});
});

有关如何更好地检索IP地址的更多详细信息,您可以查看this answer .

关于reactjs - 使用 Reactjs 和 Firebase 获取 Stripe 注册的用户 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46663111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com