
DocuSign API的getEnvelope方法无法直接获取信封的取消原因。要获取此信息,开发者需要通过API检索信封的审计事件(Audit Events)列表。然后,遍历这些事件,查找与信封作废或取消相关的特定事件,从中提取详细的取消理由。
在DocuSign的API开发实践中,许多开发者在尝试获取信封(Envelope)的详细状态时,会首先想到使用EnvelopesApi::getEnvelope方法。然而,该方法主要返回信封的元数据和当前状态,并不直接包含信封被作废(Void)或拒绝(Declined)时的具体原因。要获取这一关键信息,我们需要深入到信封的审计追踪(Audit Trail)中。
DocuSign信封的审计追踪记录了信封生命周期中的每一个重要事件,包括创建、发送、签名、查看、作废、拒绝等。每个事件都包含时间戳、执行者以及相关的详细信息。信封的取消或作废原因,就作为特定审计事件的一部分被记录下来。通过分析这些事件,我们可以精确地追踪信封状态变化的来龙去脉。
要获取信封的审计事件,我们需要使用EnvelopesApi中的getAuditEvents方法。这个方法将返回一个包含信封所有历史事件的列表。
以下是一个使用PHP DocuSign eSign SDK获取审计事件并解析取消原因的示例代码:
<?php
require_once(__DIR__ . '/vendor/autoload.php'); // 根据您的项目结构调整
use DocuSign\eSign\Client\ApiClient;
use DocuSign\eSign\Configuration;
use DocuSign\eSign\Api\EnvelopesApi;
use DocuSign\eSign\Model\EnvelopeAuditEvents; // 引入DocuSign SDK的模型
class DocuSignEnvelopeService
{
private $apiClient;
private $accountId;
private $envelopeId;
private $accessToken;
public function __construct(string $accessToken, string $accountId, string $envelopeId, string $basePath)
{
$this->accessToken = $accessToken;
$this->accountId = $accountId;
$this->envelopeId = $envelopeId;
$config = new Configuration();
$config->setHost($basePath);
$config->addDefaultHeader('Authorization', 'Bearer ' . $this->accessToken);
$this->apiClient = new ApiClient($config);
}
/**
* 获取DocuSign信封的审计事件列表。
*
* @return array|null 审计事件列表(EnvelopeAuditEvent对象数组)或null(如果发生错误)
*/
public function getEnvelopeAuditEvents(): ?array
{
try {
$envelopeApi = new EnvelopesApi($this->apiClient);
/** @var EnvelopeAuditEvents $results */
$results = $envelopeApi->getAuditEvents($this->accountId, $this->envelopeId);
return $results->getEvents(); // 获取事件数组
} catch (\DocuSign\eSign\Client\ApiException $e) {
error_log("DocuSign API Error fetching audit events: " . $e->getMessage());
// 可以在此处添加更详细的错误处理和日志记录
return null;
} catch (Exception $e) {
error_log("General Error fetching audit events: " . $e->getMessage());
return null;
}
}
/**
* 从审计事件中解析信封的取消或作废原因。
*
* @return string|null 取消原因字符串或null(如果未找到)
*/
public function parseCancellationReason(): ?string
{
$events = $this->getEnvelopeAuditEvents();
if (empty($events)) {
return null;
}
foreach ($events as $event) {
// 审计事件对象(EnvelopeAuditEvent)通常包含以下方法来获取信息
// getEventDescription():事件描述,如 "Envelope Voided"
// getEventFields():一个包含额外字段(如原因)的数组
$eventDescription = $event->getEventDescription();
// 检查事件描述是否包含“Voided”或“Cancelled”
if (strpos($eventDescription, 'Voided') !== false || strpos($eventDescription, 'Cancelled') !== false) {
$eventFields = $event->getEventFields();
if (!empty($eventFields)) {
foreach ($eventFields as $field) {
// 查找名为 'Reason' 或 'VoidReason' 的字段
if (isset($field['name']) && ($field['name'] === 'Reason' || $field['name'] === 'VoidReason') && isset($field['value'])) {
return $field['value'];
}
}
}
// 如果在eventFields中未找到,尝试从eventDescription中解析
// 这种方式是备用方案,可能需要更复杂的字符串匹配
// 例如,"Envelope Voided by [User Name] with reason: [Reason Text]"
if (preg_match('/reason:\s*(.+)/i', $eventDescription, $matches)) {
return $matches[1];
}
}
}
return null; // 未找到取消原因
}
}
// 示例用法:
// $accessToken = 'YOUR_DOCUSIGN_ACCESS_TOKEN'; // 从OAuth流程获取
// $accountId = 'YOUR_DOCUSIGN_ACCOUNT_ID'; // DocuSign账户ID
// $envelopeId = 'YOUR_DOCUSIGN_ENVELOPE_ID'; // 目标信封ID
// $basePath = 'https://demo.docusign.net/restapi'; // 沙箱环境URL,生产环境为 'https://www.docusign.net/restapi'
// $service = new DocuSignEnvelopeService($accessToken, $accountId, $envelopeId, $basePath);
// $cancellationReason = $service->parseCancellationReason();
// if ($cancellationReason !== null) {
// echo "信封取消原因: " . $cancellationReason . "\n";
// } else {
// echo "未找到信封取消原因或信封未被取消。\n";
// }
?>代码说明:
获取DocuSign信封的取消或作废原因不能通过简单的getEnvelope调用实现。正确的做法是利用DocuSign API提供的getAuditEvents方法,获取信封的完整审计事件列表。然后,通过遍历并解析这些事件,定位到作废事件,并从中提取出详细的取消理由。这种方法确保了对信封生命周期中关键状态变化的全面追踪和理解,对于构建健壮的DocuSign集成应用至关重要。
以上就是获取DocuSign信封取消原因的API教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号