Upated the function createCashfreeOrder
This commit is contained in:
parent
dca28b4417
commit
897a65abdd
@ -372,6 +372,7 @@ export const createCashfreeOrder = onRequest({
|
||||
}
|
||||
|
||||
const idToken = authHeader.split('Bearer ')[1];
|
||||
try {
|
||||
const decodedToken = await admin.auth().verifyIdToken(idToken);
|
||||
const uid = decodedToken.uid;
|
||||
|
||||
@ -380,7 +381,10 @@ export const createCashfreeOrder = onRequest({
|
||||
customerName,
|
||||
customerEmail,
|
||||
customerPhone,
|
||||
productInfo
|
||||
productInfo,
|
||||
userId,
|
||||
gymId,
|
||||
orderId
|
||||
} = request.body;
|
||||
|
||||
if (!amount || !customerEmail || !customerPhone) {
|
||||
@ -390,18 +394,25 @@ export const createCashfreeOrder = onRequest({
|
||||
|
||||
const clientId = process.env.CASHFREE_CLIENT_ID;
|
||||
const clientSecret = process.env.CASHFREE_CLIENT_SECRET;
|
||||
const isTest = true;
|
||||
|
||||
if (!clientId || !clientSecret) {
|
||||
logger.error('Cashfree credentials not configured');
|
||||
response.status(500).json({ error: 'Payment gateway configuration error' });
|
||||
return;
|
||||
}
|
||||
|
||||
const isTest = true;
|
||||
const hashKey = `hash_${Date.now()}_${uid.substring(0, 1)}_${orderId}`;
|
||||
const apiUrl = isTest
|
||||
? 'https://sandbox.cashfree.com/pg/orders'
|
||||
: 'https://api.cashfree.com/pg/orders';
|
||||
|
||||
const orderId = `order_${Date.now()}_${uid.substring(0, 6)}`;
|
||||
|
||||
try {
|
||||
const cashfreeResponse = await axios.post(
|
||||
apiUrl,
|
||||
{
|
||||
order_id: orderId,
|
||||
hash_key: hashKey,
|
||||
order_amount: amount,
|
||||
order_currency: 'INR',
|
||||
customer_details: {
|
||||
@ -411,7 +422,7 @@ export const createCashfreeOrder = onRequest({
|
||||
customer_phone: customerPhone
|
||||
},
|
||||
order_meta: {
|
||||
return_url: `https://fitlien.com/payment/status?order_id={order_id}`,
|
||||
return_url: `https://fitlien.com/payment-bridge?order_id=${orderId}&hash_key=${hashKey}&user_id=${userId}&gym_id=${gymId}`,
|
||||
// notify_url: `https://$filien.web.app/verifyCashfreePayment`
|
||||
},
|
||||
order_note: productInfo || 'Fitlien Membership'
|
||||
@ -426,6 +437,7 @@ export const createCashfreeOrder = onRequest({
|
||||
}
|
||||
);
|
||||
|
||||
try {
|
||||
await admin.firestore().collection('payment_orders').doc(orderId).set({
|
||||
userId: uid,
|
||||
amount: amount,
|
||||
@ -434,25 +446,51 @@ export const createCashfreeOrder = onRequest({
|
||||
orderStatus: 'CREATED',
|
||||
paymentGateway: 'Cashfree',
|
||||
createdAt: new Date(),
|
||||
hashKey: hashKey,
|
||||
clientId: userId,
|
||||
gymId: gymId,
|
||||
orderId: orderId,
|
||||
...cashfreeResponse.data
|
||||
});
|
||||
} catch (firestoreError) {
|
||||
logger.error('Error storing order in Firestore:', firestoreError);
|
||||
}
|
||||
|
||||
response.json({
|
||||
success: true,
|
||||
order_id: cashfreeResponse.data.order_id,
|
||||
payment_session_id: cashfreeResponse.data.payment_session_id
|
||||
});
|
||||
|
||||
logger.info(`Cashfree order created: ${orderId}`);
|
||||
} catch (axiosError: any) {
|
||||
logger.error('Cashfree API error:', axiosError);
|
||||
response.status(axiosError.response?.status || 500).json({
|
||||
success: false,
|
||||
error: 'Payment gateway error',
|
||||
details: axiosError.response?.data || axiosError.message,
|
||||
code: axiosError.code
|
||||
});
|
||||
}
|
||||
} catch (authError) {
|
||||
logger.error('Authentication error:', authError);
|
||||
response.status(401).json({
|
||||
success: false,
|
||||
error: 'Invalid authentication token'
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
logger.error('Cashfree order creation error:', error);
|
||||
response.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to create payment order',
|
||||
details: error.response?.data || error.message
|
||||
details: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
export const verifyCashfreePayment = onRequest({
|
||||
region: '#{SERVICES_RGN}#'
|
||||
}, async (request: Request, response: express.Response) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user