-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> * point to newer deps --------- Co-authored-by: Aaron Wilde <[email protected]> Co-authored-by: AaronWDS <[email protected]>
- Loading branch information
1 parent
87f7a16
commit ad5cace
Showing
8 changed files
with
474 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
81 changes: 81 additions & 0 deletions
81
src/Controllers/Examples/eSignature/EG042DocumentGeneration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
177
src/Services/Examples/eSignature/DocumentGenerationService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
]) | ||
] | ||
]) | ||
] | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.