Compare commits
3 Commits
f9930b143c
...
b4a75f4968
| Author | SHA1 | Date | |
|---|---|---|---|
| b4a75f4968 | |||
| e553bd673a | |||
| f221f4850e |
@ -9,7 +9,7 @@ import * as path from 'path';
|
|||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as https from 'https';
|
import * as https from 'https';
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { getStorage } from 'firebase-admin/storage';
|
||||||
const formData = require('form-data');
|
const formData = require('form-data');
|
||||||
const Mailgun = require('mailgun.js');
|
const Mailgun = require('mailgun.js');
|
||||||
const { convert } = require('html-to-text');
|
const { convert } = require('html-to-text');
|
||||||
@ -79,8 +79,44 @@ export const sendEmailWithAttachment = onRequest({
|
|||||||
response.status(500).json({ success: false, error: error instanceof Error ? error.message : String(error) });
|
response.status(500).json({ success: false, error: error instanceof Error ? error.message : String(error) });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const accessFile = onRequest({
|
||||||
|
region: '#{SERVICES_RGN}#'
|
||||||
|
}, async (request: Request, response: express.Response) => {
|
||||||
|
try {
|
||||||
|
const filePath = request.query.path as string;
|
||||||
|
if (!filePath) {
|
||||||
|
response.status(400).send('File path is required');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const expirationMs = 60 * 60 * 1000;
|
||||||
|
|
||||||
|
const bucket = getStorage().bucket();
|
||||||
|
const file = bucket.file(filePath);
|
||||||
|
|
||||||
|
const [exists] = await file.exists();
|
||||||
|
if (!exists) {
|
||||||
|
response.status(404).send('File not found');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [signedUrl] = await file.getSignedUrl({
|
||||||
|
action: 'read',
|
||||||
|
expires: Date.now() + expirationMs,
|
||||||
|
responseDisposition: `attachment; filename="${path.basename(filePath)}"`,
|
||||||
|
});
|
||||||
|
|
||||||
|
response.redirect(signedUrl);
|
||||||
|
logger.info(`File access redirect for ${filePath}`);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('Error accessing file:', error);
|
||||||
|
response.status(500).send('Error accessing file');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export const sendEmailMessage = onRequest({
|
export const sendEmailMessage = onRequest({
|
||||||
region: process.env.SERVICES_RGN
|
region: '#{SERVICES_RGN}#'
|
||||||
}, (request: Request, response: express.Response) => {
|
}, (request: Request, response: express.Response) => {
|
||||||
const mailgun = new Mailgun(formData);
|
const mailgun = new Mailgun(formData);
|
||||||
const mailGunClient = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY });
|
const mailGunClient = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY });
|
||||||
@ -109,7 +145,7 @@ export const sendEmailMessage = onRequest({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const sendSMSMessage = onRequest({
|
export const sendSMSMessage = onRequest({
|
||||||
region: process.env.SERVICES_RGN
|
region: '#{SERVICES_RGN}#'
|
||||||
}, (request: Request, response: express.Response) => {
|
}, (request: Request, response: express.Response) => {
|
||||||
const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
|
const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
|
||||||
const { to, body } = request.body;
|
const { to, body } = request.body;
|
||||||
@ -138,7 +174,7 @@ interface Invitation {
|
|||||||
|
|
||||||
export const notifyInvitation = onDocumentCreated({
|
export const notifyInvitation = onDocumentCreated({
|
||||||
document: 'notifications/{notificationId}',
|
document: 'notifications/{notificationId}',
|
||||||
region: process.env.SERVICES_RGN
|
region: '#{SERVICES_RGN}#'
|
||||||
}, async (event: any) => {
|
}, async (event: any) => {
|
||||||
const invitation = event.data?.data() as Invitation;
|
const invitation = event.data?.data() as Invitation;
|
||||||
const invitationId = event.params.invitationId;
|
const invitationId = event.params.invitationId;
|
||||||
@ -207,7 +243,7 @@ export const notifyInvitation = onDocumentCreated({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const createCashfreeOrder = onRequest({
|
export const createCashfreeOrder = onRequest({
|
||||||
region: process.env.SERVICES_RGN
|
region: '#{SERVICES_RGN}#'
|
||||||
}, async (request: Request, response: express.Response) => {
|
}, async (request: Request, response: express.Response) => {
|
||||||
try {
|
try {
|
||||||
const authHeader = request.headers.authorization;
|
const authHeader = request.headers.authorization;
|
||||||
@ -220,12 +256,12 @@ export const createCashfreeOrder = onRequest({
|
|||||||
const decodedToken = await admin.auth().verifyIdToken(idToken);
|
const decodedToken = await admin.auth().verifyIdToken(idToken);
|
||||||
const uid = decodedToken.uid;
|
const uid = decodedToken.uid;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
amount,
|
amount,
|
||||||
customerName,
|
customerName,
|
||||||
customerEmail,
|
customerEmail,
|
||||||
customerPhone,
|
customerPhone,
|
||||||
productInfo
|
productInfo
|
||||||
} = request.body;
|
} = request.body;
|
||||||
|
|
||||||
if (!amount || !customerEmail || !customerPhone) {
|
if (!amount || !customerEmail || !customerPhone) {
|
||||||
@ -236,13 +272,13 @@ export const createCashfreeOrder = onRequest({
|
|||||||
const clientId = process.env.CASHFREE_CLIENT_ID;
|
const clientId = process.env.CASHFREE_CLIENT_ID;
|
||||||
const clientSecret = process.env.CASHFREE_CLIENT_SECRET;
|
const clientSecret = process.env.CASHFREE_CLIENT_SECRET;
|
||||||
const isTest = true;
|
const isTest = true;
|
||||||
|
|
||||||
const apiUrl = isTest
|
const apiUrl = isTest
|
||||||
? 'https://sandbox.cashfree.com/pg/orders'
|
? 'https://sandbox.cashfree.com/pg/orders'
|
||||||
: 'https://api.cashfree.com/pg/orders';
|
: 'https://api.cashfree.com/pg/orders';
|
||||||
|
|
||||||
const orderId = `order_${Date.now()}_${uid.substring(0, 6)}`;
|
const orderId = `order_${Date.now()}_${uid.substring(0, 6)}`;
|
||||||
|
|
||||||
const cashfreeResponse = await axios.post(
|
const cashfreeResponse = await axios.post(
|
||||||
apiUrl,
|
apiUrl,
|
||||||
{
|
{
|
||||||
@ -270,7 +306,7 @@ export const createCashfreeOrder = onRequest({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
await admin.firestore().collection('payment_orders').doc(orderId).set({
|
await admin.firestore().collection('payment_orders').doc(orderId).set({
|
||||||
userId: uid,
|
userId: uid,
|
||||||
amount: amount,
|
amount: amount,
|
||||||
@ -281,16 +317,16 @@ export const createCashfreeOrder = onRequest({
|
|||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
...cashfreeResponse.data
|
...cashfreeResponse.data
|
||||||
});
|
});
|
||||||
|
|
||||||
response.json({
|
response.json({
|
||||||
order_id: cashfreeResponse.data.order_id,
|
order_id: cashfreeResponse.data.order_id,
|
||||||
payment_session_id: cashfreeResponse.data.payment_session_id
|
payment_session_id: cashfreeResponse.data.payment_session_id
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(`Cashfree order created: ${orderId}`);
|
logger.info(`Cashfree order created: ${orderId}`);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
logger.error('Cashfree order creation error:', error);
|
logger.error('Cashfree order creation error:', error);
|
||||||
response.status(500).json({
|
response.status(500).json({
|
||||||
error: 'Failed to create payment order',
|
error: 'Failed to create payment order',
|
||||||
details: error.response?.data || error.message
|
details: error.response?.data || error.message
|
||||||
});
|
});
|
||||||
@ -298,24 +334,24 @@ export const createCashfreeOrder = onRequest({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const verifyCashfreePayment = onRequest({
|
export const verifyCashfreePayment = onRequest({
|
||||||
region: process.env.SERVICES_RGN
|
region: '#{SERVICES_RGN}#'
|
||||||
}, async (request: Request, response: express.Response) => {
|
}, async (request: Request, response: express.Response) => {
|
||||||
try {
|
try {
|
||||||
const orderId = request.body.order_id || request.query.order_id;
|
const orderId = request.body.order_id || request.query.order_id;
|
||||||
|
|
||||||
if (!orderId) {
|
if (!orderId) {
|
||||||
response.status(400).json({ error: 'Order ID is required' });
|
response.status(400).json({ error: 'Order ID is required' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const clientId = process.env.CASHFREE_CLIENT_ID;
|
const clientId = process.env.CASHFREE_CLIENT_ID;
|
||||||
const clientSecret = process.env.CASHFREE_CLIENT_SECRET;
|
const clientSecret = process.env.CASHFREE_CLIENT_SECRET;
|
||||||
const isTest = process.env.CASHFREE_ENVIRONMENT !== 'production';
|
const isTest = process.env.CASHFREE_ENVIRONMENT !== 'production';
|
||||||
|
|
||||||
const apiUrl = isTest
|
const apiUrl = isTest
|
||||||
? `https://sandbox.cashfree.com/pg/orders/${orderId}`
|
? `https://sandbox.cashfree.com/pg/orders/${orderId}`
|
||||||
: `https://api.cashfree.com/pg/orders/${orderId}`;
|
: `https://api.cashfree.com/pg/orders/${orderId}`;
|
||||||
|
|
||||||
const cashfreeResponse = await axios.get(
|
const cashfreeResponse = await axios.get(
|
||||||
apiUrl,
|
apiUrl,
|
||||||
{
|
{
|
||||||
@ -326,27 +362,27 @@ export const verifyCashfreePayment = onRequest({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
await admin.firestore().collection('payment_orders').doc(orderId).update({
|
await admin.firestore().collection('payment_orders').doc(orderId).update({
|
||||||
orderStatus: cashfreeResponse.data.order_status,
|
orderStatus: cashfreeResponse.data.order_status,
|
||||||
paymentDetails: cashfreeResponse.data,
|
paymentDetails: cashfreeResponse.data,
|
||||||
updatedAt: new Date()
|
updatedAt: new Date()
|
||||||
});
|
});
|
||||||
|
|
||||||
if (request.headers['x-webhook-source'] === 'cashfree') {
|
if (request.headers['x-webhook-source'] === 'cashfree') {
|
||||||
response.status(200).send('OK');
|
response.status(200).send('OK');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
response.json({
|
response.json({
|
||||||
status: cashfreeResponse.data.order_status,
|
status: cashfreeResponse.data.order_status,
|
||||||
paymentDetails: cashfreeResponse.data
|
paymentDetails: cashfreeResponse.data
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(`Cashfree payment verified: ${orderId}`);
|
logger.info(`Cashfree payment verified: ${orderId}`);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
logger.error('Cashfree payment verification error:', error);
|
logger.error('Cashfree payment verification error:', error);
|
||||||
response.status(500).json({
|
response.status(500).json({
|
||||||
error: 'Failed to verify payment status',
|
error: 'Failed to verify payment status',
|
||||||
details: error.response?.data || error.message
|
details: error.response?.data || error.message
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user