Cloud function for custom payment gateway integration

Database & APIs

Hi there. I need help integrating a payment gateway called Peach Payments into my application. The first step is a cloud function to prepare the checkout. However, I am struggling to deploy the function to Firebase. Below is the code I am using for the cloud function. I urgently need help fixing it.

What have you tried so far?

const functions = require('firebase-functions');

const admin = require('firebase-admin');

// To avoid deployment errors, do not call admin.initializeApp() in your code

const axios = require('axios'); // Library for HTTP requests

exports.prepareCheckout = functions.region(region)

.runWith({

memory: '128MB'

})

.https.onCall((data, context) => {

if (!context.auth.uid) {

return; // User not authenticated, don't proceed

}

const amount = data.amount; // Extract amount from FlutterFlow request

const currency = data.currency; // Extract currency from FlutterFlow request

const paymentType = 'PA'; // Set paymentType to PA for pre-authorization

const url = 'https://eu-test.oppwa.com/v1/checkouts';

// Retrieve entityId and apiKey securely from Firebase environment variables

const entityId = process.env.PEACH_PAYMENTS_ENTITY_ID;

const apiKey = Buffer.from(process.env.PEACH_PAYMENTS_API_KEY, 'base64').toString('utf-8');

const headers = {

Authorization: Bearer ${apiKey},

};

const body = {

entityId,

amount,

currency,

paymentType,

};

return axios.post(url, body, { headers })

.then((response) => {

const checkoutId = response.data.id;

console.log('Checkout prepared successfully:', checkoutId);

return { checkoutId }; // Return checkout ID as JSON object

})

.catch((error) => {

console.error('Error preparing checkout:', error);

throw new functions.https.HttpsError('internal', 'Failed to prepare checkout');

});

});

Did you check FlutterFlow's Documentation for this topic?
Yes