Last active
February 24, 2023 20:59
-
-
Save webrobert/bec57657cda76b7a628d253121dc5b2e to your computer and use it in GitHub Desktop.
never mind me
This file contains hidden or 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
| <?php | |
| namespace App\Services; | |
| use DocuSign\eSign\Api\EnvelopesApi; | |
| use DocuSign\eSign\Client\ApiClient; | |
| use DocuSign\eSign\Model\CompositeTemplate; | |
| use DocuSign\eSign\Model\Document; | |
| use DocuSign\eSign\Model\EnvelopeDefinition; | |
| use DocuSign\eSign\Model\InlineTemplate; | |
| use DocuSign\eSign\Model\Recipients; | |
| use DocuSign\eSign\Model\Signer; | |
| use DocuSign\eSign\Model\SignHere; | |
| use DocuSign\eSign\Model\Tabs; | |
| class Docusign | |
| { | |
| public ApiClient $client; | |
| private mixed $accountInfo; | |
| public function __construct() | |
| { | |
| $this->setUpClient(); | |
| } | |
| public function send($subject, $parties, $documents, $optionalMessage = null) | |
| { | |
| return $this->envelope((new EnvelopeDefinition()) | |
| ->setCompositeTemplates($this->composites($documents, $parties)) | |
| ->setEmailSubject($subject) | |
| ->setEmailBlurb($optionalMessage) | |
| ->setStatus('sent') | |
| ); | |
| } | |
| private function composites($documents, $parties): array | |
| { | |
| return $documents->transform( fn($document) => new CompositeTemplate([ | |
| 'inline_templates' => $this->inlineTemplates($parties, $document->id), | |
| 'composite_template_id' => "{$document->id}", | |
| 'document' => (new Base64Document())($document), | |
| ])) | |
| ->toArray(); | |
| } | |
| private function inlineTemplates($parties, $documentId): array | |
| { | |
| $signers = $this->signers($parties, $documentId); | |
| return [ | |
| (new InlineTemplate()) | |
| ->setRecipients(new Recipients(compact('signers'))) | |
| ->setSequence(1) | |
| ]; | |
| } | |
| private function signers($parties, $documentId): array | |
| { | |
| return $parties | |
| ->map( fn($party) => (new Signer()) | |
| ->setEmail($party->email ?? '[email protected]') | |
| ->setName($party->person_name) | |
| // ->setRoutingOrder() // i think this is order of signers.. | |
| ->setRecipientId($party->id) | |
| ->setTabs($this->tabs("**{$party->role->name}-{$documentId}**")) | |
| )->toArray(); | |
| } | |
| private function tabs($anchor_string): Tabs | |
| { | |
| $anchor = ['anchor_string' => Str::lower($anchor_string), 'anchor_units' => "pixels"]; | |
| $sign_here_tabs = [ | |
| new SignHere([ ...$anchor, | |
| 'anchor_x_offset' => "98", | |
| 'anchor_y_offset' => "-26" | |
| ]) | |
| ]; | |
| $date_signed_tabs = [ | |
| new DateSigned([ ...$anchor, | |
| 'anchor_x_offset' => "358", | |
| 'anchor_y_offset' => "-11", | |
| 'font' => "TimesNewRoman", | |
| 'font_size' => 'Size12' | |
| ]) | |
| ]; | |
| return new Tabs(compact(['sign_here_tabs', 'date_signed_tabs'])); | |
| } | |
| public function envelope($envelopeDefinition) | |
| { | |
| return (new EnvelopesApi($this->client)) | |
| ->createEnvelope($this->accountInfo->getAccountId(), $envelopeDefinition); | |
| } | |
| private function setUpClient() | |
| { | |
| $apiClient = new ApiClient(); | |
| $apiClient->getOAuth()->setOAuthBasePath(config('services.docusign.auth_server')); | |
| $this->client = $apiClient; | |
| $userInfo = $this->client->getUserInfo($this->getToken()); | |
| $accountInfo = $userInfo[0]->getAccounts(); | |
| $this->client->getConfig()->setHost( | |
| $accountInfo[0]->getBaseUri() . config('services.docusign.uri_suffix') | |
| ); | |
| $this->accountInfo = $accountInfo[0]; | |
| } | |
| private function getToken() | |
| { | |
| try { | |
| $privateKey = file_get_contents(storage_path(config('services.docusign.key_path')),true); | |
| $response = $this->client->requestJWTUserToken( | |
| $ikey = config('services.docusign.client_id'), | |
| $userId = config('services.docusign.impersonate_user_id'), | |
| $key = $privateKey, | |
| $scope = config('services.docusign.jwt_scope') | |
| ); | |
| $token = $response[0]; | |
| $accessToken = $token->getAccessToken(); | |
| } catch (\Throwable $th) { | |
| throw $th; | |
| } | |
| return $accessToken; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment