php SDK

Following on from our .Net & Java SDKs, we’ve now released our php SDK. This provides a friendly way to access Paydock, making it even easier to use Paydock.

You can pull this down through composer on Packagist and the code is open source on github.

Providing the right interface

One of the questions when putting together a new SDK is how to support this in a way that is as language friendly as possible. What we’ve put together for a strongly typed language like .Net, just doesn’t make as much sense for a dynamically typed language like php.

The simplest approach to this would be to simply map through the request, serialised as json, eg something like this:

$request = [“amount” => 100, “currency” => “AUD”, “token” => “token_id”]
$response = Charges::Create($request);

This works fine with simpler requests, but it tends to make it hard with larger, more complex request. In those cases, it’s easy to create a situation where you send through either too many parameters or too few. Also, when you can send through nested sets of parameters, it’s easy to send through parameters encoded at the wrong depth, e.g.:

// missing items object
$request = [“amount” => 100, “currency” => “AUD”, “token” => “token_id”, “transfer” => [“amount” => 25, “destination” => “account_id”]]
// correct structure
$request = [“amount” => 100, “currency” => “AUD”, “token” => “token_id”, “transfer” => [ “items” => [“amount” => 25, “destination” => “account_id”]]]

For this reason we settled on a fluent style interface.

Fluent interface

Using a fluent interface, you build up a request through a number of calls. Some good examples of fluent interfaces are things like LINQ in .Net:

var topOrders = orders.Where(o => o.Currency = “AUD”)
                .OrderBy(o => o.Amount)
                .Select(o => o.Email);

These are often a very natural way of searching for data, but also work well to build up complex requests.

In order to make this as clear as possible, we’ve followed certain conventions. An example of this in the SDK is creating a charge with bank account details:

$response = $svc->create("John", "Smith", "", "+61414111111")
	->withBankAccount(self::bsbGateway, "test", "012003", "456456")
	->includeAddress("1 something st", "", "NSW", "Australia", "Sydney", "2000")

The first call is part of the call specifies the action (create). If this needs additional data, this is send through as with<Something>. Optional data is added through include<something>. And finally call() makes the actual call.

This made the API more complex to build, but provided a better experience of using the API.

As always, we’re open to improving this, so please raise any issues or concerns on on github and we’ll jump on it!