eliosfund/plaid-php-sdk

安装: 569

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 0

开放问题: 2

类型:项目

v1.0.4 2024-09-03 23:49 UTC

README

Test Suite Downloads Packagist Version GitHub License codecov

一个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产品:

  • 身份验证
  • 身份
  • 链接
  • 沙箱
  • 交易
  • 转账

贡献

有关贡献的更多详细信息,请参阅此处