Search code examples
laravel-5.3docusignapidocusigncompositetmplts

Docusign create envelope and apply template to newly added document Laravel 5.5


Below is how i am sending doc for sign i need to run AutoPlace on this attached doc but iam not able to make it work. This code is working but not placing autoplace that i had set in this template.

$client = new DocuSign\Rest\Client([
                'username'       => config("docusign.DOCUSIGN_USERNAME"),
                'password'       => config("docusign.DOCUSIGN_PASSWORD"),
                'integrator_key' => config("docusign.DOCUSIGN_INTEGRATOR_KEY"),
            ]);

            $templateRole = $client->templateRole([
                'email'     => <email>,
                'name'      => <name>,
                'role_name' => 'Client',
            ]);

            $envelopeDefinition = $client->envelopeDefinition([
                'status'         => 'sent',
                'email_subject'  => 'Signature Required On Your Order ',
                'template_id'    => '<docusign template id>',
                'template_roles' => [
                    $templateRole,
                ],
            ]);
            $doc = Storage::disk('local')->url('mypdf.pdf');

            $envelopeOptions = $client->envelopes->createEnvelopeOptions([
                'documents' => [
                    'documentBase64' => base64_encode($doc),
                    'name'           => 'mypdf.pdf',
                ],
            ]);
            $envelopeSummary = $client->envelopes->createEnvelope($envelopeDefinition, $envelopeOptions);
            print_R($envelopeSummary);
            die();

Can you please suggest me how can i add that code.. Thanks in advance.


Solution

  • You are using wrong code pattern. With your code you can either apply template or you can add document to the envelope but you will not be able to apply template to the document. To apply a template to the document, you need to use Composite Template. If you have anchor strings (autoplace) present in both the server template and the added document then with composite template you will replace the server template document with the added document. An example would be like below. Server Template - 1afc0348-e853-4a0c-92db-06101168eb4d has a document with the anchorstring (autoplace), and FileName - "Added Document Agreement" in the "document" node has new document on which autoplace anchor string needs to be applied from the server template. Below code will show how to achieve this using composite template.

    {
       "compositeTemplates": [
          {
             "document": {
                  "documentBase64": "<Base64>",
                  "documentId": "1",
                  "fileExtension": "pdf",
                  "name": "Added Document Agreement"
             },
             "inlineTemplates": [
                {
                   "recipients": {
                      "signers": [
                         {
                            "email": "[email protected]",
                            "name": "John Doe",
                            "recipientId": "1",
                            "roleName": "InternalSigner",
                            "routingOrder": "1"
                         }
                      ]
                   },
                   "sequence": "2"
                }
             ],
             "serverTemplates": [
                {
                   "sequence": "1",
                   "templateId": "1afc0348-e853-4a0c-92db-06101168eb4d"
                }
             ]
          }
       ],
       "status": "sent"
    }