recurly / recurly-client
The PHP client library for the Recurly API
Installs: 5 108 519
Dependents: 7
Suggesters: 0
Security: 0
Stars: 174
Watchers: 76
Forks: 93
Open Issues: 20
Requires
- php: >= 7.2.0
- psr/log: ^1.1 || ^2.0 || ^3.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.0
- phpcompatibility/php-compatibility: ^9.3
- phpdocumentor/phpdocumentor: ~3.0.0
- phpstan/phpstan: ^0.12.11
- phpunit/phpunit: ^8
- squizlabs/php_codesniffer: ^3.5
- dev-v3-v2021-02-25
- 4.56.0
- 4.55.0
- 4.54.0
- 4.53.0
- 4.52.0
- 4.51.0
- 4.50.0
- 4.49.0
- 4.48.0
- 4.47.0
- 4.46.0
- 4.45.0
- 4.44.0
- 4.43.0
- 4.42.0
- 4.41.0
- 4.40.0
- 4.39.0
- 4.37.0
- 4.36.0
- 4.35.0
- 4.34.0
- 4.33.0
- 4.32.0
- 4.31.0
- 4.30.0
- 4.29.0
- 4.28.0
- 4.27.0
- 4.26.0
- 4.25.0
- 4.24.0
- 4.23.0
- 4.22.0
- 4.21.2
- 4.21.1
- 4.21.0
- 4.20.0
- 4.19.0
- 4.18.0
- 4.17.0
- 4.16.0
- 4.15.0
- 4.14.0
- 4.13.0
- 4.12.0
- 4.11.0
- 4.10.0
- 4.9.0
- 4.8.0
- 4.7.0
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.0
- 4.0.1
- 4.0.0
- 3.22.0
- 3.21.0
- 3.20.0
- 3.19.0
- 3.18.0
- 3.17.0
- 3.16.0
- 3.15.0
- 3.14.0
- 3.13.0
- 3.12.0
- 3.11.0
- 3.10.0
- 3.9.0
- 3.8.1
- 3.8.0
- 3.7.0
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 3.0.0-beta2
- v2.x-dev
- 2.14.14
- 2.14.13
- 2.14.12
- 2.14.11
- 2.14.10
- 2.14.9
- 2.14.8
- 2.14.7
- 2.14.6
- 2.14.5
- 2.14.4
- 2.14.3
- 2.14.2
- 2.14.1
- 2.14.0
- 2.13.1
- 2.13.0
- 2.12.38
- 2.12.37
- 2.12.36
- 2.12.35
- 2.12.34
- 2.12.33
- 2.12.32
- 2.12.31
- 2.12.30
- 2.12.29
- 2.12.28
- 2.12.27
- 2.12.26
- 2.12.25
- 2.12.24
- 2.12.23
- 2.12.22
- 2.12.21
- 2.12.20
- 2.12.19
- 2.12.18
- 2.12.17
- 2.12.16
- 2.12.15
- 2.12.14
- 2.12.13
- 2.12.12
- 2.12.11
- 2.12.10
- 2.12.9
- 2.12.8
- 2.12.7
- 2.12.6
- 2.12.5
- 2.12.4
- 2.12.3
- 2.12.2
- 2.12.1
- 2.12.0
- 2.11.2
- 2.11.1
- 2.11.0
- 2.10.7
- 2.10.6
- 2.10.5
- 2.10.4
- 2.10.3
- 2.10.2
- 2.10.1
- 2.10.0
- 2.9.1
- 2.9.0
- 2.8.3
- 2.8.2
- 2.8.1
- 2.8.0
- 2.8.0.rc1
- 2.7.3
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.1
- 2.6.0
- 2.5.4
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.7
- 2.4.6
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.7
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.5
- 2.1.4
- 2.1.3
- dev-v3-v2021-02-25-10306936898
- dev-v3-v2019-10-10
- dev-v2-call-to-undefined-method-__valuesString
- dev-run-tests
- dev-v3-v2021-02-25-8897433238
- dev-v3-v2019-10-10-8347312148
- dev-v3-v2019-10-10-7979642433
- dev-add_general_ledger_accounts
- dev-space
- dev-v2_external_subscriptions_new_fields
- dev-v3-v2021-02-25-1681239797
- dev-romainneutron-patch-1
- dev-v3-v2021-02-25-1675721624
- dev-v3-v2019-10-10-1667573799
- dev-v3-v2021-02-25-1666291806
- dev-v3-v2022-01-01-1666291383
- dev-add_card_type_to_v2
- dev-php-8-test-matrix
- dev-v3-v2022-01-01
- dev-release_2_10
- dev-release_2_5
- dev-release_2_6
- dev-release_2_4
- dev-release_2_7
- dev-release_2_2
- dev-release_2_3
- dev-release_2_8
- dev-release_2_9
- dev-release_2_1
This package is auto-updated.
Last update: 2024-12-17 00:17:10 UTC
README
This repository houses the official php client for Recurly's V3 API.
Note: If you were looking for the V2 client, see the v2 branch.
Documentation for the HTTP API and example code can be found on our Developer Portal.
Getting Started
Reference documentation can be found on Github Pages.
Installing
This package is published on Packagist under the name recurly/recurly-client and can be added as a dependency to your project's composer.json
file. We recommend using Composer to install and maintain this dependency.
{ "require": { "recurly/recurly-client": "^4" } }
Note: We try to follow semantic versioning and will only apply breaking changes to major versions.
Creating a Client
Client instances provide one place where every operation on the Recurly API can be found (rather than having them spread out amongst classes). A new client can be initialized with its constructor. It only requires an API key which can be obtained on the API Credentials Page.
// You should store your API key somewhere safe // and not in plain text if possible $api_key = 'myApiKey'; $client = new \Recurly\Client($api_key);
To access Recurly API in Europe, you will need to specify the EU Region in the options.
// You should store your API key somewhere safe // and not in plain text if possible $api_key = 'myApiKey'; $client = new \Recurly\Client($api_key, ['region' => 'eu']);
Logging
The client constructor optionally accepts a logger provided by the programmer. The logger you pass should implement the PSR-3 Logger Interface. By default, the client creates an instance of the \Recurly\Logger
which is a basic implementation that prints log messages to php://stdout
with the \Psr\Log\LogLevel::WARNING
level.
// Create an instance of the Recurly\Logger $logger = new \Recurly\Logger('Recurly', \Psr\Log\LogLevel::INFO); $client = new \Recurly\Client($api_key, $logger);
SECURITY WARNING: The log level should never be set to DEBUG in production. This could potentially result in sensitive data in your logging system.
Operations
The \Recurly\Client
contains every operation you can perform on the site as a list of methods. Each method is documented explaining the types and descriptions for each input and return type. For example, to use the get_plan endpoint, call the Client#getPlan()
method:
$plan_code = "gold"; $plan = $client->getPlan("code-$plan_code");
Pagination
Pagination is accomplished using the \Recurly\Pager
object. A pager is created by the list*
operations of the client. The Pager implements PHP's Iterator interface, so it can be used in a foreach
loop.
Note Calling
list*
methods do not call the API right away. They return immediately with the Pager. The API is not called until you iterate over the pager or request items from it.
$accounts = $client->listAccounts(); foreach($accounts as $account) { echo 'Account code: ' . $account->getCode() . PHP_EOL; }
Sorting and Filtering
When calling the list*
methods and constructing Pagers, you can pass in optional query parameters to help you sort
or filter the resulting resources in the list. The query params are documented on the method and can be found in the docs
under the Query Parameters section of any pageable endpoint.
Example filtering an sorting accounts:
$options = array('params' = array( // the following params are common amongst pageable endpoints 'limit' => 200, // 200 resources per page (http call) 'order' => 'asc', // asc or desc order 'begin_time' => '2020-01-01T01:00:00Z', // don't include accounts before 2020-01-01 'end_time' => '2020-02-01T01:00:00Z', // don't include accounts after 2020-02-01 // the following params are specific to the list_account endpoint 'email' => 'admin@email.com', // only accounts with this email 'subscriber' => true, // only accounts with a subscription in the active, canceled, or future state 'past_due' => false // no accounts with an invoice in the past_due state )); $accounts = $client->listAccounts($options); foreach($accounts as $account) { echo 'Account code: ' . $account->getCode() . PHP_EOL; }
Counting Resources
The Pager class implements a getCount()
method which allows you to count the resources the pager would return if iterated.
It does so by calling the endpoint with HEAD
and parsing and returning the Recurly-Total-Records
header. This method respects any filtering parameters you apply to the pager, but the sorting parameters will have no effect.
$accounts = $client->listAccounts([ 'past_due' => true ]); // make the HTTP call to get the total count $count = $accounts->getCount(); echo "Number of accounts past due: $count"
Efficiently Fetch the First or Last Resource
The Pager class implements a getFirst()
method which allows you to fetch just the first or last resource from the server. On top of being a convenient abstraction, this is implemented efficiently by only asking the server for the 1 resource you want.
$accounts = $client->listAccounts([ 'order' => 'desc', 'past_due' => true ]); // fetch only the first account with past due invoice $account = $accounts->getFirst();
If you want to fetch the last account in this scenario, invert the order from desc
to asc
:
$accounts = $client->listAccounts([ 'order' => 'asc', 'past_due' => true ]); // fetch only the last account with past due invoice $account = $accounts->getFirst();
A Note on Headers
In accordance with section 4.2 of RFC 2616, HTTP header fields are case-insensitive.
Creating Resources
For creating or updating resources, pass a plain associative array to one of the create*
or update*
methods:
$plan_create = array( "name" => "Monthly Coffee Subscription", "code" => "coffee-monthly", "currencies" => [ array( "currency" => "USD", "unit_amount" => 20.0 ) ] ); $plan = $client->createPlan($plan_create); echo 'Created Plan:' . PHP_EOL; var_dump($plan);
Error Handling
try { $account = $client->deactivateAccount($account_id); } catch (\Recurly\Errors\Validation $e) { // If the request was not valid, you may want to tell your user // why. You can find the invalid params and reasons in err.params // TODO show how to get params var_dump($e); } catch (\Recurly\Errors\NotFound $e) { // You'll receive a NotFound error if one of the identifiers in your request // was incorrect. In this case, it's possible the $account_id is incorrect or // the associated account does not exist var_dump($e); } catch (\Recurly\RecurlyError $e) { // All errors inherit from this base class, so this should catch // any error that this library throws. If we don't know what to // do with the err, we should probably re-raise and let // our web framework and logger handle it var_dump($e); }
HTTP Metadata
Sometimes you might want to get some additional information about the underlying HTTP request and response. Instead of returning this information directly and forcing the programmer to unwrap it, we inject this metadata into the top level resource that was returned. You can access the response by calling getResponse()
on any Resource.
Warning: Do not log or render whole requests or responses as they may contain PII or sensitive data.
$account = $client->getAccount("code-douglas"); $response = $account->getResponse(); echo "Request ID:" . $response->getRequestId() . PHP_EOL; echo "Rate limit remaining:" . $response->getRateLimitRemaining() . PHP_EOL;
Information about the request is also included in the \Recurly\Response
class and can be accessed using the getRequest()
method on the Response.
$account = $client->getAccount("code-douglas"); $response = $account->getResponse(); $request = $response->getRequest(); echo "Request path:" . $request->getPath() . PHP_EOL; echo "Request body as JSON:" . $request->getBodyAsJson() . PHP_EOL; foreach($request->getHeaders() as $k => $v) { echo "Request header: $k => $v" . PHP_EOL; }
This also works on Empty
resources (for when there is no return body):
$response = $client->removeLineItem("a959576b2b10b012")->getResponse(); echo "Request ID:" . $response->getRequestId() . PHP_EOL;