Category Archives: Closures, Callbacks & Anonymous Functions

How to Handle a Coinbase Callback in PHP to Recieve Instant Bit Coin Payment Notifications

Coinbase Merchant Settings
In the above screen shot of a Coinbase.com account Merchant Settings page you will notice the field that allows you to enter a Callback URL so that you may receive instant payment notifications when you receive an order or bitcoin payment via the Coinbase payment button on your website. These can be useful to mark an order or user as “paid” once you’ve received a payment. Coinbase Callbacks send an automated message to the URL that you specify in the “Callback URL” field in the Merchant Settings in your Coinbase.com account. Callbacks are always POST requests in JSON format to a callback URL that you set in the above example it is http://www.yoursite.com/coin.php.

Below is an example of the JSON Order Callback that is sent to coin.php.
Sample Order JSON

Below is the code for coin.php that processes the Coinbase Callback. It decodes the Callback input and inserts the parameters into a MySQL database that you specify. It also sends you an email notifying you of the transaction. Note that coin.php returns an integer code of 200, this is what the Coinbase expects to receive in order to acknowledge that the callback was successful.

Processing a Coinbase Callback

PHP Callbacks

A callback is a function that is passed to another segment of code, service or process in order for that segment of code, service or process to call the function at some later point in time when a certain condition has been met. A typical use for a callback is handling information that is requested from a web service like the Google Custom Search API.

A callback function is passed by its name as a string using the call_user_func() helper function. Object methods and static class methods can also be called back using call_user_func().

I will go ahead and give some examples of each of these three ways to call a callback by creating a simple function and a class that has both a static method and a normal method (that must be called by an instantiated object).

Callbacks1
Callbacks1
Callbacks1

These first three examples were to demonstrate the three main ways that call_user_func() can be used to pass callbacks. I will continue on with another example to demonstrate a more practical use for callbacks, using them to process data from the Google API.

Callbacks1
Callbacks1

You can imagine all the other practical uses for callbacks from writing a payment confirmation to your database after a customer makes a successful purchase from your web site’s payment gateway, or writing search results to your database as is implied in the example above.

Lamda Functions

Lambda functions or “anonymous functions,” as they are often referred to, are simply throwaway functions that can be defined at any time and are typically bound to a variable. The functions themselves only exist in the scope of the variable of which they are defined, so when that variable goes out of scope, so does the function.

Example:

$double = function($num) { return $num * 2; }

Lambda functions are useful for a number of instances, most notably for many PHP functions that accept a callback function. One such function is array_map(), which allows us to walk through an array and apply a callback function to each element of the array.

PHP Closures

Closures are anonymous functions and are like lambda functions, but smarter in the sense that they have the ability to interact with variables from the outside environment of where the closure is defined. They are functions created without a name, and they are generally passes and callback handlers to other functions.

Added in PHP 5.3, these functions are typically created on the fly, and as such didn’t have access to objects outside of what were being passes arguments to the function. PHP 5.4 adds the ability to access the this scope and therefore the scope of the parent PHP document class.

I’ll demonstrate with a simple example. I’ll create a file called closers.php and create a new class in it called Event. This class is going to have a private property called $name, I’ll just set it my name.

Next I will create a function, and this function is going to return an instance of a function.

<?php

class Event
{
private $name = "Michelle";

public function getFunction ()
{
return function () { return $this->name; };
}

}

$event = new Event();
$func = $event->getFunction();

echo "Hello, ", $func();

?>

Now, with PHP 5.4, this function is going to actually be able to access the $this scope. You may wonder why would I would create a function that returns a function? I do this because the benefit of passing functions around like variables allows us to essentially
encapsulate logic that can be called at a later time in code execution.

A good reason to do this is when you want something to happen after another event occurs.

For example, if I call a web service and I want to call another function to process the results when they comeback, this is a good way to do it. You can create your own special events.

They take functions and arguments and use them to call them at appropriate times at runtime. So what I can do is in my function I simply return, and then now that I have access to the $this scope I can just output what’s in the name property.

So that’s my event class. Now to use it I can create a new instance.

$event = new Event();

I can then call on that function to set a variable equal to that function object, which I can call at a later time.

$func = $event->getFunction();

So because func is now that function, I can now output the result by calling it. So what I should see when I run this is Hello Michelle, and it does this by calling the function that I returned in my class.

So closures, or anonymous functions, were a new addition in PHP 5.3. However, there is internal disagreement among PHP developers as to what the PHP $this scope should refer to exactly. Because of that, it didn’t allow access to it.

Now in 5.4, the PHP $this scope has been allowed, and you can use it to reference instance functions and properties as in the example above.