Skip to content

Commit

Permalink
It works
Browse files Browse the repository at this point in the history
  • Loading branch information
hotmeteor committed Sep 3, 2022
1 parent e559bdc commit 5e36524
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 63 deletions.
130 changes: 130 additions & 0 deletions src/Connect/Connect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Phinch\Connect;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class Connect
{
/**
* @var array|string[]
*/
protected array $products = [
'company',
'directory'
];

/**
* @var string|null
*/
protected ?string $state = null;

/**
* @var string|null
*/
protected ?string $provider = null;

/**
* @var bool
*/
protected bool $sandbox = false;

/**
* @param string $client_id
* @param string $client_secret
* @param string $redirect_uri
*/
public function __construct(protected string $client_id, protected string $client_secret, protected string $redirect_uri)
{
}

/**
* Build the URL to open Finch Connect
* https://developer.tryfinch.com/docs/reference/61cff54e1d9b3-your-application-redirects-to-finch-connect#open-finch-connect
*
* @return string
*/
public function redirectUrl(): string
{
$query = http_build_query(array_filter([
'client_id' => $this->client_id,
'redirect_uri' => $this->redirect_uri,
'products' => implode(' ', $this->products),
'sandbox' => $this->sandbox,
'state' => $this->state,
'payroll_provider' => $this->provider,
]));

return "https://connect.tryfinch.com/authorize?".$query;
}

/**
* Exchange the code for the access token.
* https://developer.tryfinch.com/docs/reference/61cff54e1d9b3-your-application-redirects-to-finch-connect#obtain-consent
*
* @param string $code
* @return string
* @throws GuzzleException
*/
public function exchange(string $code): string
{
$client = new Client([
'json' => [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'code' => $code,
'redirect_uri' => $this->redirect_uri,
],
]);

$response = $client->post("https://api.tryfinch.com/auth/token");

$data = json_decode($response->getBody());

return $data->access_token;
}

/**
* @param array $products
* @return $this
*/
public function products(array $products): static
{
$this->products = $products;

return $this;
}

/**
* @param mixed $value
* @return $this
*/
public function state(mixed $value): static
{
$this->state = $value;

return $this;
}

/**
* @param string $provider
* @return $this
*/
public function payrollProvider(string $provider): static
{
$this->provider = $provider;

return $this;
}

/**
* @return $this
*/
public function inSandbox(): static
{
$this->sandbox = true;

return $this;
}
}
6 changes: 3 additions & 3 deletions src/Finch/Management.php → src/Management.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace Phinch\Finch;
namespace Phinch;

use GuzzleHttp\Exception\GuzzleException;

class Management
{
/**
* @param FinchClient $client
* @param PhinchClient $client
*/
public function __construct(protected FinchClient $client)
public function __construct(protected PhinchClient $client)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Finch/Paginated.php → src/Paginated.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Phinch\Finch;
namespace Phinch;

class Paginated
{
Expand All @@ -10,7 +10,7 @@ class Paginated
* @param array $data
* @param string|null $resultsKey
*/
public function __construct(public array $data, public ?string $resultsKey = null)
public function __construct(protected array $data, protected ?string $resultsKey = null)
{
}

Expand Down
36 changes: 23 additions & 13 deletions src/Finch/Finch.php → src/Phinch.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
<?php

namespace Phinch\Finch;
namespace Phinch;

use GuzzleHttp\Exception\GuzzleException;

class Finch
class Phinch
{
/**
* @var FinchClient
*/
protected FinchClient $client;

/**
* Finch constructor.
*
* @param FinchClient $client
* @param PhinchClient $client
*/
public function __construct(FinchClient $client)
public function __construct(protected PhinchClient $client)
{
$this->client = $client;
}

/**
Expand All @@ -27,11 +21,17 @@ public function __construct(FinchClient $client)
*
* @return mixed
*/
public function __call($name, $arguments)
public function __call($name, mixed $arguments)
{
if ($name === 'authorize') {
$this->client->authorize($arguments[0]);

return $this;
}

$product_name = ucwords($name);

$class = __CLASS__.'\\Products\\'.$product_name;
$class = __NAMESPACE__ . '\\Products\\' . $product_name;

if (class_exists($class)) {
return new $class($this->client);
Expand All @@ -51,7 +51,7 @@ public function __get($name)
return call_user_func($this->{$name}, func_get_args());
}

return $this->__call($name);
return $this->__call($name, func_get_args());
}

/**
Expand All @@ -74,6 +74,9 @@ public function token($code): array
}

/**
* Read account information associated with an access_token
* https://developer.tryfinch.com/docs/reference/eee6e798b0f93-introspect
*
* @param $access_token
*
* @return array
Expand All @@ -85,6 +88,10 @@ public function introspect($access_token): array
}

/**
* Disconnect an employer from your application and invalidate all access_tokens associated with the employer.
* We require applications to implement the Disconnect endpoint for billing and security purposes.
* https://developer.tryfinch.com/docs/reference/c65ecbd512332-disconnect
*
* @param $access_token
*
* @return array
Expand All @@ -96,6 +103,9 @@ public function disconnect($access_token): array
}

/**
* Return details on all available payroll and HR systems.
* https://developer.tryfinch.com/docs/reference/327c384190aeb-providers
*
* @return array
* @throws GuzzleException
*/
Expand Down
Loading

0 comments on commit 5e36524

Please sign in to comment.