Kết nối Firebase với thế giới bên ngoài
Hướng dẫn AI viết Cloud Functions để tích hợp với payment gateways, messaging APIs và các dịch vụ AI khác.
Ví dụ 1: Tích hợp cổng thanh toán (MoMo/VNPay)
Prompt: "Viết Cloud Function callable từ frontend để:
- Nhận thông tin đơn hàng (courseId, userId, amount)
- Gọi VNPay API tạo payment URL
- Trả về URL để redirect user
- Tạo webhook handler để xác nhận thanh toán thành công"
Ví dụ 2: Gửi thông báo Zalo OA
Prompt: "Viết Cloud Function trigger khi có job mới trên Marketplace:
- Lấy thông tin job
- Format message template cho Zalo OA
- Gọi Zalo API gửi broadcast đến followers
- Log kết quả vào Firestore"
Ví dụ 3: Tích hợp Gemini AI
Prompt: "Viết Cloud Function HTTP endpoint /api/chat:
- Nhận message từ user
- Gọi Gemini API với context là CV/Portfolio của user (lấy từ Firestore)
- Trả về response AI
- Lưu conversation history"
Code pattern chung:
import * as functions from 'firebase-functions';
import axios from 'axios';
export const integrateExternalAPI = functions.https
.onCall(async (data, context) => {
// Verify auth
if (!context.auth) {
throw new functions.https.HttpsError('unauthenticated', 'Login required');
}
// Call external API
const response = await axios.post(
process.env.EXTERNAL_API_URL,
data,
{ headers: { 'Authorization': `Bearer ${process.env.API_KEY}` }}
);
return response.data;
});
Bảo mật:
- Lưu API keys trong Firebase Environment Config
firebase functions:config:set vnpay.secret="xxx"
