DocuSign API 内嵌签名实现 Symfony

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

尝试将 DocuSign 嵌入式签名实施到我们的 Web 应用程序中,但我很难做到正确。文档有点分散,所以我无法获得正确的 API 调用流程。虽然我从 Docusign 建立了快速启动项目,但这并没有帮助我做对。

场景如下:我们希望用户(可能是多个)能够从我们的网站签署文档。该文档有以下字段与签名字段一起填写:

Date
Name
additional comments
number
。该文档将从应用程序内提供。现在我需要知道通过 PHP SDK 实现此场景所需遵循的正确步骤顺序。

我已经通过开发者帐户创建了应用程序和集成密钥。

更新

我已经能够将这段代码放在一起:


$accessToken = $this->generate_jwt_token();
        $config = new Configuration();
        $config->setHost('https://demo.docusign.net/restapi');
        $config->addDefaultHeader('Authorization', 'Bearer ' . $accessToken);
        $api_client = new ApiClient($config);
        $api_client->getOAuth()->setOAuthBasePath('https://account-d.docusign.com');
        $api_client->getConfig()->setAccessToken($accessToken);
        $api_client->getConfig()->addDefaultHeader('Authorization', 'Bearer ' . $accessToken);


        // Create an envelope from a template
        $relativePath = "/../web/dummy.pdf";
        $basePath = $this->container->get('kernel')->getRootDir() . $relativePath;
        $signer_name = 'John Smith';
        $signer_email = '[email protected]';
        $envelope_summary = $this->create_envelope_from_pdf($basePath, $signer_name, $signer_email, 'Dummy PDF', $api_client);

// Generate the URL for the embedded signing ceremony
        $envelope_id = $envelope_summary->getEnvelopeId();
        $recipient_name = 'John Smith';
        $recipient_email = '[email protected]';
        $signing_ceremony_url = $this->generate_signing_ceremony_url($envelope_id, $recipient_name, $recipient_email, $api_client);

// Redirect the user to the signing ceremony URL
        header('Location: ' . $signing_ceremony_url);
        exit;


function generate_jwt_token() {
        $private_key_path = 'PRIVATE_KEY';
        $jwt = array(
            'iss' => 'ACCOUNT_ID',
            'sub' => 'USER_ID',
            'aud' => 'https://demo.docusign.net/restapi',
            'exp' => time() + 3600
        );
        $jwt = JWT::encode($jwt, $private_key_path, 'RS256');
        return $jwt;
    }

    /**
     * @throws ApiException
     */
    function create_envelope_from_pdf($pdf_path, $signer_name, $signer_email, $document_name, $apiClient)
    {
        // Set up the envelope definition
        $envelope_definition = new EnvelopeDefinition();
        $envelope_definition->setEmailSubject('Please sign this document');
        $envelope_definition->setStatus('sent');

        // Add the document to the envelope
        $document = new Document();
        $document->setDocumentBase64(base64_encode(file_get_contents($pdf_path)));
        $document->setName($document_name);
        $document->setFileExtension('pdf');
        $envelope_definition->setDocuments([$document]);

        // Add the signer to the envelope
        $signer = new Signer();
        $signer->setName($signer_name);
        $signer->setEmail($signer_email);
        $signer->setRecipientId('1');
        $signer->setClientUserId('1234');

        // Add a sign here tab to the signer
        $sign_here = new SignHere();
        $sign_here->setDocumentId('1');
        $sign_here->setPageNumber('1');
        $sign_here->setRecipientId('1');
        $sign_here->setTabLabel('Sign Here');
        $sign_here->setXPosition('100');
        $sign_here->setYPosition('100');
        $signer->setTabs(new Tabs(['sign_here_tabs' => [$sign_here]]));
        $envelope_definition->setRecipients(new Recipients(['signers' => [$signer]]));

        // Create the envelope
        $envelopes_api = new EnvelopesApi($apiClient);
        $envelope_summary = $envelopes_api->createEnvelope('ACCOUNT_ID', $envelope_definition, new CreateEnvelopeOptions(['cdse_mode' => 'true']));

        return $envelope_summary;
    }

    /**
     * @throws ApiException
     */
    function generate_signing_ceremony_url($envelope_id, $recipient_name, $recipient_email, $apiClient) {
        global $api_client;
        $recipient_view_request = new RecipientViewRequest();
        $recipient_view_request->setReturnUrl('https://example.com/');
        $recipient_view_request->setClientUserId('1234');
        $recipient_view_request->setAuthenticationMethod('none');
        $recipient_view_request->setUserName($recipient_name);
        $recipient_view_request->setEmail($recipient_email);
        $recipient_view_request->setRecipientId('1');
//        $recipient_view_request->set('https://www.example.com');
        $envelopes_api = new EnvelopesApi($apiClient);
        $recipient_view = $envelopes_api->createRecipientView('ACCOUNT_ID', $envelope_id, $recipient_view_request);
        return $recipient_view->getUrl();

现在我收到这个错误:

Error while requesting server, received a non successful HTTP code [401] with response Body: O:8:"stdClass":2:{s:9:"errorCode";s:27:"AUTHORIZATION_INVALID_TOKEN";s:7:"message";s:59:"The access token provided is expired, revoked or malformed.";}

正确生成 JWT 令牌。调用

EnvelopesApi ->createEnvelope
时发生错误。

php docusignapi symfony-2.8
1个回答
0
投票

来自 DocuSign 开发人员支持的 Adrian,

您的代码看起来不错,但是,您的 JWT 部分有一些不正确的值:

这个:

 $private_key_path = 'PRIVATE_KEY';
        $jwt = array(
            'iss' => 'ACCOUNT_ID',
            'sub' => 'USER_ID',
            'aud' => 'https://demo.docusign.net/restapi',
            'exp' => time() + 3600

iss应该是你的integration Key,不是Account iD,

aud 应该是“account-d.docusign.com”。

不要忘记添加范围: “范围”:“签名模拟”

这是我们的 PHP JWT 指南,其中包含一个很好的代码示例,以防万一:

https://developers.docusign.com/docs/esign-rest-api/sdks/php/auth/

这就是我们的编码方式:

 self::$apiClient->getOAuth()->setOAuthBasePath($GLOBALS['JWT_CONFIG']['authorization_server']);
    true);
    $privateKey = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $GLOBALS['JWT_CONFIG']['private_key_file'], true);

    $scope = DocuSign::getDefaultScopes()[0];
    $jwt_scope = $scope . " impersonation"; 

    $response = self::$apiClient->requestJWTUserToken(
       $aud = $GLOBALS['JWT_CONFIG']['ds_client_id'],
       $aud = $GLOBALS['JWT_CONFIG']['ds_impersonated_user_id'],
       $aud = $privateKey,
       $aud = $jwt_scope
    );

我们希望这些信息有所帮助。

问候 阿德里安 DocuSign 开发者支持

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