Firebase HTTP関数(Firebase、Stripe OAuth、React(JSX)フロントエンド)を使用して認証コードをStripeに返す
Aug 20 2020
Stripe OAuthプロセスを完了しようとしているReactフロントエンド、Firebaseバックエンドがあります。リダイレクトURIが戻ってきました(に戻るhttps://mywebsitename.com/oauth_return)そしてそのページで開いているreactコンポーネントは、その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)
私が苦労しているのは、firebaseHTTP関数がPOSTメソッドを介して認証コードを返すようにする方法を理解しようとしていることです。私のfirebase関数はすべて、functionsディレクトリの「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に送り返すのでしょうか。
この問題を明確にしていただければ幸いです。
回答
2 RameshD Aug 20 2020 at 16:30
これがあなたの目的のために書かれた作業コードです。これが解決策の提供に役立つことを願っています。
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,
});
}
}
);
})