eliosfund / plaid-php-sdk
v1.0.4
2024-09-03 23:49 UTC
Requires
- php: ^8.2
- illuminate/support: ^10.0
- saloonphp/saloon: ^3.0
Requires (Dev)
- laravel/pint: ^1.16
- mockery/mockery: ^1.6
- pestphp/pest: ^2.34
- pestphp/pest-plugin-type-coverage: ^2.8
- phpstan/phpstan: ^1.11
This package is auto-updated.
Last update: 2024-09-23 05:48:47 UTC
README
一个PHP包,用于帮助您快速开始下一个Plaid集成。
简介
Plaid PHP SDK是一个PHP包,提供与Plaid API交互的简单易用的接口。
目录
安装
您可以使用Composer安装此包
composer require eliosfund/plaid-php-sdk
入门
要开始,应该创建一个新的Plaid
客户端实例。然后,您可以使用该实例与Plaid API进行交互。在以下示例中,我们请求一个公钥令牌,然后可以使用该令牌初始化一个Link
会话。
<?php use Plaid\Environment; use Plaid\Plaid; // Create the Plaid client $plaid = new Plaid( clientId: env('PLAID_CLIENT_ID'), clientSecret: env('PLAID_SECRET'), environment: Environment::SANDBOX ); // Request a public link token $token = $plaid ->link() ->publicTokenCreate([ "client_name" => env('APP_NAME'), "products" => ["auth"], "country_codes" => ["US"], "language" => "en", "user" => [ "client_user_id" => "1" ] ]) ->json('link_token');
版本控制
可以通过在请求中添加Plaid-Version
头来使用特定的Plaid API版本。以下示例演示了如何请求Auth
端点的特定版本。
<?php use Plaid\Environment; use Plaid\Exceptions\PlaidException; use Plaid\Http\Requests\Auth\GetRequest; use Plaid\Plaid; // Create the Plaid client $plaid = new Plaid( clientId: env('PLAID_CLIENT_ID'), clientSecret: env('PLAID_SECRET'), accessToken: $accessToken, environment: Environment::SANDBOX ); // Create a request $request = new GetRequest(); $request->headers()->add('Plaid-Version', '2020-09-14'); // Send the request $data = $plaid->send($request)->json();
错误处理
当API发生错误时,Plaid PHP SDK会抛出异常。您可以使用标准try/catch
块捕获这些异常并相应地处理它们。SDK抛出的所有错误都扩展了PlaidException
类。为了更优雅的错误处理方式,请考虑使用基于promise
的方法。
<?php use Plaid\Environment; use Plaid\Exceptions\PlaidException; use Plaid\Plaid; // Create the Plaid client $plaid = new Plaid( clientId: env('PLAID_CLIENT_ID'), clientSecret: env('PLAID_SECRET'), environment: Environment::SANDBOX ); // Perform a request try { $transactions = $plaid->transactions([ 'start_date' => '2021-01-01', 'end_date' => '2021-01-31' ])->get()->json(); } catch (PlaidException $exception) { // Handle API exceptions } catch (Exception $e) { // Handle PHP exceptions }
Promise支持
Plaid PHP SDK可以使用基于promise
的方法发送异步请求。这允许您以更优雅的方式处理成功和失败的请求。
<?php use Plaid\Environment; use Plaid\Http\Requests\Auth\GetRequest; use Plaid\Plaid; use Saloon\Exceptions\Request\RequestException; use Saloon\Http\Response; // Create the Plaid client $plaid = new Plaid( clientId: env('PLAID_CLIENT_ID'), clientSecret: env('PLAID_SECRET'), accessToken: $accessToken, environment: Environment::SANDBOX ); // Create a promise $promise = $plaid->sendAsync(new GetRequest()); // Send the request $promise ->then(function (Response $response) { // Handle successful response }) ->otherwise(function (RequestException $exception) { // Handle failed request }); // Force the promise to be resolved $promise->wait();
支持的产品
目前支持以下Plaid产品:
- 身份验证
- 身份
- 链接
- 沙箱
- 交易
- 转账
贡献
有关贡献的更多详细信息,请参阅此处。