Giter Club home page Giter Club logo

gocardless-pro-php's Introduction

GoCardless Pro PHP client library

A PHP client for interacting with the GoCardless Pro API.

PHP version CircleCI

Installation

The recommended way to install gocardless-pro is using Composer.

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, run the Composer command to install the latest stable version of gocardless-pro.

php composer.phar require gocardless/gocardless-pro

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

Manual installation

We strongly recommend using Composer - it'll make it easier to manage your dependencies and stay up to date. But if you don't want to, you can also install the library manually:

  • Make sure you have PHP's cURL, JSON and mbstring extensions enabled (Composer checks these dependencies automatically)
  • Download the latest zipped release of Guzzle, which we use for making HTTP requests, and require the autoloader.php file
  • Grab the PHP library's source, and require the lib/loader.php file

Initialising A Client

Create a GoCardlessPro\Client instance, providing your access token and the environment you want to use. We strongly advise storing your access token as an environment variable, rather than directly in your code. you can easily load the environment variables from a .env file by using something like phpdotenv, though keep it out of version control!

$access_token = getenv('GC_ACCESS_TOKEN');
$client = new \GoCardlessPro\Client(array(
  'access_token' => $access_token,
  'environment'  => \GoCardlessPro\Environment::SANDBOX
));

You can create an access_token from the "Developers" tab in your GoCardless dashboard.

The environment can either be \GoCardlessPro\Environment::SANDBOX or \GoCardlessPro\Environment::LIVE, depending on whether you want to use the sandbox or live API.

For full documentation, see our API docs.

GET requests

You can make a request to get a list of resources using the list method.

$client->customers()->list();

Note: This README will use customers throughout but each of the resources in the API is available in this library.

If you need to pass any options, the last (or only, in the absence of URL params) argument to list() is an array of URL parameters:

$customers = $client->customers()->list(['params' => ['limit' => 400]]);

A call to list() returns an instance of ListResponse. You can use its records attribute to iterate through the results.

echo count($customers->records);
foreach ($customers->records as $resource) {
  echo $resource->given_name;
}

In the case where a URL parameter is needed, the method signature will contain the required arguments:

$customer = $client->customers()->get($customer_id);
echo $customer->given_name;

As with list, the last argument can be an options array, with any URL parameters given:

$client->customers()->get($customer_id, ['params' => ['some_flag' => true]]);

Both individual resource and ListResponse instances have an api_response attribute, which lets you access the following properties of the request:

  • status
  • headers
  • body
$api_response = $client->customers()->get($customer_id)->api_response;
echo $api_response->status_code;

POST/PUT Requests

For POST and PUT requests, you need to provide a body for your request by passing it in as the first argument.

$client->customers()->create([
  'params' => ["given_name" => "Pete", "family_name" => "Hamilton"]
]);

As with GET requests, if any parameters are required, these come first:

$client->customers()->update($customer_id, [
  'params' => ["family_name" => "Smith"]
]);

The GoCardless API includes idempotency keys. The library will automatically inject these into your request when you create a resource, preventing it from getting duplicated if something goes wrong with the API (e.g. networking issues or a timeout).

You can also specify your own idempotency key - you could, for example, use IDs of records in your database, protecting yourself not only from network or API issues, but also mistakes on your side which could lead to double-creation:

$client->customers()->create([
  'params' => ["given_name" => "Pete", "family_name" => "Hamilton"]
  "headers" => ["Idempotency-Key" => "ABC123"]
]);

If the library hits an idempotency key conflict (that is, you try to create a resource with an idempotency key you've already used), it will automatically load and return the already-existing resource.

Handling Failures

When the API returns an error, the library will return a corresponding subclass of ApiException, one of:

  • InvalidApiUsageException
  • InvalidStateException
  • ValidationFailedException

These types of error are covered in the API documentation.

If the error is an HTTP transport layer error (e.g. timeouts or issues within GoCardless's infrastructure), requests will automatically be retried by the library up to 3 times, with a 500ms delay between attempts, before a ApiConnectionException is raised.

If the library can't parse the response from GoCardless, it will throw a MalformedResponseException.

try {
  $client->customer()->create(array(
    "params" => array("invalid_name" => "Pete")
  ));
} catch (\GoCardlessPro\Core\Exception\ApiException $e) {
  // Api request failed / record couldn't be created.
} catch (\GoCardlessPro\Core\Exception\MalformedResponseException $e) {
  // Unexpected non-JSON response
} catch (\GoCardlessPro\Core\Exception\ApiConnectionException $e) {
  // Network error
}

Properties of the exception can be accessesed with the following methods:

  • $e->getType();
  • $e->getCode();
  • $e->getErrors();
  • $e->getDocumentationUrl();
  • $e->getMessage();
  • $e->getRequestId();
  • $e->getApiResponse();

Supporting PHP < 5.5

This client library only supports PHP >= 5.5. Earlier releases of PHP are now considered end of life and may be exposed to security vunerabilities.

Contributing

This client is auto-generated from Crank, a toolchain that we hope to soon open source. Issues should for now be reported on this repository.

Please do not modify the source code yourself, your changes will be overriden!

gocardless-pro-php's People

Contributors

nickcampbell18 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.