将状态参数设置为 Stripe 查询以传递 FireStore uuid

Setting state parameter to a Stripe query to pass a FireStore uuid

我正在尝试将 FirebaseFirestore 用户 Uid 传递给 Stripe / firestore 云函数。

所以我会有一个如下所示的 https 查询:

https://connect.stripe.com/express/oauth/authorize?response_type=code&client_id={accountid}&scope=read_write 在网络视图中打开

这是我的函数

exports.connectStripeExpressAccount = functions.https.onRequest((req, res) =>{
  console.log('query state is  ----> ' + req.query.state);
  const authCode = req.query.code;
  return stripe.oauth.token({
    grant_type: 'authorization_code',
    code: authCode,
  }).then(async response => {
      var connected_account_id = response.stripe_user_id;
      const uid = req.query.state
      const writeResult = await admin.firestore().collection('Registration').doc(uid)
                           .set({'customer_id': connected_account_id});
      return res.send("Well done, account integration is completed. You can now close the window and go back to the app");
      });
});

如果用户 uid 存储在查询参数 state 中并且 URL 如下所示: https://connect.stripe.com/express/oauth/authorize?response_type=code&client_id=ca_JCV8JW9ZIjBaGkwkhbDDDQegceWGidqh&scope=read_write&state=useruidxxx

您的代码如下所示:

exports.connectStripeExpressAccount = functions.https.onRequest((req, res) =>{
  console.log('query state is  ----> ' + req.query.state);
  const authCode = req.query.code;
  return stripe.oauth.token({
    grant_type: 'authorization_code',
    code: authCode,
  }).then(async response => {
      var connected_account_id = response.stripe_user_id;
      const uid = req.query.state
      const writeResult = await admin.firestore().collection('Registration').doc(uid)
                           .set({'customer_id': connected_account_id});
      return res.send("Well done, account integration is completed. You can now close the window and go back to the app");
      });
});

对于与 Express Accounts 的新集成,您最好使用 Account Links functionality 而不是 OAuth。也就是说,如果您提供 state 值,它应该会继续执行,所以我会确保您在打开 WebView 时确实提供了它。