在 Laravel 应用程序中使用 DocuSign API 自动同意发送信封

问题描述 投票:0回答:1

我已经在 Laravel 应用程序中实现了 DocuSign,并且目前正在使用 JWT 授权身份验证将文档发送给客户端进行签名。集成工作正常,但我遇到了一个问题:当我发送包含文档的信封供客户签名时,最初,我必须通过 DocuSign 同意页面手动提供同意。

由于我的应用程序被设计为通过触发器自动发送文档的后端微服务,因此此手动同意过程会中断流程。我想问是否有办法自动化这个同意过程,这样就可以发送文件而无需人工干预。

在此类后端服务中使用 DocuSign 时,是否有任何配置或 API 功能允许应用程序自动同意或预授权,或者是否有任何最佳实践?

这是我的服务类,负责准备和发送信封。

 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);
    }
docusignapi
1个回答
0
投票

您可以使用组织级别同意 https://developers-d.docusign.com/platform/auth/consent/obtaining-admin-consent-external/ 如果您的所有用户都在同一组织/帐户中

如果不是这种情况,您可以与合作伙伴团队合作寻找更适合ISV的解决方案,这样您就不必遇到同意问题。

© www.soinside.com 2019 - 2024. All rights reserved.