tucker-eric / docusign-rest-client
官方 Docusign Esign 库的 PHP 封装,实现 Docusign REST API
Requires
- php: ^7.3 | ^8.0
- ext-json: *
- docusign/esign-client: ^6.0
Requires (Dev)
- phpunit/phpunit: ~9.5
README
官方 Docusign PHP 客户端库 的封装。
此库处理 DocuSign Api 探索器 中所有端点的对象实例化。
通过 Composer 安装
composer require tucker-eric/docusign-rest-client
用法
DocuSign\Rest\Client
访问所有模型和 API 端点,并返回相应的对象。它还处理所有身份验证以及向任何需要它的方法传递 $account_id
。
API 端点
DocuSign\eSign\Api
命名空间中的所有类都作为 DocuSign\Rest\Client
的 属性 可用。
每个属性都是原始类名驼峰命名,不带 Api
后缀。
示例
<?php require 'vendor/autoload.php'; $client = new DocuSign\Rest\Client([ 'impersonated_user_id' => $impersonated_user_id, 'integrator_key' => $integrator_key, 'host' => $host, 'private_key' => $private_key, 'auth_server' => $auth_server ]); $client->accounts // Returns DocuSign\eSign\Api\AccountsApi $client->customTabs // Returns DocuSign\eSign\Api\CustomTabsApi $client->folders // Returns DocuSign\eSign\Api\FoldersApi
客户端处理将 $account_id
注入任何需要它的方法。
因此,调用如 DocuSign\eSign\Api\FoldersApi
list()
方法的操作就像
$client->folders->list(); // Returns \DocuSign\eSign\Model\FoldersResponse
模型
DocuSign\eSign\Model
命名空间中的所有类都作为 DocuSign\Rest\Client
的 camel cased 方法 可用,并接受一个 snake cased 参数数组,这些参数将被设置在模型上。调用该方法将返回该模型对象。
示例
<?php require 'vendor/autoload.php'; $client = new DocuSign\Rest\Client([ 'impersonated_user_id' => $impersonated_user_id, 'private_key' => $private_key, 'integrator_key' => $integrator_key, 'host' => $host, 'auth_server' => $auth_server ]); $templateRole = $client->templateRole([ 'email' => '[SIGNER_EMAIL]', 'name' => '[SIGNER_NAME]', 'role_name' => '[ROLE_NAME]' ]); // Returns DocuSign\eSign\Model\TemplateRole $templateRole->getRoleName() // returns '[ROLE_NAME]' as set in the above method. $envelopeDefinition = $client->envelopeDefinition([ 'status' => 'sent' 'email_subject' => '[DocuSign PHP SDK] - Signature Request Sample', 'template_id' => '[TEMPLATE_ID]', 'template_roles' => [ $templateRole ], ]); // Returns DocuSign\eSign\Model\EnvelopeDefinition
API 选项
DocuSign\eSign\Api
命名空间中的一些类具有选项对象,可以创建并传递给该类的任何方法。这些对象可以通过从该 API 类调用它作为方法来访问。选项方法接受一个 snake cased 参数数组来设置。您还可以不传递任何参数调用此方法,它将返回选项对象,然后您可以使用该对象的 setter 方法设置其属性
DocuSign\eSign\Api\FoldersApi
类中的 ->search()
方法接受 DocuSign\eSign\Api\FoldersApi\SearchOptions
来设置选项。这些选项设置如下所示
示例
$foldersApi = $this->client->folders; $searchOptions = $foldersApi->searchOptions([ 'count' => 20, 'order_by' => 'sent', 'from_date' => '2010-01-01' ]); $foldersApi->search($searchOptions);
您可以使用 getOptions
方法检索 API 类上之前设置的任何选项。传递用于设置选项的方法的名称返回该选项对象;或者不传递任何参数,将返回一个数组,其中包含该 API 类的所有选项对象,按方法名称键入。
示例
$envelopesApi = $this->client->envelopes; $envelopesApi->createEnvelopeOptions([ 'merge_roles_on_draft' => 'true' ]); $envelopesApi->updateOptions([ 'resend_envelope' => 'true' ]); $envelopesApi->getOptions(); // returns ['updateOptions' => DocuSign\eSign\Api\EnvelopesApi\UpdateOptions, 'createEnvelopeOptions' => DocuSign\eSign\Api\EnvelopesApi\CreateEnvelopeOptions] $envelopesApi->getOptions('updateOptions') // returns DocuSign\eSign\Api\EnvelopesApi\UpdateOptions
示例
登录并发送来自模板的签名请求
这将产生与官方 Docusign 客户端中的此示例相同的结果
<?php require 'vendor/autoload.php'; class DocuSignSample { public function signatureRequestFromTemplate() { $impersonated_user_id = "[IMPERSONATED_USER_ID]"; $private_key = "[PRIVATE_KEY]"; $integrator_key = "[INTEGRATOR_KEY]"; // change these to production before going live $host = "https://demo.docusign.net/restapi"; $auth_server = "account-d.docusign.com"; // Once instantiated, authentication is handled automatically $client = new DocuSign\Rest\Client([ 'impersonated_user_id' => $impersonated_user_id, 'private_key' => $private_key, 'integrator_key' => $integrator_key, 'host' => $host, 'auth_server' => $auth_server ]); $templateRole = $client->templateRole([ 'email' => '[SIGNER_EMAIL]', 'name' => '[SIGNER_NAME]', 'role_name' => '[ROLE_NAME]' ]); $envelopeDefinition = $client->envelopeDefinition([ 'status' => 'sent', 'email_subject' => '[DocuSign PHP SDK] - Signature Request Sample', 'template_id' => '[TEMPLATE_ID]', 'template_roles' => [ $templateRole ], ]); $envelopeOptions = $client->envelopes->createEnvelopeOptions([ 'merge_roles_on_draft' => false ]); $envelopeSummary = $client->envelopes->createEnvelope($envelopeDefinition, $envelopeOptions); } }
将相同的示例重构为其自己的类
<?php require 'vendor/autoload.php'; class DocuSignSample { protected $impersonated_user_id = "[IMPERSONATED_USER_ID]"; protected $private_key = "[PRIVATE_KEY]"; protected $integrator_key = "[INTEGRATOR_KEY]"; // change these to production before going live protected $host = "https://demo.docusign.net/restapi"; protected $auth_server = "account-d.docusign.com"; protected $client; public function __construct() { // Once instantiated, authentication is handled automatically $this->client = new DocuSign\Rest\Client([ 'impersonated_user_id' => $impersonated_user_id, 'private_key' => $private_key, 'integrator_key' => $integrator_key, 'host' => $host, 'auth_server' => $auth_server ]); } /** * @return DocuSign\eSign\Model\EnvelopeSummary */ public function signatureRequestFromTemplate() { return $this->client->envelopes->createEnvelope($this->client->envelopeDefinition([ 'status' => 'sent', 'email_subject' => '[DocuSign PHP SDK] - Signature Request Sample', 'template_id' => '[TEMPLATE_ID]', 'template_roles' => [ $this->client->templateRole([ 'email' => '[SIGNER_EMAIL]', 'name' => '[SIGNER_NAME]', 'role_name' => '[ROLE_NAME]' ]) ] ]) ); } }