Using the Docusign PHP SDK, how do I replace my template base document when creating an envelope?

I have created a template with Docusign API using a base document (a customer contract) the details of which will change when I send out an envelope but the layout will remain the same. I am trying to replace the base document from the template when creating the envelope. I have been informed that I need to use composite templates but when I try to code that I am getting a 500 error.

Here is the code for the envelope definition:

* @param  $args array
 * @return array ['redirect_url']
 * @throws ApiException for API problems and perhaps file access \Exception too.
 */
# ***DS.snippet.0.start
private function worker(array $args): array
{
    # 1. Create the envelope request object
    $envelope_definition = $this->make_envelope($args["envelope_args"]);

    # 2. call Envelopes::create API method
    # Exceptions will be caught by the calling function
    $envelope_api = $this->clientService->getEnvelopeApi();
    $results = $envelope_api->createEnvelope($args['account_id'], $envelope_definition);

    return ['envelope_id' => $results->getEnvelopeId()];
}



/**
 * Creates envelope definition using composite templates
 * Parameters for the envelope: signer_email, signer_name, signer_client_id
 *
 * @param  $args array
 * @return mixed -- returns an envelope definition
 */
private function make_envelope(array $args): EnvelopeDefinition
{
    $signer = new \DocuSign\eSign\Model\Signer([

        'email' => $args['signer_email'], 'name' => $args['signer_name'],

        'role_name' => "signer", 'recipient_id' => "1",

        # Adding clientUserId transforms the template recipient

        # into an embedded recipient:

        'client_user_id' => $args['signer_client_id']

    ]);

    # Create the company signer recipient

    $companySigner = new \DocuSign\eSign\Model\Signer([

        'email' => $args['companySigner_email'], 'name' => $args['companySigner_name'],

        'role_name' => "companySigner", 'recipient_id' =>"2"

    ]);

    # Recipients object:

    $recipients_server_template = new \DocuSign\eSign\Model\Recipients([

        'signers' => [$signer, $companySigner]]);

    # Create a composite template for the Server template + roles

    $comp_template1 = new \DocuSign\eSign\Model\CompositeTemplate([

        'composite_template_id' => "1",

        'server_templates' => [

            new \DocuSign\eSign\Model\ServerTemplate([

                'sequence' => "1", 'template_id' => $args['template_id']])

        ],

        # Add the roles via an inlineTemplate

        'inline_templates' => [

            new \DocuSign\eSign\Model\InlineTemplate([

                'sequence' => "1",

                'recipients' => $recipients_server_template])

        ]

    ]);

    # Create Signer definitions for the added document - using the same tabs from server template
    $signerAddedDoc = new \DocuSign\eSign\Model\Signer([

        'email' => $args['signer_email'],

        'name' => $args['signer_name'],

        'role_name' => "signer", 'recipient_id' => "1",

        'client_user_id' => $args['signer_client_id'],

        'tabs' => $signer_tabs]);

    $companySignerAddedDoc = new \DocuSign\eSign\Model\Signer([

            'email' => $args['companySigner_email'],
    
            'name' => $args['companySigner_name'],
    
            'role_name' => "companySigner", 'recipient_id' => "2",
    
            'client_user_id' => $args['signer_client_id'],
    
            'tabs' => $companySigner_tabs]);

    # The Recipients object for the added document.
    # Using companySigner definition from above.

    $recipients_added_doc = new \DocuSign\eSign\Model\Recipients([

        'signers' => [$signerAddedDoc, $companySignerAddedDoc]]);


    # Create the pdf document that will be added to the envelope
    $doc_file = 'Connect_Customer_Agreement.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' => 'Prepared Connect Customer Agreement',  # 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 a composite template for the added document
    $comp_template2 = new \DocuSign\eSign\Model\CompositeTemplate([

        'composite_template_id' => "2",

        # Add the recipients via an inlineTemplate
        'inline_templates' => [

            new \DocuSign\eSign\Model\InlineTemplate([

                'sequence' => "2", 'recipients' => $recipients_added_doc])

        ],

        'document' => $document]);

    # Create the envelope definition with the composited templates
    $envelope_definition = new \DocuSign\eSign\Model\EnvelopeDefinition([

        'status' => "sent",

        'composite_templates' => [$comp_template1, $comp_template2]

    ]);

    return $envelope_definition;

Welcome to the PHPhelp site !

Here is a link how someone else handled it. It does not match your code, but, might help you.
S.O. create envelope from template

The “500” error can be many different things. Quite often it is just a badly formed argument to a library. Or, it maybe be missing commas, or hundreds of other issues. You did not tell us what line the error was being throw at. Where is it failing?

Thank you - I am so happy to have found this site!

Using tail -f /var/log/httpd/error_log httpd log returns no errors. Also inputting error_reporting(E_ALL); on script runtime returns nothing also.

php.ini file is also updated to error_log ON.

Its a struggle because I am not finding docusign’s documentation easy to work through.

Well, Jaybo, glad you found us, too. I have not used this library, but, reviewing it seems like it is easy enough to use. I have found that “500” errors are usually always calling a function incorrectly, missing library files or other easy to track problems. Also, often it is caused by cross-origin’s of library files. So, you need to debug it.

To debug 500 errors, load the page in either Google-Chrome or Firefox browser. Then, “INSPECT” the page, selecting anywhere on the page. This will open a console at the bottom that gives you 100% info on what is happening when you load the page. I would start with the the CONSOLE and refresh the page. This will show you what files are being loaded when the page is fetched from the server. There are many other tabs you can select and review different items of your active page. This might give you somewhere to start. Good luck and let us know how you progress…

Thanks ErnieAlex will look into it some more!

Sponsor our Newsletter | Privacy Policy | Terms of Service