Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# GoCardless Pro PHP client library
# GoCardless PHP client library

A PHP client for interacting with the GoCardless Pro API.
A PHP client for interacting with the GoCardless API.

[![PHP version](https://badge.fury.io/ph/gocardless%2Fgocardless-pro.svg)](https://badge.fury.io/ph/gocardless%2Fgocardless-pro)
[![CircleCI](https://circleci.com/gh/gocardless/gocardless-pro-php.svg?style=shield)](https://circleci.com/gh/gocardless/gocardless-pro-php)
Expand Down Expand Up @@ -236,6 +236,18 @@ try {
}
```

#### Accessing the webhook ID

If you need to access the webhook ID for debugging purposes, you can use `Webhook::parseWithMeta` instead:

```php
$result = GoCardlessPro\Webhook::parseWithMeta($request_body, $signature_header, $webhook_endpoint_secret);
$events = $result->getEvents();
$webhookId = $result->getWebhookId(); // e.g. "WB123" - useful for debugging
```

Note: The webhook ID is intended for debugging and logging purposes only. It should not be used for deduplication - instead, use the event IDs to deduplicate, as each event has a unique ID that remains consistent if the same event is sent multiple times.

For more details on working with webhooks, see our ["Getting started" guide](https://developer.gocardless.com/getting-started/api/introduction/?lang=php).

## Supporting PHP >= 8.1
Expand Down
32 changes: 30 additions & 2 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Client
*/
public function __construct($config)
{
$this->validate_config($config);
$this->validateConfig($config);

$access_token = $config['access_token'];

Expand Down Expand Up @@ -125,6 +125,10 @@ public function __construct($config)

$this->services['outbound_payments'] = new Services\OutboundPaymentsService($this->api_client);

$this->services['outbound_payment_imports'] = new Services\OutboundPaymentImportsService($this->api_client);

$this->services['outbound_payment_import_entries'] = new Services\OutboundPaymentImportEntriesService($this->api_client);

$this->services['payer_authorisations'] = new Services\PayerAuthorisationsService($this->api_client);

$this->services['payer_themes'] = new Services\PayerThemesService($this->api_client);
Expand Down Expand Up @@ -495,6 +499,30 @@ public function outboundPayments()
return $this->services['outbound_payments'];
}

/**
* Service for interacting with outbound payment imports
* @return Services\OutboundPaymentImportsService
*/
public function outboundPaymentImports()
{
if (!isset($this->services['outbound_payment_imports'])) {
throw new \Exception('Key outbound_payment_imports does not exist in services array');
}
return $this->services['outbound_payment_imports'];
}

/**
* Service for interacting with outbound payment import entries
* @return Services\OutboundPaymentImportEntriesService
*/
public function outboundPaymentImportEntries()
{
if (!isset($this->services['outbound_payment_import_entries'])) {
throw new \Exception('Key outbound_payment_import_entries does not exist in services array');
}
return $this->services['outbound_payment_import_entries'];
}

/**
* Service for interacting with payer authorisations
* @return Services\PayerAuthorisationsService
Expand Down Expand Up @@ -706,7 +734,7 @@ private function getUrlForEnvironment($environment)
*
* @param array[string]mixed $config the client configuration options
*/
private function validate_config(&$config)
private function validateConfig(&$config)
{
$required_option_keys = array('access_token', 'environment');

Expand Down
4 changes: 2 additions & 2 deletions lib/Core/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ private function handleErrors(Response $response)
return null;
}

$status_code = $response->getStatusCode();
$json = json_decode($response->getBody());

if ($json === null) {
$msg = "Malformed response received from server";
throw new Exception\MalformedResponseException($msg, $response);
throw new Exception\MalformedResponseException($msg, $response, $status_code);
}

$status_code = $response->getStatusCode();
if ($status_code < 400) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Core/Exception/ApiConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class ApiConnectionException extends GoCardlessProException
{
};
}
2 changes: 1 addition & 1 deletion lib/Core/Exception/AuthenticationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class AuthenticationException extends InvalidApiUsageException
{
};
}
2 changes: 1 addition & 1 deletion lib/Core/Exception/GoCardlessInternalException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class GoCardlessInternalException extends ApiException
{
};
}
2 changes: 1 addition & 1 deletion lib/Core/Exception/GoCardlessProException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class GoCardlessProException extends \Exception
{
};
}
2 changes: 1 addition & 1 deletion lib/Core/Exception/InvalidApiUsageException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class InvalidApiUsageException extends ApiException
{
};
}
2 changes: 1 addition & 1 deletion lib/Core/Exception/InvalidSignatureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class InvalidSignatureException extends GoCardlessProException
{
};
}
2 changes: 1 addition & 1 deletion lib/Core/Exception/InvalidStateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ private function getIdempotentCreationConflictError()
}
}
}
};
}
36 changes: 33 additions & 3 deletions lib/Core/Exception/MalformedResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,47 @@

class MalformedResponseException extends GoCardlessProException
{
const BODY_PREVIEW_MAX_LENGTH = 500;

private $response;
private $statusCode;

public function __construct($message, $response)
public function __construct($message, $response, $statusCode = null)
{
$this->response = $response;
parent::__construct($message);
$this->statusCode = $statusCode;
parent::__construct(self::buildMessage($message, $response, $statusCode));
}


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

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

private static function buildMessage($message, $response, $statusCode)
{
$full = $message;
if ($statusCode !== null) {
$full .= ' (HTTP ' . $statusCode . ')';
}
$body = null;
if (is_string($response)) {
$body = $response;
} elseif (is_object($response) && method_exists($response, 'getBody')) {
$body = (string) $response->getBody();
}
if ($body !== null && $body !== '') {
if (strlen($body) > self::BODY_PREVIEW_MAX_LENGTH) {
$body = substr($body, 0, self::BODY_PREVIEW_MAX_LENGTH) . '...';
}
$full .= ': ' . $body;
}
return $full;
}
}
2 changes: 1 addition & 1 deletion lib/Core/Exception/PermissionsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class PermissionsException extends InvalidApiUsageException
{
};
}
2 changes: 1 addition & 1 deletion lib/Core/Exception/RateLimitException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class RateLimitException extends InvalidApiUsageException
{
};
}
2 changes: 1 addition & 1 deletion lib/Core/Exception/ValidationFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ protected function extractErrorMessage($error)
return $error->message;
}
}
};
}
16 changes: 8 additions & 8 deletions lib/Core/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Paginator implements \Iterator
/**
* Default max records to retrieve per page
*/
const HARD_RECORD_LIMIT = 500;
public const HARD_RECORD_LIMIT = 500;

/**
* @var \GoCardlessPro\Services\BaseService The resource service to fetch records with
Expand Down Expand Up @@ -63,7 +63,7 @@ public function rewind()
{
$this->current_position = 0;
$this->current_page_position = 0;
$this->current_response = $this->initial_response();
$this->current_response = $this->initialResponse();
}

/**
Expand All @@ -74,7 +74,7 @@ public function rewind()
#[\ReturnTypeWillChange]
public function current()
{
return $this->current_records()[$this->key()];
return $this->currentRecords()[$this->key()];
}

/**
Expand All @@ -98,7 +98,7 @@ public function next()
++$this->current_position;

if (!$this->valid()) {
$this->current_response = $this->next_response();
$this->current_response = $this->nextResponse();
$this->current_page_position = $this->current_position;
}
}
Expand All @@ -112,15 +112,15 @@ public function next()
public function valid()
{
return !is_null($this->current_response) &&
array_key_exists($this->key(), $this->current_records());
array_key_exists($this->key(), $this->currentRecords());
}

/**
* Fetch the first page of results
*
* @return ListResponse
*/
private function initial_response()
private function initialResponse()
{
$options = $this->options;
$options['params']['after'] = null;
Expand All @@ -132,7 +132,7 @@ private function initial_response()
*
* @return ListResponse
*/
private function next_response()
private function nextResponse()
{
$options = $this->options;
$options['params']['after'] = $this->current_response->after;
Expand All @@ -148,7 +148,7 @@ private function next_response()
*
* @return \GoCardlessPro\Resources\BaseResource[]
*/
private function current_records()
private function currentRecords()
{
return $this->current_response->records;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Resources/Balance.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Balance extends BaseResource
protected $balance_type;

/**
* [ISO 4217](http://en.wikipedia.org/wiki/ISO_4217#Active_codes) currency
* [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217#Active_codes) currency
* code. Currently "AUD", "CAD", "DKK", "EUR", "GBP", "NZD", "SEK" and "USD"
* are supported.
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/Resources/BillingRequestTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class BillingRequestTemplate extends BaseResource
protected $mandate_request_constraints;

/**
* [ISO 4217](http://en.wikipedia.org/wiki/ISO_4217#Active_codes) currency
* [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217#Active_codes) currency
* code.
*/
protected $mandate_request_currency;
Expand Down Expand Up @@ -134,7 +134,7 @@ class BillingRequestTemplate extends BaseResource
protected $payment_request_amount;

/**
* [ISO 4217](http://en.wikipedia.org/wiki/ISO_4217#Active_codes) currency
* [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217#Active_codes) currency
* code. `GBP` and `EUR` supported; `GBP` with your customers in the UK and
* for `EUR` with your customers in supported Eurozone countries only.
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/Resources/Creditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Creditor extends BaseResource

/**
* [ISO 3166-1 alpha-2
* code.](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements)
* code.](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements)
*/
protected $country_code;

Expand All @@ -101,7 +101,7 @@ class Creditor extends BaseResource
protected $custom_payment_pages_enabled;

/**
* [ISO 4217](http://en.wikipedia.org/wiki/ISO_4217#Active_codes) code for
* [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217#Active_codes) code for
* the currency in which amounts will be paid out (after foreign exchange).
* Currently "AUD", "CAD", "DKK", "EUR", "GBP", "NZD", "SEK" and "USD" are
* supported. Present only if payouts will be (or were) made via foreign
Expand Down
4 changes: 2 additions & 2 deletions lib/Resources/CreditorBankAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CreditorBankAccount extends BaseResource

/**
* [ISO 3166-1 alpha-2
* code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements).
* code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements).
* Defaults to the country code of the `iban` if supplied, otherwise is
* required.
*/
Expand All @@ -70,7 +70,7 @@ class CreditorBankAccount extends BaseResource
protected $created_at;

/**
* [ISO 4217](http://en.wikipedia.org/wiki/ISO_4217#Active_codes) currency
* [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217#Active_codes) currency
* code. Currently "AUD", "CAD", "DKK", "EUR", "GBP", "NZD", "SEK" and "USD"
* are supported.
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/Resources/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Customer extends BaseResource

/**
* [ISO 3166-1 alpha-2
* code.](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements)
* code.](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements)
*/
protected $country_code;

Expand Down Expand Up @@ -105,7 +105,7 @@ class Customer extends BaseResource
protected $id;

/**
* [ISO 639-1](http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) code.
* [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) code.
* Used as the language for notification emails sent by GoCardless if your
* organisation does not send its own (see [compliance
* requirements](#appendix-compliance-requirements)). Currently only "en",
Expand Down
Loading
Loading