我已经在 Laravel 应用程序中实现了 DocuSign,并且目前使用 JWT Grant Authentication 将文档发送给客户进行签名。集成工作正常,但我遇到了一个问题:当我发送带有文档的信封供客户签名时,最初我必须通过 DocuSign 同意页面手动提供同意。
由于我的应用程序设计为后端微服务,通过触发器自动发送文档,因此此手动同意流程会中断流程。我想问一下是否有办法使此同意流程自动化,这样无需人工干预即可发送文档。
是否有任何配置或 API 功能允许应用程序自动同意或预授权,或者在这样的后端服务中使用 DocuSign 时有任何最佳实践?
这是我的负责准备和发送信封的服务类。
public function getToken(ApiClient $apiClient): string
{
try {
$privateKey = file_get_contents(storage_path(env('DS_KEY_PATH')), true);
$response = $apiClient->requestJWTUserToken(
env('DS_CLIENT_ID'),
env('DS_IMPERSONATED_USER_ID'),
$privateKey,
env('DS_JWT_SCOPE')
);
$token = $response[0];
return $token->getAccessToken();
} catch (\Throwable $th) {
if (strpos($th->getMessage(), 'consent_required') !== false) {
throw new \Exception('consent_required');
}
throw $th;
}
}
/**
* Build and return the consent URL
*
* @return string
*/
public function getConsentUrl(): string
{
return "https://account-d.docusign.com/oauth/auth?response_type=code&scope=signature%20impersonation&client_id="
. env('DS_CLIENT_ID')
. "&redirect_uri=" . urlencode(env('DS_REDIRECT_URI'));
}
/**
* Build the envelope definition from the request
*
* @param Request $request
* @return EnvelopeDefinition
*/
public function buildEnvelope(Request $request): EnvelopeDefinition
{
$fileContent = $request->file('formFile')->get();
$fileName = $request->file('formFile')->getClientOriginalName();
$fileExtension = $request->file('formFile')->getClientOriginalExtension();
$recipientEmail = $request['email'];
$recipientName = $request['name'];
$document = new Document([
'document_id' => "1",
'document_base64' => base64_encode($fileContent),
'file_extension' => $fileExtension,
'name' => $fileName
]);
$sign_here_tab = new SignHere([
'anchor_string' => "FIRMA CLIENTE",
'page_number' => "1",
'anchor_units' => "pixels",
'anchor_x_offset' => "40",
'anchor_y_offset' => "40"
]);
$tabs = new Tabs([
'sign_here_tabs' => [$sign_here_tab]
]);
$signer = new Signer([
'email' => $recipientEmail,
'name' => $recipientName,
'recipient_id' => "1",
'tabs' => $tabs
]);
$recipients = new Recipients([
'signers' => [$signer]
]);
$inlineTemplate = new InlineTemplate([
'recipients' => $recipients,
'sequence' => "1"
]);
$compositeTemplate = new CompositeTemplate([
'composite_template_id' => "1",
'document' => $document,
'inline_templates' => [$inlineTemplate]
]);
return new EnvelopeDefinition([
'composite_templates' => [$compositeTemplate],
'email_subject' => "Please sign",
'status' => "sent"
]);
}
/**
* Send the envelope using the DocuSign API
*
* @param ApiClient $apiClient
* @param string $accountId
* @param EnvelopeDefinition $envelopeDefinition
* @return object
*/
public function sendEnvelope(ApiClient $apiClient, string $accountId, EnvelopeDefinition $envelopeDefinition): object
{
$envelopeApi = new EnvelopesApi($apiClient);
return $envelopeApi->createEnvelope($accountId, $envelopeDefinition);
}