使用 Firebase HTTP 函数将授权代码返回到 Stripe(Firebase、Stripe OAuth、React (JSX) 前端)

Returning Authorization Code to Stripe using Firebase HTTP Function (Firebase, Stripe OAuth, React (JSX) frontend)

我有一个 React 前端,Firebase 后端试图完成 Stripe OAuth 流程。重定向 URI 已返回(returning 到 https://mywebsitename.com/oauth_return),我在该页面上打开的反应组件解析该 URL 并访问身份验证代码和状态。 (请看下文)

在“oauth_return.js”文件中

import React from 'react';
import queryString from 'query-string';

const oauth_redirect  = () => {

    //Parsing over URL
    const value=queryString.parse(window.location.search);
    const code=value.code;
    console.log('code:', code)
    const state=value.state;
    console.log('state:', state)
}
export default (oauth_redirect)

我遇到的困难是试图弄清楚如何通过 POST 方法使 firebase HTTP 函数 return 成为身份验证代码。我所有的 firebase 函数都存在于函数目录的“index.js”文件中。我看过的所有教程都展示了在 Typescript 中构建此函数的各种方法,但我的代码需要用 Javascript.

编写

在“functions/index.js”文件中

(...)

  exports.stripeCreateOathResponseToken = functions.https.onRequest((req, res) => {

  (...) Not sure what to write in this function to return the authorization code. All tutorials I've found are written in Typescript.

});

不幸的是,我不明白如何首先触发调用此 HTTP 函数(即我是否需要在“oauth_return.js”文件中显式调用它?如何传递授权码?最重要的是,它如何将授权码发送回 Stripe?

如能澄清此问题,我们将不胜感激。

这是为您的目的而编写的工作代码。希望这对您提供解决方案有所帮助。

exports.stripeCreateOathResponseToken = functions.https.onRequest((req, res) => {

    res.set('Access-Control-Allow-Origin', '*');

    if (req.method === 'OPTIONS') {
        console.log('Executing OPTIONS request code block');
        res.set('Access-Control-Allow-Methods', 'POST');
        res.set('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept');
        res.set('Access-Control-Max-Age', '3600');
        res.status(204).send('');
    } else if (req.method === 'POST') {
        console.log('Executing POST request code block');
        return oauthResponseCheck(req, res);
    }
    else {
        // Log, but ignore requests which are not OPTIONS or POST.
        console.log(`We received an invalid request method of type: ${req.method}`);
        return;
    }
});


function oauthResponseCheck(req, res) {
     // code: An authorization code you can use in the next call to get an access token for your user.
    // This can only be used once and expires in 5 minutes.

    // state: The value of the state parameter you provided on the initial POST request.
    const { code, state } = req.body;

    // Assert the state matches the state you provided in the OAuth link (optional).
    if (!stateMatches(state)) {
        console.log('Incorrect state parameter for state::', state)
        return res.status(403).json({ error: 'Incorrect state parameter: ' + state });
    }
   
    // Send the authorization code to Stripe's API.
    stripe.oauth.token({
        grant_type: 'authorization_code',
        code
    }).then(
        (response) => {
            var stripe_connected_account_id = response.stripe_user_id;
         
            console.log('Stripe Connected Account Saved successfully: ' + JSON.stringify(response));
            return res.status(200).json({
                status: true,
                message: 'Stripe Connected Account Saved successfully',
                data: response
            });

        },
        (err) => {
            if (err.type === 'StripeInvalidGrantError') {
                console.log('Invalid authorization code: ' + code);
                return res.status(400).json({
                    status: false,
                    message: 'Invalid authorization code.::' + code,
                }
                );
            } else {
                console.log('An unknown error occurred: ' + err);
                return res.status(500).json({
                    status: false,
                    message: 'An unknown error occurred.::' + err,
                });
            }
        }
    );
})