firebase-realtime-databasepaypalgoogle-cloud-functionspaypal-ipn

How to dispatch a Paypal IPN to a Google Cloud function?


I've read here that it's possible to send an IPN directly to a Google cloud function. I have my Google Cloud functions running on Firebase on an index.js file.

I've set up my Paypal buttons to send the IPN to a page on my webapp.

Here is an example of one of the functions I'm running off Google Cloud Functions/Firebase:

// UPDATE ROOMS INS/OUTS
exports.updateRoomIns = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {
    const beforeData = change.before.val(); 
    const afterData = change.after.val(); 
    const roomPushKey = afterData.inRoom; 
    const insbefore = beforeData.ins; 
    const insafter = afterData.ins; 
    if ((insbefore === null || insbefore === undefined) && (insafter === null || insafter === undefined) || insbefore === insafter) {
        return 0;
    } else {
        const updates = {};
        Object.keys(insafter).forEach(key => {
            updates['/rooms/' + roomPushKey + '/ins/' + key] = true;
        });
        return admin.database().ref().update(updates); // do the update} 
    }   
    return 0;
});

Now question:

1) I want to add another function to process IPN from Paypal as soon as I have a transaction. How would I go about this?

I'll mark the answer as correct if solves this first question.

2) how would that Google cloud function even look like?

I'll create another question if you can solve this one.

Note I am using Firebase (no other databases nor PHP).


Solution

  • IPN is simply a server that tries to reach a given endpoint.

    First, you have to make sure that your firebase plan supports 3rd party requests (it's unavailable in the free plan).

    After that, you need to make an http endpoint, like so:

    exports.ipn = functions.http.onRequest((req, res) => {
        // req and res are instances of req and res of Express.js
        // You can validate the request and update your database accordingly.
    });
    

    It will be available in https://www.YOUR-FIREBASE-DOMAIN.com/ipn