Skip to content

Commit ad5cace

Browse files
annahiletaAaronWDS
andauthored
Added a document generation code example (#104)
* 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 <anna.hileta@sigma.software> * point to newer deps --------- Co-authored-by: Aaron Wilde <52012222+AaronWDS@users.noreply.github.com> Co-authored-by: AaronWDS <aaron.wilde@docusign.com>
1 parent 87f7a16 commit ad5cace

File tree

8 files changed

+474
-85
lines changed

8 files changed

+474
-85
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"require": {
2323
"docusign/admin-client": "^1.1.0",
2424
"docusign/click-client": "^1.2.2",
25-
"docusign/esign-client": "^6.13.0-rc",
25+
"docusign/esign-client": "^6.14.0-rc",
2626
"docusign/rooms-client": "^2.1.0-rc",
2727
"docusign/monitor-client": "^1.1.0",
2828
"twig/twig": "^3.4.3",

composer.lock

Lines changed: 80 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ds_config_example.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'target_account_id' => false, // Set if you want a specific DocuSign AccountId, If false, the user's default account will be used.
2222
'demo_doc_path' => 'demo_documents',
2323
'doc_docx' => 'World_Wide_Corp_Battle_Plan_Trafalgar.docx',
24+
'offer_doc_docx' => 'Offer_Letter_Demo.docx',
2425
'doc_html' => 'doc_1.html',
2526
'doc_pdf' => 'World_Wide_Corp_lorem.pdf',
2627
'doc_txt' => 'Check_If_Approved.txt',
36.3 KB
Binary file not shown.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Example 001: Use embedded signing
4+
*/
5+
6+
namespace Example\Controllers\Examples\eSignature;
7+
8+
use Example\Controllers\eSignBaseController;
9+
use Example\Services\Examples\eSignature\DocumentGenerationService;
10+
use Example\Services\ManifestService;
11+
12+
class EG042DocumentGeneration extends eSignBaseController
13+
{
14+
const EG = "eg042"; # reference (and url) for this example
15+
const FILE = __FILE__;
16+
17+
/**
18+
* Create a new controller instance.
19+
*
20+
* @return void
21+
*/
22+
public function __construct()
23+
{
24+
parent::__construct();
25+
parent::controller();
26+
}
27+
28+
/**
29+
* 1. Check the token
30+
* 2. Call the worker method
31+
* 3. Redirect the user to the signing
32+
*
33+
* @return void
34+
*/
35+
public function createController(): void
36+
{
37+
$this->checkDsToken();
38+
39+
$envelopeId = DocumentGenerationService::worker(
40+
$this->args,
41+
$this->clientService,
42+
self::DEMO_DOCS_PATH
43+
);
44+
45+
if ($envelopeId) {
46+
$this->clientService->showDoneTemplateFromManifest(
47+
$this->codeExampleText,
48+
null,
49+
ManifestService::replacePlaceholders(
50+
"{0}",
51+
$envelopeId,
52+
$this->codeExampleText["ResultsPageText"]
53+
)
54+
);
55+
}
56+
}
57+
58+
/**
59+
* Get specific template arguments
60+
*
61+
* @return array
62+
*/
63+
public function getTemplateArgs(): array
64+
{
65+
$form_data = [
66+
'candidate_email' => $this->checkInputValues($_POST['candidate_email']),
67+
'candidate_name' => $this->checkInputValues($_POST['candidate_name']),
68+
'manager_name' => $this->checkInputValues($_POST['manager_name']),
69+
'job_title' => $_POST['job_title'],
70+
'salary' => $_POST['salary'],
71+
'start_date' => $_POST['start_date'],
72+
];
73+
74+
return [
75+
'account_id' => $_SESSION['ds_account_id'],
76+
'base_path' => $_SESSION['ds_base_path'],
77+
'ds_access_token' => $_SESSION['ds_access_token'],
78+
'form_data' => $form_data
79+
];
80+
}
81+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
3+
namespace Example\Services\Examples\eSignature;
4+
5+
use DocuSign\eSign\Api\EnvelopesApi\UpdateEnvelopeDocGenFormFieldsOptions;
6+
use DocuSign\eSign\Model\DateSigned;
7+
use DocuSign\eSign\Model\DocGenFormField;
8+
use DocuSign\eSign\Model\TemplateTabs;
9+
use DocuSign\eSign\Model\DocGenFormFieldRequest;
10+
use DocuSign\eSign\Model\DocGenFormFields;
11+
use DocuSign\eSign\Model\Envelope;
12+
use DocuSign\eSign\Model\EnvelopeDefinition;
13+
use DocuSign\eSign\Model\EnvelopeTemplate;
14+
use DocuSign\eSign\Model\Recipients;
15+
use DocuSign\eSign\Model\Signer;
16+
use DocuSign\eSign\Model\SignHere;
17+
use DocuSign\eSign\Model\Tabs;
18+
use DocuSign\eSign\Model\TemplateRole;
19+
use DocuSign\eSign\Model\Document;
20+
21+
class DocumentGenerationService
22+
{
23+
public static function worker(array $args, $clientService, $documentPath): string
24+
{
25+
$templatesApi = $clientService->getTemplatesApi();
26+
27+
$envelopeTemplate = DocumentGenerationService::makeTemplate();
28+
$templatesListResponse = $templatesApi->createTemplate($args['account_id'], $envelopeTemplate);
29+
$templateId = $templatesListResponse['template_id'];
30+
31+
$templatesApi->updateDocument($args['account_id'], "1", $templateId, self::addDocumentTemplate($documentPath));
32+
$templatesApi->createTabs($args['account_id'], "1", $templateId, self::prepareTabs());
33+
34+
$envelopeApi = $clientService->getEnvelopeApi();
35+
$envelopeResponse = $envelopeApi->createEnvelope(
36+
$args['account_id'],
37+
DocumentGenerationService::makeEnvelope($args["form_data"], $templateId)
38+
);
39+
$envelopeId = $envelopeResponse["envelope_id"];
40+
41+
$documents = $envelopeApi->getEnvelopeDocGenFormFields($args['account_id'], $envelopeId);
42+
$documentId = $documents["doc_gen_form_fields"][0]["document_id"];
43+
44+
$formFields = DocumentGenerationService::formFields($args["form_data"], $documentId);
45+
$envelopeApi->updateEnvelopeDocGenFormFields(
46+
$args['account_id'],
47+
$envelopeId,
48+
$formFields
49+
);
50+
51+
$envelopeResponse = $envelopeApi->update(
52+
$args['account_id'],
53+
$envelopeId,
54+
new Envelope([
55+
'status' => 'sent'
56+
])
57+
);
58+
59+
return $envelopeResponse->getEnvelopeId();
60+
}
61+
62+
public static function makeTemplate(): EnvelopeTemplate
63+
{
64+
$signer = new Signer([
65+
'role_name' => 'signer',
66+
'recipient_id' => '1',
67+
'routing_order' => '1',
68+
]);
69+
70+
return new EnvelopeTemplate(
71+
[
72+
'description' => 'Example template created via the API',
73+
'name' => 'Example Template',
74+
'email_subject' => 'Please sign this document',
75+
'recipients' => new Recipients([
76+
'signers' => [$signer]
77+
]),
78+
'status' => 'created',
79+
'shared' => 'false'
80+
]
81+
);
82+
}
83+
84+
public static function prepareTabs(): TemplateTabs
85+
{
86+
$signHere = new SignHere([
87+
'document_id' => '1',
88+
'page_number' => '1',
89+
'x_position' => '75',
90+
'y_position' => '415'
91+
]);
92+
93+
$dateSigned = new DateSigned([
94+
'document_id' => '1',
95+
'page_number' => '1',
96+
'x_position' => '290',
97+
'y_position' => '435'
98+
]);
99+
100+
return new TemplateTabs([
101+
'sign_here_tabs' => [$signHere],
102+
'date_signed_tabs' => [$dateSigned],
103+
]);
104+
}
105+
106+
public static function addDocumentTemplate(string $documentPath): EnvelopeDefinition
107+
{
108+
$documentFile = $GLOBALS['DS_CONFIG']['offer_doc_docx'];
109+
$contentBytes = file_get_contents($documentPath . $documentFile);
110+
$base64FileContent = base64_encode($contentBytes);
111+
112+
$document = new Document([
113+
'document_base64' => $base64FileContent,
114+
'name' => 'OfferLetterDemo.docx',
115+
'file_extension' => 'docx',
116+
'document_id' => '1',
117+
'order' => '1',
118+
'pages' => '1',
119+
]);
120+
121+
return new EnvelopeDefinition([
122+
'documents' => [$document],
123+
]);
124+
}
125+
126+
public static function makeEnvelope(array $args, $templateId): EnvelopeDefinition
127+
{
128+
$signer = new TemplateRole([
129+
'email' => $args['candidate_email'],
130+
'name' => $args['candidate_name'],
131+
'role_name' => "signer"
132+
]);
133+
134+
return new EnvelopeDefinition(
135+
[
136+
'status' => "created",
137+
'template_roles' => [$signer],
138+
"template_id" => $templateId
139+
]
140+
);
141+
}
142+
143+
public static function formFields(array $args, $documentId): DocGenFormFieldRequest
144+
{
145+
return new DocGenFormFieldRequest(
146+
[
147+
'doc_gen_form_fields' => [
148+
new DocGenFormFields([
149+
'document_id' => $documentId,
150+
'doc_gen_form_field_list' => [
151+
new DocGenFormField([
152+
'name' => 'Candidate_Name',
153+
'value' => $args['candidate_name']
154+
]),
155+
new DocGenFormField([
156+
'name' => 'Manager_Name',
157+
'value' => $args['manager_name']
158+
]),
159+
new DocGenFormField([
160+
'name' => 'Job_Title',
161+
'value' => $args['job_title']
162+
]),
163+
new DocGenFormField([
164+
'name' => 'Salary',
165+
'value' => $args['salary']
166+
]),
167+
new DocGenFormField([
168+
'name' => 'Start_Date',
169+
'value' => $args['start_date']
170+
])
171+
]
172+
])
173+
]
174+
]
175+
);
176+
}
177+
}

src/Services/RouterService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class RouterService implements IRouterService
5454
'eg039' => 'eSignature\EG039InPersonSigning',
5555
'eg040' => 'eSignature\EG040SetDocumentsVisibility',
5656
'eg041' => 'eSignature\EG041CFREmbeddedSigning',
57+
'eg042' => 'eSignature\EG042DocumentGeneration',
5758
'reg001' => 'Rooms\EG001CreateRoomWithData',
5859
'reg002' => 'Rooms\EG002CreateRoomWithTemplate',
5960
'reg003' => 'Rooms\EG003ExportDataFromRoom',
@@ -130,6 +131,7 @@ class RouterService implements IRouterService
130131
"eg039" => "esignature/eg039_in_person_signing.html",
131132
"eg040" => "esignature/eg040_set_document_visibility.html",
132133
'eg041' => 'esignature/eg041_cfr_embedded_signing.html',
134+
'eg042' => 'esignature/eg042_document_generation.html',
133135
"reg001" => "rooms/eg001_create_room_with_data.html",
134136
"reg002" => "rooms/eg002_create_room_with_template.html",
135137
"reg003" => "rooms/eg003_export_data_from_room.html",

0 commit comments

Comments
 (0)