PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 22, 2022

[FIXED] How to dispatch a Paypal IPN to a Google Cloud function?

 November 22, 2022     firebase-realtime-database, google-cloud-functions, paypal, paypal-ipn     No comments   

Issue

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



Answered By - Eliya Cohen
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing