Skip to content

Commit

Permalink
Merge pull request #116 from gocardless/template-changes
Browse files Browse the repository at this point in the history
Add support for webhook endpoints
  • Loading branch information
danwakefield authored Mar 2, 2021
2 parents d945731 + 0922efd commit 0238cee
Show file tree
Hide file tree
Showing 17 changed files with 460 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gocardless/gocardless-pro",
"description": "GoCardless Pro PHP Client Library",
"version": "4.9.0",
"version": "4.10.0",
"keywords": [
"gocardless",
"direct debit",
Expand Down
18 changes: 16 additions & 2 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct($config)
'Content-Type' => 'application/json',
'Authorization' => "Bearer " . $access_token,
'GoCardless-Client-Library' => 'gocardless-pro-php',
'GoCardless-Client-Version' => '4.9.0',
'GoCardless-Client-Version' => '4.10.0',
'User-Agent' => $this->getUserAgent()
),
'http_errors' => false,
Expand Down Expand Up @@ -367,6 +367,20 @@ public function taxRates()
return $this->tax_rates;
}

/**
* Service for interacting with webhooks
*
* @return Services\WebhooksService
*/
public function webhooks()
{
if (!isset($this->webhooks)) {
$this->webhooks = new Services\WebhooksService($this->api_client);
}

return $this->webhooks;
}

private function getUrlForEnvironment($environment)
{
$environment_urls = array(
Expand Down Expand Up @@ -416,7 +430,7 @@ private function getUserAgent()
{
$curlinfo = curl_version();
$uagent = array();
$uagent[] = 'gocardless-pro-php/4.9.0';
$uagent[] = 'gocardless-pro-php/4.10.0';
$uagent[] = 'schema-version/2015-07-06';
if (defined('\GuzzleHttp\Client::MAJOR_VERSION')) {
$uagent[] = 'GuzzleHttp/' . \GuzzleHttp\Client::MAJOR_VERSION;
Expand Down
98 changes: 98 additions & 0 deletions lib/Resources/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* WARNING: Do not edit by hand, this file was generated by Crank:
*
* https://github.com/gocardless/crank
*/

namespace GoCardlessPro\Resources;

/**
* A thin wrapper around a webhook, providing access to its
* attributes
*
* @property-read $created_at
* @property-read $id
* @property-read $is_test
* @property-read $request_body
* @property-read $request_headers
* @property-read $response_body
* @property-read $response_body_truncated
* @property-read $response_code
* @property-read $response_headers
* @property-read $response_headers_content_truncated
* @property-read $response_headers_count_truncated
* @property-read $successful
* @property-read $url
*/
class Webhook extends BaseResource
{
protected $model_name = "Webhook";

/**
* Fixed [timestamp](#api-usage-time-zones--dates), recording when this
* resource was created.
*/
protected $created_at;

/**
* Unique identifier, beginning with "WB".
*/
protected $id;

/**
* Boolean value showing whether this was a demo webhook for testing
*/
protected $is_test;

/**
* The body of the request sent to the webhook URL
*/
protected $request_body;

/**
* The request headers sent with the webhook
*/
protected $request_headers;

/**
* The body of the response from the webhook URL
*/
protected $response_body;

/**
* Boolean value indicating the webhook response body was truncated
*/
protected $response_body_truncated;

/**
* The response code from the webhook request
*/
protected $response_code;

/**
* The headers sent with the response from the webhook URL
*/
protected $response_headers;

/**
* Boolean indicating the content of response headers was truncated
*/
protected $response_headers_content_truncated;

/**
* Boolean indicating the number of response headers was truncated
*/
protected $response_headers_count_truncated;

/**
* Boolean indicating whether the request was successful or failed
*/
protected $successful;

/**
* URL the webhook was POST-ed to
*/
protected $url;

}
138 changes: 138 additions & 0 deletions lib/Services/WebhooksService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* WARNING: Do not edit by hand, this file was generated by Crank:
*
* https://github.com/gocardless/crank
*/

namespace GoCardlessPro\Services;

use \GoCardlessPro\Core\Paginator;
use \GoCardlessPro\Core\Util;
use \GoCardlessPro\Core\ListResponse;
use \GoCardlessPro\Resources\Webhook;
use \GoCardlessPro\Core\Exception\InvalidStateException;


/**
* Service that provides access to the Webhook
* endpoints of the API
*
* @method list()
* @method get()
* @method retry()
*/
class WebhooksService extends BaseService
{

protected $envelope_key = 'webhooks';
protected $resource_class = '\GoCardlessPro\Resources\Webhook';


/**
* List webhooks
*
* Example URL: /webhooks
*
* @param string[mixed] $params An associative array for any params
* @return ListResponse
**/
protected function _doList($params = array())
{
$path = "/webhooks";
if(isset($params['params'])) { $params['query'] = $params['params'];
unset($params['params']);
}


$response = $this->api_client->get($path, $params);


return $this->getResourceForResponse($response);
}

/**
* Get a single webhook
*
* Example URL: /webhooks/:identity
*
* @param string $identity Unique identifier, beginning with "WB".
* @param string[mixed] $params An associative array for any params
* @return Webhook
**/
public function get($identity, $params = array())
{
$path = Util::subUrl(
'/webhooks/:identity',
array(

'identity' => $identity
)
);
if(isset($params['params'])) { $params['query'] = $params['params'];
unset($params['params']);
}


$response = $this->api_client->get($path, $params);


return $this->getResourceForResponse($response);
}

/**
* Retry a webhook
*
* Example URL: /webhooks/:identity/actions/retry
*
* @param string $identity Unique identifier, beginning with "WB".
* @param string[mixed] $params An associative array for any params
* @return Webhook
**/
public function retry($identity, $params = array())
{
$path = Util::subUrl(
'/webhooks/:identity/actions/retry',
array(

'identity' => $identity
)
);
if(isset($params['params'])) {
$params['body'] = json_encode(array("data" => (object)$params['params']));

unset($params['params']);
}


try {
$response = $this->api_client->post($path, $params);
} catch(InvalidStateException $e) {
if ($e->isIdempotentCreationConflict()) {
if ($this->api_client->error_on_idempotency_conflict) {
throw $e;
}
return $this->get($e->getConflictingResourceId());
}

throw $e;
}


return $this->getResourceForResponse($response);
}

/**
* List webhooks
*
* Example URL: /webhooks
*
* @param string[mixed] $params
* @return Paginator
**/
public function all($params = array())
{
return new Paginator($this, $params);
}

}
Loading

0 comments on commit 0238cee

Please sign in to comment.