Skip to content

Commit

Permalink
Added a document generation code example (#104)
Browse files Browse the repository at this point in the history
* added a document generation code example

* added empty line before return

* updated the code flow based on the latest requirements

* Feature/adding search bar (#108)

* added loading of the data from the manifest and mapping for code examples pages

* added mapping for texts on the homepages and code example pages

* Added getting the manifest in the Quick_ACG project

* removed leftover comments

* Added support of the common texts and notes

* Added form inputs and redirects

* fixes after testing

* fixes after code review

* moved str_replace to service

* fixed the link to example 28 to example 30

* added a search bar feature

* added a dynamic switch of the API types

* moved must_authenticate value to a constant

* renamed the constant

* added a double check of scopes for examples that have API calls in constructor

* removing extra redirect with quickstart is true

* removed extra redirect if quickstart is false

* changed the language for SkipForLanguages in js file

* fixing empty homepage if the user has CFR account enabled

---------

Co-authored-by: annahileta <[email protected]>

* point to newer deps

---------

Co-authored-by: Aaron Wilde <[email protected]>
Co-authored-by: AaronWDS <[email protected]>
  • Loading branch information
3 people authored Apr 22, 2023
1 parent 87f7a16 commit ad5cace
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 85 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require": {
"docusign/admin-client": "^1.1.0",
"docusign/click-client": "^1.2.2",
"docusign/esign-client": "^6.13.0-rc",
"docusign/esign-client": "^6.14.0-rc",
"docusign/rooms-client": "^2.1.0-rc",
"docusign/monitor-client": "^1.1.0",
"twig/twig": "^3.4.3",
Expand Down
164 changes: 80 additions & 84 deletions composer.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ds_config_example.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'target_account_id' => false, // Set if you want a specific DocuSign AccountId, If false, the user's default account will be used.
'demo_doc_path' => 'demo_documents',
'doc_docx' => 'World_Wide_Corp_Battle_Plan_Trafalgar.docx',
'offer_doc_docx' => 'Offer_Letter_Demo.docx',
'doc_html' => 'doc_1.html',
'doc_pdf' => 'World_Wide_Corp_lorem.pdf',
'doc_txt' => 'Check_If_Approved.txt',
Expand Down
Binary file added public/demo_documents/Offer_Letter_Demo.docx
Binary file not shown.
81 changes: 81 additions & 0 deletions src/Controllers/Examples/eSignature/EG042DocumentGeneration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Example 001: Use embedded signing
*/

namespace Example\Controllers\Examples\eSignature;

use Example\Controllers\eSignBaseController;
use Example\Services\Examples\eSignature\DocumentGenerationService;
use Example\Services\ManifestService;

class EG042DocumentGeneration extends eSignBaseController
{
const EG = "eg042"; # reference (and url) for this example
const FILE = __FILE__;

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
parent::controller();
}

/**
* 1. Check the token
* 2. Call the worker method
* 3. Redirect the user to the signing
*
* @return void
*/
public function createController(): void
{
$this->checkDsToken();

$envelopeId = DocumentGenerationService::worker(
$this->args,
$this->clientService,
self::DEMO_DOCS_PATH
);

if ($envelopeId) {
$this->clientService->showDoneTemplateFromManifest(
$this->codeExampleText,
null,
ManifestService::replacePlaceholders(
"{0}",
$envelopeId,
$this->codeExampleText["ResultsPageText"]
)
);
}
}

/**
* Get specific template arguments
*
* @return array
*/
public function getTemplateArgs(): array
{
$form_data = [
'candidate_email' => $this->checkInputValues($_POST['candidate_email']),
'candidate_name' => $this->checkInputValues($_POST['candidate_name']),
'manager_name' => $this->checkInputValues($_POST['manager_name']),
'job_title' => $_POST['job_title'],
'salary' => $_POST['salary'],
'start_date' => $_POST['start_date'],
];

return [
'account_id' => $_SESSION['ds_account_id'],
'base_path' => $_SESSION['ds_base_path'],
'ds_access_token' => $_SESSION['ds_access_token'],
'form_data' => $form_data
];
}
}
177 changes: 177 additions & 0 deletions src/Services/Examples/eSignature/DocumentGenerationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace Example\Services\Examples\eSignature;

use DocuSign\eSign\Api\EnvelopesApi\UpdateEnvelopeDocGenFormFieldsOptions;
use DocuSign\eSign\Model\DateSigned;
use DocuSign\eSign\Model\DocGenFormField;
use DocuSign\eSign\Model\TemplateTabs;
use DocuSign\eSign\Model\DocGenFormFieldRequest;
use DocuSign\eSign\Model\DocGenFormFields;
use DocuSign\eSign\Model\Envelope;
use DocuSign\eSign\Model\EnvelopeDefinition;
use DocuSign\eSign\Model\EnvelopeTemplate;
use DocuSign\eSign\Model\Recipients;
use DocuSign\eSign\Model\Signer;
use DocuSign\eSign\Model\SignHere;
use DocuSign\eSign\Model\Tabs;
use DocuSign\eSign\Model\TemplateRole;
use DocuSign\eSign\Model\Document;

class DocumentGenerationService
{
public static function worker(array $args, $clientService, $documentPath): string
{
$templatesApi = $clientService->getTemplatesApi();

$envelopeTemplate = DocumentGenerationService::makeTemplate();
$templatesListResponse = $templatesApi->createTemplate($args['account_id'], $envelopeTemplate);
$templateId = $templatesListResponse['template_id'];

$templatesApi->updateDocument($args['account_id'], "1", $templateId, self::addDocumentTemplate($documentPath));
$templatesApi->createTabs($args['account_id'], "1", $templateId, self::prepareTabs());

$envelopeApi = $clientService->getEnvelopeApi();
$envelopeResponse = $envelopeApi->createEnvelope(
$args['account_id'],
DocumentGenerationService::makeEnvelope($args["form_data"], $templateId)
);
$envelopeId = $envelopeResponse["envelope_id"];

$documents = $envelopeApi->getEnvelopeDocGenFormFields($args['account_id'], $envelopeId);
$documentId = $documents["doc_gen_form_fields"][0]["document_id"];

$formFields = DocumentGenerationService::formFields($args["form_data"], $documentId);
$envelopeApi->updateEnvelopeDocGenFormFields(
$args['account_id'],
$envelopeId,
$formFields
);

$envelopeResponse = $envelopeApi->update(
$args['account_id'],
$envelopeId,
new Envelope([
'status' => 'sent'
])
);

return $envelopeResponse->getEnvelopeId();
}

public static function makeTemplate(): EnvelopeTemplate
{
$signer = new Signer([
'role_name' => 'signer',
'recipient_id' => '1',
'routing_order' => '1',
]);

return new EnvelopeTemplate(
[
'description' => 'Example template created via the API',
'name' => 'Example Template',
'email_subject' => 'Please sign this document',
'recipients' => new Recipients([
'signers' => [$signer]
]),
'status' => 'created',
'shared' => 'false'
]
);
}

public static function prepareTabs(): TemplateTabs
{
$signHere = new SignHere([
'document_id' => '1',
'page_number' => '1',
'x_position' => '75',
'y_position' => '415'
]);

$dateSigned = new DateSigned([
'document_id' => '1',
'page_number' => '1',
'x_position' => '290',
'y_position' => '435'
]);

return new TemplateTabs([
'sign_here_tabs' => [$signHere],
'date_signed_tabs' => [$dateSigned],
]);
}

public static function addDocumentTemplate(string $documentPath): EnvelopeDefinition
{
$documentFile = $GLOBALS['DS_CONFIG']['offer_doc_docx'];
$contentBytes = file_get_contents($documentPath . $documentFile);
$base64FileContent = base64_encode($contentBytes);

$document = new Document([
'document_base64' => $base64FileContent,
'name' => 'OfferLetterDemo.docx',
'file_extension' => 'docx',
'document_id' => '1',
'order' => '1',
'pages' => '1',
]);

return new EnvelopeDefinition([
'documents' => [$document],
]);
}

public static function makeEnvelope(array $args, $templateId): EnvelopeDefinition
{
$signer = new TemplateRole([
'email' => $args['candidate_email'],
'name' => $args['candidate_name'],
'role_name' => "signer"
]);

return new EnvelopeDefinition(
[
'status' => "created",
'template_roles' => [$signer],
"template_id" => $templateId
]
);
}

public static function formFields(array $args, $documentId): DocGenFormFieldRequest
{
return new DocGenFormFieldRequest(
[
'doc_gen_form_fields' => [
new DocGenFormFields([
'document_id' => $documentId,
'doc_gen_form_field_list' => [
new DocGenFormField([
'name' => 'Candidate_Name',
'value' => $args['candidate_name']
]),
new DocGenFormField([
'name' => 'Manager_Name',
'value' => $args['manager_name']
]),
new DocGenFormField([
'name' => 'Job_Title',
'value' => $args['job_title']
]),
new DocGenFormField([
'name' => 'Salary',
'value' => $args['salary']
]),
new DocGenFormField([
'name' => 'Start_Date',
'value' => $args['start_date']
])
]
])
]
]
);
}
}
2 changes: 2 additions & 0 deletions src/Services/RouterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class RouterService implements IRouterService
'eg039' => 'eSignature\EG039InPersonSigning',
'eg040' => 'eSignature\EG040SetDocumentsVisibility',
'eg041' => 'eSignature\EG041CFREmbeddedSigning',
'eg042' => 'eSignature\EG042DocumentGeneration',
'reg001' => 'Rooms\EG001CreateRoomWithData',
'reg002' => 'Rooms\EG002CreateRoomWithTemplate',
'reg003' => 'Rooms\EG003ExportDataFromRoom',
Expand Down Expand Up @@ -130,6 +131,7 @@ class RouterService implements IRouterService
"eg039" => "esignature/eg039_in_person_signing.html",
"eg040" => "esignature/eg040_set_document_visibility.html",
'eg041' => 'esignature/eg041_cfr_embedded_signing.html',
'eg042' => 'esignature/eg042_document_generation.html',
"reg001" => "rooms/eg001_create_room_with_data.html",
"reg002" => "rooms/eg002_create_room_with_template.html",
"reg003" => "rooms/eg003_export_data_from_room.html",
Expand Down
Loading

0 comments on commit ad5cace

Please sign in to comment.