在DocuSign Envelope中提交表单并预填充自定义字段

时间:2016-08-04 15:13:35

标签: php forms docusignapi

出于某种原因,我无法让我的代码显示我想在左窗格中添加的自定义字段(例如,名字,姓氏等)。在提交时,我的表单应该转到信封并预填充我的自定义字段。我在这里搜索了DocuSign文档以及不同的主题。任何帮助深表感谢。谢谢。

<?php
if(!empty($_POST)){
    // Input your info:
    $email = "foor@bar.com";                                 // your account email
    $password = "password";                              // your account password
    $integratorKey = "wouldnt-you-like-to-know"; // your account integrator key, found on (Preferences -> API page)
    $templateId = "66b7706e-936b-4438-bd5e-bd68ce47dffb";    // provide a valid templateId of a template in your account
    $templateRoleName = "Test";                              // use same role name that exists on the template in the console

    $recipientName = 'blah bleh';                        // provide a recipient (signer) name
    $recipientEmail = 'yee@haw.com';
                                                             // 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:
    $color = $_POST['color'];
    $number = $_POST['number'];
    $animal = $_POST['animal'];

    $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_SSL_VERIFYPEER, false);
    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 "<br>";
        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" . "<br>";

    // STEP 2 - Create an envelope with an Embedded recipient (uses the clientUserId property)
    $data = array(
        "accountId" => $accountId, 
        "emailSubject" => "DocuSign API - Embedded Signing Example",
        "emailBlurb" => "This is a test.",
        "compositeTemplates" => array(
            "serverTemplates" => array(
                "sequence" => "1",
                "templateId" => $templateId
            ),
            "inlineTemplates" => array(
                "sequence" => "2",
                "recipients" => array(
                    "signers" => array(
                        "roleName" => "Signer1",
                        "recipientId" => "1",
                        "name" => "John Doe",
                        "email" => "johndoe@test.com",
                        "clientUserId" => "1234",
                        "tabs" => array(
                            "textTabs" => array(
                                "tabLabel" => "address",
                                "value" => "123 Main Street"
                            )
                        )
                    )
                )
            )
        ),
        "status" => "sent"
    );    

    $data_string = json_encode($data);  
    $curl = curl_init($baseUrl . "/envelopes" );
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    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 --> <br>";
        print_r($json_response); echo "\n";
        exit(-1);
    }

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

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

    // STEP 3 - Get the Embedded Signing View 
    $data = array(
        "returnUrl" => "http://www.docusign.com/devcenter",
        "authenticationMethod" => "Email", 
        "clientUserId" => "1234",
        "userName" => "John Doe",
        "email" => "johnDoe@test.com"
    );   

    $data_string = json_encode($data);    
    $curl = curl_init($baseUrl . "/envelopes/$envelopeId/views/recipient" );
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    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 "\n\nNavigate to this URL to start the embedded signing view of the envelope\n" . "<br>Embedded URL is: \n\n" . "<a href='$url'>HERE!</a>"; 
}
?>
<form action="" method="POST">
<label for="color">color:</label>
<input type="text" name="color">
<br>
<label for="number">number:</label>
<input type="text" name="number">
<br>
<label for="animal">animal:</label>
<input type="text" name="animal">
<br>
<button type="submit">Submit</button>
</form>    

1 个答案:

答案 0 :(得分:2)

要使用模板创建信封并在该信封文档中预先填充字段,则需要在API请求中使用复合模板结构。 (有关复合模板的信息,请参阅this page上的“复合模板”部分。)

我不熟悉DocuSign PHP SDK,但会解释JSON中的请求语法,我想你可以找出相应的PHP语法来生成请求,如图所示。

以下示例请求(JSON)创建一个信封,该信封将指定的模板与单个收件人(角色名称= Signer1 )一起使用,并预先填充地址字段该收件人(嵌入式签名者)的值为“123 Main Street”。 (虽然此示例仅预填充 Signer1 的单个字段 - 但您可以通过简单地将其包含在请求的tabs对象中以及地址。)

POST https://{{env}}.docusign.net/restapi//v2/accounts/{{accountId}}/envelopes

{
    "emailSubject": "Test Pre-fill Tabs",
    "emailBlurb": "This is a test.",
    "compositeTemplates": [{
        "serverTemplates": [{
            "sequence": "1",
            "templateId": "CD0E6D53-3447-4A9E-BBAF-0EB2C78E8310"
        }],
        "inlineTemplates":[{
            "sequence": "2",
            "recipients": {
                "signers": [
                    {
                        "roleName": "Signer1",
                        "recipientId": "1",
                        "name": "John Doe",
                        "email": "johndoe@test.com",
                        "clientUserId": "1234",
                        "tabs": {
                            "textTabs": [
                                {
                                    "tabLabel": "address",
                                    "value": "123 Main Street"
                                }
                            ]
                        }
                    }
                ]
            }
        }]
    }],
    "status": "sent"
}

创建信封后(使用上述请求),您将执行“POST收件人视图”请求以获取 Signer1 的签名URL:

POST https://{{env}}.docusign.net/restapi//v2/accounts/{{accountId}}/envelopes/{{envelopeId/views/recipient

{
    "clientUserId": "1234",
    "userName": "John Doe",
    "email": "johndoe@test.com",
    "returnUrl": "https://www.google.com",
    "authenticationMethod": "Email"
}

(请注意,您未在此请求中指定标签。)

更新#1

从您添加到原始帖子的代码开始,我已经能够对其进行修改,使其现在成功创建一个信封(使用模板),并使用一个嵌入式收件人(< strong> Signer1 )并预先填充该签名者的地址文本字段。这是代码 - 请注意,您需要在此代码示例的顶部指定所有变量的值。

<?php

// Set values for variables
//-----------
$email = "YOUR_DOCUSIGN_LOGIN_EMAIL";                                 // your account email
$password = "YOUR_DOCUSIGN_LOGIN_PASSWORD";                              // your account password
$integratorKey = "YOUR_DOCUSIGN_INTEGRATOR_KEY"; // your account integrator key, found on (Preferences -> API page)

$templateId = "TEMPLATE_ID";    // provide a valid templateId of a template in your account
$templateRoleName = "TEMPLATE_RECIPIENT_ROLE_NAME";                              // use same role name that exists on the template in the console

$recipientName = "SIGNER_NAME";                        // provide a recipient (signer) name
$recipientEmail = "SIGNER_EMAIL_ADDRESS";            // provide a recipient (signer) email address
$recipientId = "1";                                 // set recipient id (can be any integer value)
$clientUserId = "1234";                             // set clientUserId (can be any integer value) -- this is what makes the recipient an "embedded recipient (signer)"

$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_SSL_VERIFYPEER, false);
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 "\n";
    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\n";



// STEP 2 - Create an envelope with an Embedded recipient (uses the clientUserId property)
//-----------
// tabs
$textTabs = array();
$textTabs[] = array('tabLabel' => "address", 'value' => "123 Main Street");             
$tabs = array('textTabs' => $textTabs);
#echo ("tabs:\n" . json_encode($tabs) . "\n\n");

// recipients 
$signers = array();
$signers[] = array('roleName' => $templateRoleName, 'recipientId' => $recipientId, 'name' => $recipientName, 'email' => $recipientEmail, 'clientUserId' => $clientUserId, 'tabs' => $tabs);
$recipients = array('signers' => $signers);
#echo ("recipients:\n" . json_encode($recipients) . "\n\n");

// serverTemplates
$serverTemplates = array();
$serverTemplates[] = array('sequence' => "1", 'templateId' => $templateId);
#echo ("serverTemplates:\n " . json_encode($serverTemplates) . "\n\n");

// inlineTemplates
$inlineTemplates = array();
$inlineTemplates[] = array('sequence' => "2", 'recipients' => $recipients);
#echo ("inlineTemplates:\n" . json_encode($inlineTemplates) . "\n\n");

// compositeTemplates
$compositeTemplates = array();
$compositeTemplates[] = array('serverTemplates' => $serverTemplates ,'inlineTemplates' => $inlineTemplates);
#echo ("compositeTemplates:\n" . json_encode($compositeTemplates) . "\n\n");

// request body 
$data = array('emailSubject' => "DocuSign Test - Embedded Signing", 'emailBlurb' => "Please sign. Thanks!", 'status' => 'sent', 'compositeTemplates' => $compositeTemplates);
$data_string = json_encode($data);  

$curl = curl_init($baseUrl . "/envelopes" );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
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 --> \n";
    print_r($json_response); echo "\n";
    exit(-1);
}

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

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



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

$data_string = json_encode($data);    
$curl = curl_init($baseUrl . "/envelopes/$envelopeId/views/recipient" );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
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 "\n\nNavigate to this URL to start the embedded signing view of the envelope:\n\n" . $url . "\n\n"; 

?>

免责声明:虽然这段代码功能齐全并且实现了目标,但这是我第一次尝试使用PHP,因此编写此代码的方式可能更好(更有效)。如果有任何PHP专家如此倾向,我欢迎提供反馈。

相关问题