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
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
language: php
dist: jammy
php:
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'

install:
- composer install
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ For full description of request and response payloads and properties, please see

## Requirements

* PHP 7.3+ | PHP 8.0+
* PHP 8.2+
* ext-curl
* ext-json

Expand Down Expand Up @@ -332,7 +332,7 @@ Methods:
* `refreshAssetReport(string $asset_report_token, int $days_requested, array $options = []): object`
* `filterAssetReport(string $asset_report_token, array $exclude_accounts): object`
* `getAssetReport(string $asset_report_token, bool $include_insights = false): object`
* `getAssetReportPdf(string $asset_report_token): ResponseInterface` **Note:** Because this endpoint returns PDF content in the repsponse body, this method returns an instance of a PSR-7 `ResponseInterface`. You may leverage the `Response` object to stream the PDF back to the requesting client and access response headers
* `getAssetReportPdf(string $asset_report_token): ResponseInterface` **Note:** Because this endpoint returns PDF content in the response body, this method returns an instance of a PSR HTTP Message 2.0 `ResponseInterface`. You may leverage the `Response` object to stream the PDF back to the requesting client and access response headers
* `removeAssetReport(string $asset_report_token): object`
* `createAssetReportAuditCopy(string $asset_report_token, string $auditor_id): object`
* `removeAssetReportAuditCopy(string $audit_copy_token): object`
Expand Down Expand Up @@ -426,11 +426,11 @@ $address = new TomorrowIdeas\Plaid\Entities\RecipientAddress("123 Elm St.", "Apt

Example:

The `TomorrowIdeas\Plaid\Entities\PaymnentSchedule` entity is used when creating a new payment that will be a recurring charge.
The `TomorrowIdeas\Plaid\Entities\PaymentSchedule` entity is used when creating a new payment that will be a recurring charge.
See `createPayment` method for more information.

```php
$payment_schedule = new TomorrowIdeas\Plaid\Entities\PaymnentSchedule(
$payment_schedule = new TomorrowIdeas\Plaid\Entities\PaymentSchedule(
PaymentSchedule::INTERVAL_MONTHLY,
15,
new DateTime("2020-10-01")
Expand All @@ -439,4 +439,4 @@ $payment_schedule = new TomorrowIdeas\Plaid\Entities\PaymnentSchedule(

## Errors

All unsuccessfull (non 2xx) responses will throw a `PlaidRequestException`. The full response object is available via the `getResponse()` method.
All unsuccessful (non 2xx) responses will throw a `PlaidRequestException`. The full response object is available via the `getResponse()` method.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
],
"type": "library",
"require": {
"php": ">=7.3|>=8.0",
"php": "^8.2",
"ext-json": "*",
"ext-curl": "*",
"nimbly/shuttle": "^0.4"
"nimbly/shuttle": "^2.0",
"nimbly/capsule": "^3.0",
"psr/http-message": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 5 additions & 3 deletions src/Entities/PaymentSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class PaymentSchedule
public function __construct(
string $interval,
int $interval_execution_day,
DateTime $start_date)
DateTime $start_date
)
{
if( !\in_array($interval, [self::INTERVAL_MONTHLY, self::INTERVAL_WEEKLY]) ){
if (!\in_array($interval, [self::INTERVAL_MONTHLY, self::INTERVAL_WEEKLY]))
{
throw new InvalidArgumentException("Interval must be WEEKLY or MONTHLY.");
}

Expand Down Expand Up @@ -80,4 +82,4 @@ public function getStartDate(): DateTime
{
return $this->start_date;
}
}
}
17 changes: 11 additions & 6 deletions src/Plaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Psr\Http\Client\ClientInterface;
use ReflectionClass;
use Shuttle\Shuttle;
use Nimbly\Shuttle\Shuttle;
use TomorrowIdeas\Plaid\Resources\AbstractResource;
use UnexpectedValueException;

Expand Down Expand Up @@ -84,9 +84,11 @@ class Plaid
public function __construct(
string $client_id,
string $client_secret,
string $environment = "production")
string $environment = "production"
)
{
if( !\array_key_exists($environment, $this->plaidEnvironments) ){
if (!\array_key_exists($environment, $this->plaidEnvironments))
{
throw new UnexpectedValueException("Invalid environment. Environment must be one of: production, development, or sandbox.");
}

Expand All @@ -104,13 +106,15 @@ public function __construct(
*/
public function __get(string $resource): AbstractResource
{
if( !isset($this->resource_cache[$resource]) ){
if (!isset($this->resource_cache[$resource]))
{

$resource = \str_replace([" "], "", \ucwords(\str_replace(["_"], " ", $resource)));

$resource_class = "\\TomorrowIdeas\\Plaid\\Resources\\" . $resource;

if( !\class_exists($resource_class) ){
if (!\class_exists($resource_class))
{
throw new UnexpectedValueException("Unknown Plaid resource: {$resource}");
}

Expand Down Expand Up @@ -150,7 +154,8 @@ public function setHttpClient(ClientInterface $httpClient): void
*/
public function getHttpClient(): ClientInterface
{
if( empty($this->httpClient) ){
if (empty($this->httpClient))
{
$this->httpClient = new Shuttle;
}

Expand Down
8 changes: 5 additions & 3 deletions src/PlaidRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public function __construct(ResponseInterface $responseInterface)

$this->code = $responseInterface->getStatusCode();

if( \is_object($response) ){
if (\is_object($response))
{
$this->response = $response;
$this->message = (string) ($this->response->display_message ?? $responseInterface->getReasonPhrase());
}

else {
else
{
$this->message = $responseInterface->getReasonPhrase();
}
}
Expand All @@ -43,4 +45,4 @@ public function getResponse(): ?object
{
return $this->response;
}
}
}
17 changes: 12 additions & 5 deletions src/Resources/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace TomorrowIdeas\Plaid\Resources;

use Capsule\Request;
use Nimbly\Capsule\Request;
use Nimbly\Capsule\ResponseStatus;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -50,7 +51,8 @@ public function __construct(
ClientInterface $httpClient,
string $client_id,
string $client_secret,
string $hostname)
string $hostname
)
{
$this->httpClient = $httpClient;
$this->client_id = $client_id;
Expand Down Expand Up @@ -88,7 +90,8 @@ protected function sendRequest(string $method, string $path, array $params = [])

$payload = \json_decode($response->getBody()->getContents());

if( \json_last_error() !== JSON_ERROR_NONE ){
if (\json_last_error() !== JSON_ERROR_NONE)
{
throw new UnexpectedValueException("Invalid JSON response returned by Plaid");
}

Expand All @@ -110,7 +113,11 @@ protected function sendRequestRawResponse(string $method, string $path, array $p
$this->buildRequest($method, $path, $params)
);

if( $response->getStatusCode() < 200 || $response->getStatusCode() >= 300 ){
if (
$response->getStatusCode() < ResponseStatus::OK->value
|| $response->getStatusCode() >= ResponseStatus::MULTIPLE_CHOICES->value
)
{
throw new PlaidRequestException($response);
}

Expand All @@ -137,4 +144,4 @@ protected function buildRequest(string $method, string $path, array $params = []
]
);
}
}
}
82 changes: 42 additions & 40 deletions tests/AbstractResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace TomorrowIdeas\Plaid\Tests;

use Capsule\Request;
use Capsule\Response;
use Capsule\ResponseStatus;
use Shuttle\Handler\MockHandler;
use Shuttle\Shuttle;
use Nimbly\Capsule\Request;
use Nimbly\Capsule\Response;
use Nimbly\Capsule\ResponseStatus;
use Nimbly\Shuttle\Handler\MockHandler;
use Nimbly\Shuttle\Shuttle;
use TomorrowIdeas\Plaid\Plaid;
use TomorrowIdeas\Plaid\PlaidRequestException;
use UnexpectedValueException;
Expand All @@ -26,7 +26,7 @@ public function test_build_request_with_no_params_sends_empty_object_in_body():

$reflectionClass = new \ReflectionClass($itemsResource);

$method = $reflectionClass->getMethod("buildRequest");
$method = $reflectionClass->getMethod("buildRequest");
$method->setAccessible(true);

$request = $method->invokeArgs($itemsResource, ["post", "/endpoint"]);
Expand All @@ -39,67 +39,70 @@ public function test_build_request_with_no_params_sends_empty_object_in_body():

public function test_request_exception_passes_through_plaid_display_message(): void
{
$httpClient = new Shuttle([
'handler' => new MockHandler([
function(Request $request) {
$httpClient = new Shuttle(
new MockHandler([
function (Request $request)
{

$requestParams = [
"display_message" => "DISPLAY MESSAGE",
];

return new Response(300, \json_encode($requestParams));

return new Response(ResponseStatus::MULTIPLE_CHOICES, \json_encode($requestParams));
}
])
]);
);

$plaid = new Plaid("client_id", "secret");
$plaid->setHttpClient($httpClient);

try {
try
{
$plaid->items->get("access_token");
}
catch( PlaidRequestException $plaidRequestException ){
catch (PlaidRequestException $plaidRequestException)
{

$this->assertEquals("DISPLAY MESSAGE", $plaidRequestException->getMessage());

}
}

public function test_request_exception_passes_through_http_status_code(): void
{
$httpClient = new Shuttle([
'handler' => new MockHandler([
function(Request $request) {
$httpClient = new Shuttle(
new MockHandler([
function (Request $request)
{

$requestParams = [
"display_message" => "DISPLAY MESSAGE",
];

return new Response(300, \json_encode($requestParams));

return new Response(ResponseStatus::MULTIPLE_CHOICES, \json_encode($requestParams));
}
])
]);
);

$plaid = new Plaid("client_id", "secret");
$plaid->setHttpClient($httpClient);

try {
try
{
$plaid->items->get("access_token");
}
catch( PlaidRequestException $plaidRequestException ){
catch (PlaidRequestException $plaidRequestException)
{

$this->assertEquals(300, $plaidRequestException->getCode());

}
}

public function test_1xx_responses_throw_exception(): void
{
$httpClient = new Shuttle([
'handler' => new MockHandler([
function(Request $request) {
$httpClient = new Shuttle(
new MockHandler([
function (Request $request)
{

$requestParams = [
"method" => $request->getMethod(),
Expand All @@ -111,11 +114,10 @@ function(Request $request) {
"params" => \json_decode($request->getBody()->getContents()),
];

return new Response(100, \json_encode($requestParams));

return new Response(ResponseStatus::CONTINUE, \json_encode($requestParams));
}
])
]);
);

$plaid = new Plaid("client_id", "secret");
$plaid->setHttpClient($httpClient);
Expand All @@ -126,19 +128,19 @@ function(Request $request) {

public function test_3xx_responses_and_above_throw_exception(): void
{
$httpClient = new Shuttle([
'handler' => new MockHandler([
function(Request $request) {
$httpClient = new Shuttle(
new MockHandler([
function (Request $request)
{

$requestParams = [
"display_message" => "PLAID_ERROR",
];

return new Response(300, \json_encode($requestParams));

return new Response(ResponseStatus::MULTIPLE_CHOICES, \json_encode($requestParams));
}
])
]);
);

$plaid = new Plaid("client_id", "secret");
$plaid->setHttpClient($httpClient);
Expand All @@ -149,11 +151,11 @@ function(Request $request) {

public function test_invalid_json_when_parsing_response(): void
{
$httpClient = new Shuttle([
'handler' => new MockHandler([
$httpClient = new Shuttle(
new MockHandler([
new Response(ResponseStatus::OK, "invalid_json")
])
]);
);

$plaid = new Plaid("client_id", "secret");
$plaid->setHttpClient($httpClient);
Expand All @@ -162,4 +164,4 @@ public function test_invalid_json_when_parsing_response(): void

$plaid->items->get("access_token");
}
}
}
Loading