การคืนรหัสการให้สิทธิ์เป็น Stripe โดยใช้ฟังก์ชัน Firebase HTTP (Firebase, Stripe OAuth, React (JSX) frontend)
ฉันมี React front-end ส่วนหลังของ Firebase ที่พยายามทำกระบวนการ Stripe OAuth ให้เสร็จสมบูรณ์ URI การเปลี่ยนเส้นทางกลับมาแล้ว (กลับไปที่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)
สิ่งที่ฉันประสบปัญหาคือพยายามหาวิธีทำให้ฟังก์ชัน firebase HTTP ส่งคืนรหัสการตรวจสอบสิทธิ์ผ่านวิธี POST ฟังก์ชัน 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,
});
}
}
);
})