Issue
I want to create templates for document signing using docusign in PHP. It seems their API is not fully detail.
Docusign for template creation
I have followed their instructions and have obtained my access token, base_url etc via cURL in PHP.
Here is the PHP code they provide without further details. No PHP SDK and how to use it. I couldn't find were to use my access_token, base_url etc as per documentation.
  private function make_template_req(): EnvelopeTemplate
    {
        $doc_file = 'World_Wide_Corp_fields.pdf';
        $content_bytes = file_get_contents(self::DEMO_DOCS_PATH . $doc_file);
        $base64_file_content = base64_encode($content_bytes);
        # Create the document model
        $document = new Document([  # create the DocuSign document object
            'document_base64' => $base64_file_content,
            'name' => 'Lorem Ipsum',  # can be different from actual file name
            'file_extension' => 'pdf',  # many different document types are accepted
            'document_id' => '1'  # a label used to reference the doc
        ]);
        # Create the signer recipient model
        # Since these are role definitions, no name/email:
        $signer = new Signer([
            'role_name' => 'signer', 'recipient_id' => "1", 'routing_order' => "1"]);
        # create a cc recipient to receive a copy of the documents
        $cc = new CarbonCopy([
            'role_name' => 'cc', 'recipient_id' => "2", 'routing_order' => "2"]);
        # Create fields using absolute positioning
        # Create a sign_here tab (field on the document)
        $sign_here = new SignHere(['document_id' => '1', 'page_number' => '1',
            'x_position' => '191', 'y_position' => '148']);
        $check1 = new Checkbox(['document_id' => '1', 'page_number' => '1',
            'x_position' => '75', 'y_position' => '417', 'tab_label' => 'ckAuthorization']);
        $check2 = new Checkbox(['document_id' => '1', 'page_number' => '1',
            'x_position' => '75', 'y_position' => '447', 'tab_label' => 'ckAuthentication']);
        $check3 = new Checkbox(['document_id' => '1', 'page_number' => '1',
            'x_position' => '75', 'y_position' => '478', 'tab_label' => 'ckAgreement']);
        $check4 = new Checkbox(['document_id' => '1', 'page_number' => '1',
            'x_position' => '75', 'y_position' => '508', 'tab_label' => 'ckAcknowledgement']);
        $list1 = new ModelList([
            'font' => "helvetica",
            'font_size' => "size11",
            'anchor_string' => '/l1q/',
            'anchor_y_offset' => '-10', 'anchor_units' => 'pixels',
            'anchor_x_offset' => '0',
            'list_items' => [
                    ['text' => "Red"   , 'value' => "red"   ], ['text' => "Orange", 'value' => "orange"],
                    ['text' => "Yellow", 'value' => "yellow"], ['text' => "Green" , 'value' => "green" ],
                    ['text' => "Blue"  , 'value' => "blue"  ], ['text' => "Indigo", 'value' => "indigo"]
                ],
            'required' => "true",
            'tab_label' => "l1q"
        ]);
        $number1 = new Number(['document_id' => "1", 'page_number' => "1",
            'x_position' => "163", 'y_position' => "260",
            'font' => "helvetica", 'font_size' => "size14", 'tab_label' => "numbersOnly",
            'width' => "84", 'required' => "false"]);
        $radio_group = new RadioGroup(['document_id' => "1", 'group_name' => "radio1",
            'radios' => [
                new Radio(['page_number' => "1", 'x_position' => "142", 'y_position' => "384",
                    'value' => "white", 'required' => "false"]),
                new Radio(['page_number' => "1", 'x_position' => "74", 'y_position' => "384",
                    'value' => "red", 'required' => "false"]),
                new Radio(['page_number' => "1", 'x_position' => "220", 'y_position' => "384",
                    'value' => "blue", 'required' => "false"])
            ]]);
        $text = new Text(['document_id' => "1", 'page_number' => "1",
            'x_position' => "153", 'y_position' => "230",
            'font' => "helvetica", 'font_size' => "size14", 'tab_label' => "text",
            'height' => "23", 'width' => "84", 'required' => "false"]);
        # Add the tabs model to the signer
        # The Tabs object wants arrays of the different field/tab types
        $signer->setTabs(new Tabs(['sign_here_tabs' => [$sign_here],
            'checkbox_tabs' => [$check1, $check2, $check3, $check4], 'list_tabs' => [$list1],
            'number_tabs' => [$number1], 'radio_group_tabs' => [$radio_group], 'text_tabs' => [$text]
        ]));
        # Template object:
        $template_request = new EnvelopeTemplate([
            'description' => "Example template created via the API",
            'name' => $this->template_name,
            'shared' => "false",
            'documents' => [$document], 'email_subject' => "Please sign this document",
            'recipients' => new Recipients([
            'signers' => [$signer], 'carbon_copies' => [$cc]]),
            'status' => "created"
        ]);
        return $template_request;
    }
$results = $templates_api->createTemplate($args['account_id'], $template_req_object);
I decided to use cURL but they provide Bash code in Step 2 (Create Template) and cURL code in Step 3 (Call the eSignature REST API) which is confusing.
I decide to combine Bash Code and Curl code in php to see if it will work but it throws error.
$access_token='access_token goes here';
$base_uri='my base url goes here';
$account_id ='my account id goes here';
$data_param=
'{
    "description": "Example template created via the API",
    "name": "Example Signer and CC template",
    "shared": "false",
    "documents": [
        {
            
           "documentBase64": "' > $request_data
            cat $doc1_base64 >> $request_data
            printf '",
            "documentId": "1", "fileExtension": "pdf",
            "name": "Lorem Ipsum"
        }
    ],
    "emailSubject": "Please sign this document",
   "recipients": {
        "carbonCopies": [
            {"recipientId": "2", "roleName": "cc", "routingOrder": "2"}
        ],
        "signers": [
            {
                "recipientId": "1", "roleName": "signer", "routingOrder": "1",
                "tabs": {
                    "checkboxTabs": [
                        {
                            "documentId": "1", "pageNumber": "1",
                            "tabLabel": "ckAuthorization", "xPosition": "75",
                            "yPosition": "417"
                        },
                        {
                            "documentId": "1", "pageNumber": "1",
                            "tabLabel": "ckAuthentication", "xPosition": "75",
                            "yPosition": "447"
                        },
                        {
                            "documentId": "1", "pageNumber": "1",
                            "tabLabel": "ckAgreement", "xPosition": "75",
                            "yPosition": "478"
                        },
                        {
                            "documentId": "1", "pageNumber": "1",
                            "tabLabel": "ckAcknowledgement", "xPosition": "75",
                            "yPosition": "508"
                        }
                    ],
                    "listTabs": [
                        {
                            "documentId": "1", "font": "helvetica", 
                            "fontSize": "size14",
                            "listItems": [
                                {"text": "Red", "value": "red"},
                                {"text": "Orange", "value": "orange"},
                                {"text": "Yellow", "value": "yellow"},
                                {"text": "Green", "value": "green"},
                                {"text": "Blue", "value": "blue"},
                                {"text": "Indigo", "value": "indigo"},
                                {"text": "Violet", "value": "violet"}
                            ],
                            "pageNumber": "1", "required": "false", 
                            "tabLabel": "list", "xPosition": "142",
                            "yPosition": "291"
                        }
                    ],
                    "radioGroupTabs": [
                        {
                            "documentId": "1", "groupName": "radio1",
                            "radios": [
                                {
                                    "pageNumber": "1", "required": "false",
                                    "value": "white", "xPosition": "142",
                                    "yPosition": "384"
                                },
                                {
                                    "pageNumber": "1", "required": "false",
                                    "value": "red", "xPosition": "74",
                                    "yPosition": "384"
                                },
                                {
                                    "pageNumber": "1", "required": "false",
                                    "value": "blue", "xPosition": "220",
                                    "yPosition": "384"
                                }
                            ]
                        }
                    ],
                    "signHereTabs": [
                        {
                            "documentId": "1", "pageNumber": "1",
                            "xPosition": "191", "yPosition": "148"
                        }
                    ],
                    "textTabs": [
                        {
                            "documentId": "1", "font": "helvetica",
                            "fontSize": "size14", "height": 23, 
                            "pageNumber": "1", "required": "false",
                            "tabLabel": "text", "width": 84,
                            "xPosition": "153", "yPosition": "230"
                        },
                        {
                            "documentId": "1", "font": "helvetica",
                            "fontSize": "size14", "height": 23,
                            "pageNumber": "1", "required": "false",
                            "tabLabel": "numbersOnly", "width": 84,
                            "xPosition": "153", "yPosition": "260"
                        }
                    ]
                }
            }
        ]
    },
    "status": "created"
}';
$url ="$base_uri/v2.1/accounts/$account_id/templates";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', "Authorization: Bearer $access_token"));  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_param);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch); 
echo $output;
Updated part of the code
After installing PHP SDK and run my sample code below, it throws error
<?php
require_once('vendor/autoload.php');
$access_token='my token goes here';
$base_path='https://demo.docusign.net';
$account_id ='my account id goes here';
// You will need to obtain an access token using your chosen authentication flow 
$api_client = new \DocuSign\eSign\client\ApiClient($base_path);
$config = new \DocuSign\eSign\Model\Configuration($api_client);
$config->addDefaultHeader('Authorization', 'Bearer ' + $access_token);
$users_api = new \DocuSign\eSign\Api\UsersApi($api_client);
?>
Error:
Fatal error: Uncaught TypeError: DocuSign\eSign\Client\ApiClient::__construct(): Argument #1 ($config) must be of type ?DocuSign\eSign\Configuration, string given, called in C:\xampp\htdocs\docusign-esign\index.php on line 9 and defined in C:\xampp\htdocs\docusign-esign\vendor\docusign\esign-client\src\Client\ApiClient.php:91 Stack trace: #0 C:\xampp\htdocs\docusign-esign\index.php(9): DocuSign\eSign\Client\ApiClient->__construct('https://demo.do...') #1 {main} thrown in C:\xampp\htdocs\docusign-esign\vendor\docusign\esign-client\src\Client\ApiClient.php on line 91
When I run the entire code as per below, it throws error
<?php
require_once('vendor/autoload.php');
$access_token='my access token';
$base_path='https://demo.docusign.net';
$account_id ='my account id goes here';
private function make_template_req(): EnvelopeTemplate
    {
// You will need to obtain an access token using your chosen authentication flow 
$api_client = new \DocuSign\eSign\client\ApiClient($base_path);
$config = new \DocuSign\eSign\Model\Configuration($api_client);
$config->addDefaultHeader('Authorization', 'Bearer ' + $access_token);
$users_api = new \DocuSign\eSign\Api\UsersApi($api_client);
        $doc_file = 'World_Wide_Corp_fields.pdf';
        $content_bytes = file_get_contents(self::DEMO_DOCS_PATH . $doc_file);
        $base64_file_content = base64_encode($content_bytes);
        # Create the document model
        $document = new Document([  # create the DocuSign document object
            'document_base64' => $base64_file_content,
            'name' => 'Lorem Ipsum',  # can be different from actual file name
            'file_extension' => 'pdf',  # many different document types are accepted
            'document_id' => '1'  # a label used to reference the doc
        ]);
        # Create the signer recipient model
        # Since these are role definitions, no name/email:
        $signer = new Signer([
            'role_name' => 'signer', 'recipient_id' => "1", 'routing_order' => "1"]);
        # create a cc recipient to receive a copy of the documents
        $cc = new CarbonCopy([
            'role_name' => 'cc', 'recipient_id' => "2", 'routing_order' => "2"]);
        # Create fields using absolute positioning
        # Create a sign_here tab (field on the document)
        $sign_here = new SignHere(['document_id' => '1', 'page_number' => '1',
            'x_position' => '191', 'y_position' => '148']);
        $check1 = new Checkbox(['document_id' => '1', 'page_number' => '1',
            'x_position' => '75', 'y_position' => '417', 'tab_label' => 'ckAuthorization']);
        $check2 = new Checkbox(['document_id' => '1', 'page_number' => '1',
            'x_position' => '75', 'y_position' => '447', 'tab_label' => 'ckAuthentication']);
        $check3 = new Checkbox(['document_id' => '1', 'page_number' => '1',
            'x_position' => '75', 'y_position' => '478', 'tab_label' => 'ckAgreement']);
        $check4 = new Checkbox(['document_id' => '1', 'page_number' => '1',
            'x_position' => '75', 'y_position' => '508', 'tab_label' => 'ckAcknowledgement']);
        $list1 = new ModelList([
            'font' => "helvetica",
            'font_size' => "size11",
            'anchor_string' => '/l1q/',
            'anchor_y_offset' => '-10', 'anchor_units' => 'pixels',
            'anchor_x_offset' => '0',
            'list_items' => [
                    ['text' => "Red"   , 'value' => "red"   ], ['text' => "Orange", 'value' => "orange"],
                    ['text' => "Yellow", 'value' => "yellow"], ['text' => "Green" , 'value' => "green" ],
                    ['text' => "Blue"  , 'value' => "blue"  ], ['text' => "Indigo", 'value' => "indigo"]
                ],
            'required' => "true",
            'tab_label' => "l1q"
        ]);
        $number1 = new Number(['document_id' => "1", 'page_number' => "1",
            'x_position' => "163", 'y_position' => "260",
            'font' => "helvetica", 'font_size' => "size14", 'tab_label' => "numbersOnly",
            'width' => "84", 'required' => "false"]);
        $radio_group = new RadioGroup(['document_id' => "1", 'group_name' => "radio1",
            'radios' => [
                new Radio(['page_number' => "1", 'x_position' => "142", 'y_position' => "384",
                    'value' => "white", 'required' => "false"]),
                new Radio(['page_number' => "1", 'x_position' => "74", 'y_position' => "384",
                    'value' => "red", 'required' => "false"]),
                new Radio(['page_number' => "1", 'x_position' => "220", 'y_position' => "384",
                    'value' => "blue", 'required' => "false"])
            ]]);
        $text = new Text(['document_id' => "1", 'page_number' => "1",
            'x_position' => "153", 'y_position' => "230",
            'font' => "helvetica", 'font_size' => "size14", 'tab_label' => "text",
            'height' => "23", 'width' => "84", 'required' => "false"]);
        # Add the tabs model to the signer
        # The Tabs object wants arrays of the different field/tab types
        $signer->setTabs(new Tabs(['sign_here_tabs' => [$sign_here],
            'checkbox_tabs' => [$check1, $check2, $check3, $check4], 'list_tabs' => [$list1],
            'number_tabs' => [$number1], 'radio_group_tabs' => [$radio_group], 'text_tabs' => [$text]
        ]));
        # Template object:
        $template_request = new EnvelopeTemplate([
            'description' => "Example template created via the API",
            'name' => $this->template_name,
            'shared' => "false",
            'documents' => [$document], 'email_subject' => "Please sign this document",
            'recipients' => new Recipients([
            'signers' => [$signer], 'carbon_copies' => [$cc]]),
            'status' => "created"
        ]);
        return $template_request;
    }
$results = $templates_api->createTemplate($args['account_id'], $template_req_object);
?>
Error:
Parse error: syntax error, unexpected token "private", expecting end of file in C:\xampp\htdocs\docusign-esign\index.php on line 9
Solution
I couldn't find were to use my access_token, base_url etc as per documentation
Here is how to use $access_token and $base_path, assuming you already obtained it using OAuth.
# You will need to obtain an access token using your chosen authentication flow 
$api_client = new \DocuSign\eSign\client\ApiClient($base_path);
$config = new \DocuSign\eSign\Model\Configuration($api_client);
$config->addDefaultHeader('Authorization', 'Bearer ' + $access_token);
$users_api = new \DocuSign\eSign\Api\UsersApi($api_client);
                        
                        Answered By - Inbar Gazit Answer Checked By - Clifford M. (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.