Mengembalikan Kode Otorisasi ke Stripe menggunakan Fungsi HTTP Firebase (Firebase, Stripe OAuth, React (JSX) frontend)
Saya memiliki React front-end, Firebase back-end mencoba menyelesaikan proses Stripe OAuth. URI pengalihan telah kembali (kembali kehttps://mywebsitename.com/oauth_return) dan komponen react yang saya buka di halaman itu mengurai URL itu dan mengakses Kode Otentikasi dan status. (silahkan lihat di bawah ini)
di dalam file "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)
Apa yang saya mengalami kesulitan adalah mencoba mencari cara untuk membuat fungsi HTTP firebase mengembalikan kode otentikasi melalui metode POST. Semua fungsi firebase saya ada di dalam file "index.js" pada direktori fungsi. Semua tutorial yang telah saya lihat menunjukkan berbagai cara untuk membangun fungsi ini dalam Ketikan, tetapi kode saya perlu ditulis dalam Javascript.
di dalam file "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.
});
Sayangnya, saya tidak mengerti bagaimana fungsi HTTP ini dapat dipicu untuk dipanggil (misalnya, apakah saya perlu memanggilnya secara eksplisit di dalam file "oauth_return.js"? Bagaimana cara mengirimkan kode otorisasi ke dalamnya? Dan sebagian besar yang terpenting, bagaimana cara mengirim kembali kode otorisasi ke Stripe?
Kejelasan tentang masalah ini akan sangat dihargai.
Jawaban
Berikut adalah kode kerja yang ditulis untuk tujuan yang sama persis seperti milik Anda. Semoga ini bisa membantu Anda dalam memberikan solusi.
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,
});
}
}
);
})