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;
