2016-03-10 66 views
0

我有一个网络应用程序,通过一系列表单获取用户,然后生成一个PDF格式的答案。我想在PDF表单的末尾添加一个Docusign签名,以便在完成在线表单后,他们被要求在iframe中签名。Docusign REST API获取文档信封的收件人URL

我开始与这个文档 - https://www.docusign.com/developer-center/recipes/signing-from-your-app然后拿着步骤2,从这个文档创建我的文档信封 - https://www.docusign.com/developer-center/recipes/request-a-signature-via-email

我的代码创建的信封ID很好,但是当我试图让使用/envelopes/$envelopeId/views/recipient网址我得到的状态400错误:

{ "errorCode": "ACCOUNT_NOT_AUTHORIZED_FOR_ENVELOPE", "message": "This account is not authorized to access the requested envelope." } 

这是我下面全码:

<?php 

$docusign_username = "[email protected]"; 
$docusign_password = "mypassword"; 
$docusign_integrator_key = "my-integrator-key"; 

$applicant_email = "[email protected]"; 
$applicant_name = "John Smith"; 
$applicant_unique_id = "123"; 

$application_unique_id = "31587"; 
$application_form_pdf = "31587.pdf"; 

// construct the authentication header: 
$header = "<DocuSignCredentials><Username>" . $docusign_username . "</Username><Password>" . $docusign_password . "</Password><IntegratorKey>" . $docusign_integrator_key . "</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); 

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


////////////////////////////////////////////////////////////////////////////////////// 
// STEP 2 - Create an envelope with document 
////////////////////////////////////////////////////////////////////////////////////// 
$data = 
    array (
     "emailSubject" => "DocuSign API - Please sign " . $application_form_pdf, 
     "documents" => array(
      array("documentId" => $application_unique_id, "name" => $application_form_pdf) 
      ), 
     "recipients" => array(
      "signers" => array(
       array(
        "email" => $applicant_email, 
        "name" => $applicant_name, 
        "clientUserId" => $applicant_unique_id, 
        "recipientId" => $applicant_unique_id, 
        "tabs" => array(
         "signHereTabs" => array(
          array(
           "xPosition" => "100", 
           "yPosition" => "100", 
           "documentId" => $application_unique_id, 
           "pageNumber" => "1" 
          ) 
         ) 
        ) 
       ) 
      ) 
     ) 
    , "status" => "sent" 
    // , "status" => "created" 
); 
$data_string = json_encode($data); 
$file_contents = file_get_contents($application_form_pdf); 

// Create a multi-part request. First the form data, then the file content 
$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=\"$application_form_pdf\"; documentid=".$application_unique_id." \r\n" 
    ."\r\n" 
    ."$file_contents\r\n" 
    ."--myboundary--\r\n" 
    ."\r\n"; 

// Send to the /envelopes end point, which is relative to the baseUrl received above. 
$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); // Do it! 

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

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

//--- display results 
echo "Envelope created! Envelope ID: " . $envelopeId . "\n"; 

////////////////////////////////////////////////////////////////////////////////////// 
// STEP 3 - Get the Embedded Signing View 
////////////////////////////////////////////////////////////////////////////////////// 
$data = array(
     "returnUrl" => "https://www.docusign.com/devcenter" 
    , "authenticationMethod" => "None" 
    , "authenticationInstant" => "None" 
    , "userId" => $applicant_unique_id 
    , "clientUserId" => $applicant_unique_id 
    // , "email" => $applicant_email 
    // , "userName" => $applicant_name 
); 

$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 
echo "Embedded URL is: \n\n" . $url . "\n\nNavigate to this URL to start the embedded signing view of the envelope\n"; 

?> 

任何想法会感激地收到,谢谢!

+0

你正在做的卷曲上传都错了(或者,即使你在技术上做是正确的,我怀疑,你这样做是在一个非常容易出错),不要设置内容长度,curl会为你做,并且不要手动编写普通的HTTP POST multipart/form-data上传请求体,再次让curl为你做,用CURLFile。你也发送了太多的\ r \ n的,因为curl会自动添加HTTP头部和HTTP BODY,所以你现在发送的内容可能是\ r \ n \ r \ r \ n \ r \ n \ r \ n这会破坏请求主体 – hanshenrik

+0

ohhh,你在做这件事是因为Content-disposition中的“documentid”不能用CURLFile设置吗?那会解释它.. – hanshenrik

回答

2

当获得接收者视图(签署视图),尝试通过只指定

"email" => $email, # signer's email 
"userName" => $recipientName, 
"clientUserId" => $clientUserId 

如图中嵌入签名配方识别签名者。

不要设置userId

+0

这样做了!非常感谢!! – bbeckford

相关问题