fitlien-services/functions/src/payments/phonepe/invoice/getInvoiceUrl.ts
Allen T J d3cc6b3710
All checks were successful
Deploy FitLien services to Dev / Deploy to Dev (push) Successful in 3m35s
phonepe (#43)
Co-authored-by: AllenTJ7 <163137620+AllenTJ7@users.noreply.github.com>
Reviewed-on: #43
2025-05-21 11:31:44 +00:00

63 lines
1.8 KiB
TypeScript

import { onRequest } from "firebase-functions/v2/https";
import { Request } from "firebase-functions/v2/https";
import { getCorsHandler } from "../../../shared/middleware";
import { getAdmin, getLogger } from "../../../shared/config";
import { InvoiceService } from "./invoiceService";
const admin = getAdmin();
const logger = getLogger();
const corsHandler = getCorsHandler();
const invoiceService = new InvoiceService();
export const getInvoiceUrl = onRequest({
region: '#{SERVICES_RGN}#'
}, async (request: Request, response) => {
return corsHandler(request, response, async () => {
try {
const authHeader = request.headers.authorization || '';
if (!authHeader || !authHeader.startsWith('Bearer ')) {
response.status(401).json({ error: 'Unauthorized' });
return;
}
const idToken = authHeader.split('Bearer ')[1];
try {
await admin.auth().verifyIdToken(idToken);
const { invoicePath } = request.query;
if (!invoicePath) {
response.status(400).json({
success: false,
error: 'Missing invoice path'
});
return;
}
const downloadUrl = await invoiceService.getInvoiceDownloadUrl(invoicePath as string);
response.json({
success: true,
downloadUrl
});
} catch (authError: any) {
logger.error('Authentication error:', authError);
response.status(401).json({
success: false,
error: 'Invalid authentication token',
details: authError.message
});
}
} catch (error: any) {
logger.error('Error getting invoice URL:', error);
response.status(500).json({
success: false,
error: 'Failed to get invoice URL',
details: error.message
});
}
});
});