
本文详细阐述如何通过docusign api获取信封被取消或签署人拒绝的具体原因。不同于简单的getenvelope调用,获取此类详细信息需要查询信封的审计追踪(audit trail)。教程将指导您如何调用相关api获取审计事件列表,并从中解析出信封取消或拒绝的事件及其附带的原因。
在使用DocuSign API管理电子签名流程时,开发者经常需要获取信封的当前状态。虽然EnvelopesApi::getEnvelope方法可以提供信封的总体状态(例如“已发送”、“已完成”、“已作废”等),但它通常不包含导致信封取消或签署人拒绝的具体文字原因。例如,当一个信封被作废(Voided)或被签署人拒绝(Rejected)时,用户通常会输入一个理由,而这个理由并不会直接通过getEnvelope调用返回。
要获取这些详细的、用户输入的取消或拒绝原因,我们需要深入到DocuSign的审计追踪机制。
DocuSign为每个信封维护一个详细的审计追踪日志,记录了信封生命周期中的所有关键事件。这些事件包括信封的创建、发送、签署、作废、拒绝等,并且许多事件都附带了详细的上下文信息,包括用户输入的理由。
获取信封的取消或拒绝原因的关键在于:
DocuSign API提供了专门的端点来获取信封的审计事件。在PHP SDK中,这通常通过EnvelopesApi::getAuditEvents方法实现。
以下是获取信封审计事件的PHP代码示例:
<?php
namespace App\Services;
use DocuSign\eSign\Api\EnvelopesApi;
use DocuSign\eSign\Client\ApiClient;
use DocuSign\eSign\Configuration;
use Exception;
class DocusignEnvelopeService
{
private $token;
private $envelope_id;
private $account_id;
public function __construct(string $token, string $envelope_id, string $account_id)
{
$this->token = $token;
$this->envelope_id = $envelope_id;
$this->account_id = $account_id;
}
/**
* 获取DocuSign信封的审计事件列表
*
* @return array|null 审计事件列表或null(如果发生错误)
*/
public function getEnvelopeAuditEvents(): ?array
{
try {
$config = new Configuration();
$config->setHost(env('DOCUSIGN_BASE_URL'));
$config->addDefaultHeader('Authorization', 'Bearer ' . $this->token);
$api_client = new ApiClient($config);
$envelope_api = new EnvelopesApi($api_client);
// 调用getAuditEvents方法获取审计事件
// 该方法返回一个EnvelopeAuditEventResponse对象,其中包含一个auditEvents数组。
$auditEventsResponse = $envelope_api->getAuditEvents($this->account_id, $this->envelope_id);
if ($auditEventsResponse && $auditEventsResponse->getAuditEvents()) {
return $auditEventsResponse->getAuditEvents();
}
return null;
} catch (Exception $e) {
// 捕获并处理API调用过程中可能发生的异常
error_log("Error fetching DocuSign audit events: " . $e->getMessage());
return null;
}
}
/**
* 从审计事件中解析出信封取消或拒绝的原因
*
* @return string|null 取消或拒绝的原因,如果没有找到则返回null
*/
public function getCancellationOrRejectionReason(): ?string
{
$auditEvents = $this->getEnvelopeAuditEvents();
if (empty($auditEvents)) {
return null;
}
foreach ($auditEvents as $event) {
// 审计事件对象通常包含一个'description'字段来描述事件类型
// 常见的取消/拒绝事件描述可能包括 "Envelope Voided", "Recipient Rejected" 等
$eventDescription = $event->getDescription(); // 假设DocuSign SDK的EnvelopeAuditEvent对象有getDescription()方法
if (strpos($eventDescription, 'Voided') !== false || strpos($eventDescription, 'Rejected') !== false) {
// 如果事件描述包含“Voided”或“Rejected”,则尝试提取原因
// 原因通常在事件对象的某个字段中,例如'reason'或'voidReason'
// 具体的字段名需要根据实际的API响应结构来确定
// 这里假设存在getReason()或getVoidReason()方法
if (method_exists($event, 'getReason') && $event->getReason()) {
return $event->getReason();
}
if (method_exists($event, 'getVoidReason') && $event->getVoidReason()) {
return $event->getVoidReason();
}
// 有些情况下,原因可能直接包含在description中,或者需要更复杂的解析
// 例如: "Envelope Voided by Sender. Reason: [Your Reason Here]"
if (preg_match('/Reason:\s*(.*)/i', $eventDescription, $matches)) {
return trim($matches[1]);
}
}
}
return null; // 未找到取消或拒绝原因
}
}
// 示例用法:
// $yourAccessToken = 'YOUR_DOCUSIGN_ACCESS_TOKEN';
// $yourEnvelopeId = 'YOUR_DOCUSIGN_ENVELOPE_ID';
// $yourAccountId = env('DOCUSIGN_ACCOUNT_ID'); // 从环境变量获取账户ID
// $docusignService = new DocusignEnvelopeService($yourAccessToken, $yourEnvelopeId, $yourAccountId);
// $reason = $docusignService->getCancellationOrRejectionReason();
// if ($reason) {
// echo "信封取消/拒绝原因:" . $reason . PHP_EOL;
// } else {
// echo "未找到信封取消/拒绝原因或信封未被取消/拒绝。" . PHP_EOL;
// }代码说明:
要获取DocuSign信封被取消或拒绝的详细原因,不能仅仅依赖于getEnvelope方法。核心策略是利用EnvelopesApi::getAuditEvents方法获取信封的完整审计追踪,然后通过遍历和解析这些事件,定位到相关的取消或拒绝事件,并从中提取出用户提供的具体原因。理解并正确实现这一机制,将大大增强您对DocuSign信封生命周期管理和问题诊断的能力。
以上就是DocuSign API:获取信封取消或拒绝原因的详细教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号