Docusign 将嵌入式视图和自定义信封与 pdf 相结合

Docusign combine Embedded view and custom envelope with pdf

我正在尝试结合 2 个 API 演练示例,但使用以下代码时出现 authorization_invalid_request 错误。

谁能帮我理解这是为什么?

我也不知道这是否是最佳流程,但我想要完成的是让用户根据为该用户保存在我的本地数据库中的 pdf 签署不同的文档,并动态地拥有模板创建而不是在 docusign 中预定义它。有没有更好的方法来实现它?

我的代码是:

<?php

class DocusignView {


    public static function getEmbeddedSignView($signerName, $templateId, $templateRoleName, $clientUserId)
    {        
        // Input your info:
    $email              = "***";              // your account email
    $password           = "***";                                   // your account password
    $integratorKey      = "***";  // your account integrator key, found on (Preferences -> API page)
    $recipientName      = '***';                    // provide a recipient (signer) name
    $templateId         = '***';       // provide a valid templateId of a template in your account
    $templateRoleName   = 'Employee';                                   // use same role name that exists on the template in the console
    $clientUserId       = '1';                                          // to add an embedded recipient you must set their clientUserId property in addition to
                                                                            // the recipient name and email.  Whatever you set the clientUserId to you must use the same
                                                                            // value when requesting the signing URL
        // construct the authentication header:
    $header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";

    /////////////////////////////////////////////////////////////////////////////////////////////////
    // STEP 1 - Login (retrieves baseUrl and accountId)
    /////////////////////////////////////////////////////////////////////////////////////////////////
    $url = "https://demo.docusign.net/restapi/v2/login_information";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));

    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ( $status != 200 ) {
        echo "error calling webservice, status is:" . $status;
        exit(-1);
    }

    $response = json_decode($json_response, true);
    $accountId = $response["loginAccounts"][0]["accountId"];
    $baseUrl = $response["loginAccounts"][0]["baseUrl"];
    curl_close($curl);




    $envelopeId = DocusignView::requestSignByDoc($clientUserId);//$response["envelopeId"];

     /////////////////////////////////////////////////////////////////////////////////////////////////
    // STEP 2 - Get the Embedded Singing View 
    /////////////////////////////////////////////////////////////////////////////////////////////////
    $data = array("returnUrl" => "http://www.docusign.com/devcenter",
        "authenticationMethod" => "None", "email" => $email, 
        "userName" => $recipientName, "clientUserId" => $clientUserId
    );                                                                    

    $data_string = json_encode($data);    
    $curl = curl_init($baseUrl . "/envelopes/$envelopeId/views/recipient" );
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string),
        "X-DocuSign-Authentication: $header" )                                                                       
    );

    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ( $status != 201 ) {
        echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
        print_r($json_response); echo "\n";
        exit(-1);
    }

    $response = json_decode($json_response, true);
    $url = $response["url"];

    //--- display results
    return $url; 
    }




    public static function requestSignByDoc($clientUserId){        

    // Input your info here:
    $email = "***";         // your account email
    $password = "***";      // your account password
    $integratorKey = "***";     // your account integrator key, found on (Preferences -> API page)
    $recipientName = "***";     // provide a recipient (signer) name
    $documentName = "***.pdf";      // copy document with same name into this directory!

    // construct the authentication header:
    $header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";

    /////////////////////////////////////////////////////////////////////////////////////////////////
    // STEP 1 - Login (to retrieve baseUrl and accountId)
    /////////////////////////////////////////////////////////////////////////////////////////////////
    $url = "https://demo.docusign.net/restapi/v2/login_information";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));

    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ( $status != 200 ) {
        echo "error calling webservice, status is:" . $status;
        exit(-1);
    }

    $response = json_decode($json_response, true);
    $accountId = $response["loginAccounts"][0]["accountId"];
    $baseUrl = $response["loginAccounts"][0]["baseUrl"];
    curl_close($curl);

    //--- display results
//  echo "\naccountId = " . $accountId . "\nbaseUrl = " . $baseUrl . "\n";

    /////////////////////////////////////////////////////////////////////////////////////////////////
    // STEP 2 - Create an envelope with one recipient, one tab, and one document and send
    /////////////////////////////////////////////////////////////////////////////////////////////////

    // the following request body will place 1 signature tab on the document you supply, located
    // 100 pixels to the right and 100 pixels down from the top left of the document
    $data = array (
            "emailSubject" => "DocuSign API - Signature Request on Document",
            "documents" => array( array( "documentId" => "1", "name" => $documentName)),
            "recipients" => array( "signers" => array(
                array(  "email" => $email,
                        "name" => $recipientName,
                        "recipientId"=> '1',
                        "clientUserId" => $clientUserId,
                        "tabs" => array(
                            "signHereTabs" => array(
                                array(  "anchorString"            => "Signed .....................................................",
                                        "anchorXOffset"           => "0",
                                        "anchorYOffset"           => "1",
                                        "anchorIgnoreIfNotPresent"=> "false",
                                        "anchorUnits"             => "inches" )
                            ))
                 ))
            ),
        "status" => "created"
    );
    $data_string = json_encode($data);  
    $temp = __DIR__.'/***.pdf';
    $file_contents = file_get_contents($temp);

    $requestBody = "\r\n"
    ."\r\n"
    ."--myboundary\r\n"
    ."Content-Type: application/json\r\n"
    ."Content-Disposition: form-data\r\n"
    ."\r\n"
    ."$data_string\r\n"
    ."--myboundary\r\n"
    ."Content-Type:application/pdf\r\n"
    ."Content-Disposition: file; filename=\"$documentName\"; documentid=1 \r\n"
    ."\r\n"
    ."$file_contents\r\n"
    ."--myboundary--\r\n"
    ."\r\n";

    // *** append "/envelopes" to baseUrl and as signature request endpoint
    $curl = curl_init($baseUrl . "/envelopes" );
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);                                                                  
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: multipart/form-data;boundary=myboundary',
        'Content-Length: ' . strlen($requestBody),
        "X-DocuSign-Authentication: $header" )                                                                       
    );

    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ( $status != 201 ) {
        echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
        print_r($json_response); echo "\n";
        exit(-1);
    }

    $response = json_decode($json_response, true);
    $envelopeId = $response["envelopeId"];

        return $envelopeId;
    }
}

我的JSON请求:

"{
"returnUrl":"http:\/\/www.docusign.com\/devcenter",
"authenticationMethod":"None",
"email":"***",
"userName":"***",
"clientUserId":"1"
}"

我的 JSON 回复:

"{
   "errorCode": "AUTHORIZATION_INVALID_REQUEST",
   "message": "The authorization request is malformed."
 }"

我的身份验证方法设置为空,但我没有问题运行当我没有使用自定义信封(即请求文档签名)时的嵌入式视图示例。

下面是使用 API 同样问题的尝试:

$docuSignClient         = new DocuSign_Client();
$docuService            = new DocuSign_ViewsService($docuSignClient);
$viewResource           = new DocuSign_ViewsResource($docuService);                
$signatureResource      = new DocuSign_RequestSignatureResource($docuService);

$temp = __DIR__.'/test.pdf';
$file_contents = file_get_contents($temp);
$document[]               = new DocuSign_Document('test.pdf', '1', $file_contents);

$recipient[]              = new DocuSign_Recipient('1', '1', 'Signer1', 'recipientsEmail@email.com', '1', 'signers');

$envelopeId  = $signatureResource->createEnvelopeFromDocument('TEST EMAIL SUBJECT', "PLease sign this", "created", $document, $recipient)->envelopeId;                
$returnUrl   = $request->getUri();
$url         = $viewResource->getRecipientView($returnUrl, $envelopeId, 'Signer1', 'recipientsEmail@email.com', '1'); 

我错过了什么?

所以这个错误是因为信封处于 draft/created 状态并且没有发送!有道理,但花了很长时间才弄明白。