PHPackages                             gocardless/gocardless-pro-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. gocardless/gocardless-pro-php

Abandoned → [gocardless/gocardless-pro](/?search=gocardless%2Fgocardless-pro)Library[API Development](/categories/api)

gocardless/gocardless-pro-php
=============================

GoCardless PHP Client Library

7.2.0(10mo ago)10376.1k↓17.4%40[4 issues](https://github.com/gocardless/gocardless-pro-php/issues)[3 PRs](https://github.com/gocardless/gocardless-pro-php/pulls)MITPHPPHP &gt;=8.1CI passing

Since Jun 9Pushed 1mo ago88 watchersCompare

[ Source](https://github.com/gocardless/gocardless-pro-php)[ Packagist](https://packagist.org/packages/gocardless/gocardless-pro-php)[ Docs](https://gocardless.com/)[ RSS](/packages/gocardless-gocardless-pro-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (95)Used By (0)

GoCardless Pro PHP client library
=================================

[](#gocardless-pro-php-client-library)

A PHP client for interacting with the GoCardless Pro API.

[![PHP version](https://camo.githubusercontent.com/fbd80dc80bfe0c6fa455a34b9a70b56ded508deb9ec8f9684aafb71756f89592/68747470733a2f2f62616467652e667572792e696f2f70682f676f636172646c657373253246676f636172646c6573732d70726f2e737667)](https://badge.fury.io/ph/gocardless%2Fgocardless-pro)[![CircleCI](https://camo.githubusercontent.com/91ac3410bc596595065ef7f4e9908a687b5a4295d9cb29f878f22a0e77bddf8f/68747470733a2f2f636972636c6563692e636f6d2f67682f676f636172646c6573732f676f636172646c6573732d70726f2d7068702e7376673f7374796c653d736869656c64)](https://circleci.com/gh/gocardless/gocardless-pro-php)

- ["Getting started" guide](https://developer.gocardless.com/getting-started/api/introduction/?lang=php)with copy and paste PHP code samples
- [API Reference](https://developer.gocardless.com/api-reference)
- [Composer Package](https://packagist.org/packages/gocardless/gocardless-pro)
- [Changelog](https://github.com/gocardless/gocardless-pro-php/releases)

### Installation

[](#installation)

The recommended way to install `gocardless-pro` is using [Composer](https://getcomposer.org/).

```
# 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';
```

### Initialising A Client

[](#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](https://github.com/vlucas/phpdotenv), though keep it out of version control!

```
$access_token = getenv('GC_ACCESS_TOKEN');
$client = new \GoCardlessPro\Client([
  '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](https://developer.gocardless.com/api-reference).

### GET requests

[](#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

[](#postput-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](https://developer.gocardless.com/api-reference/#making-requests-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

[](#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](https://developer.gocardless.com/pro/#overview-errors).

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([
    'params' => ['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 accessed with the following methods:

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

### Handling webhooks

[](#handling-webhooks)

GoCardless supports webhooks, allowing you to receive real-time notifications when things happen in your account, so you can take automatic actions in response, for example:

- When a customer cancels their mandate with the bank, suspend their club membership
- When a payment fails due to lack of funds, mark their invoice as unpaid
- When a customer's subscription generates a new payment, log it in their "past payments" list

The client allows you to validate that a webhook you receive is genuinely from GoCardless, and to parse it into `GoCardlessPro\Resources\Event` objects which are easy to work with:

```
