Skip to content

Commit 82f0143

Browse files
Add Maestro examples
2 parents 750216a + ee4eb39 commit 82f0143

23 files changed

+2146
-209
lines changed

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121
"docusign/rooms-client": "^2.1.0",
2222
"docusign/monitor-client": "^1.1.0",
2323
"docusign/webforms-client": "^1.0.0",
24+
"docusign/maestro-client": "dev-1.0.0-rc1-v1.0.0-1.0.3",
2425
"twig/twig": "^3.5.1",
2526
"league/oauth2-client": "^2.6.1",
2627
"ext-json": "*",
2728
"guzzlehttp/guzzle": "7.5.0",
28-
"firebase/php-jwt": "5.5.1",
29+
"firebase/php-jwt": "6.0.0",
2930
"mashape/unirest-php": "3.0.4",
3031
"squizlabs/php_codesniffer": "*",
3132
"phpunit/phpunit": "^9.5"
3233
},
3334
"require-dev": {
3435
"squizlabs/php_codesniffer": "*"
3536
}
36-
}
37+
}

composer.lock

+293-207
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/assets/search.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ let DS_SEARCH = (function () {
66
ROOMS: 'rooms',
77
ADMIN: 'admin',
88
CONNECT: 'connect',
9+
MAESTRO: 'maestro',
910
WEBFORMS: 'webforms'
1011
};
1112

@@ -126,6 +127,8 @@ let DS_SEARCH = (function () {
126127
return "eg";
127128
case API_TYPES.CONNECT:
128129
return "con";
130+
case API_TYPES.MAESTRO:
131+
return "mae";
129132
case API_TYPES.WEBFORMS:
130133
return "web";
131134
}

src/Controllers/Auth/DocuSign.php

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public function getDefaultScopes(): array
121121
. " account_read domain_read identity_provider_read user_data_redact asset_group_account_read"
122122
. " asset_group_account_clone_write asset_group_account_clone_read"
123123
];
124+
} elseif ($_SESSION['api_type'] == ApiTypes::MAESTRO) {
125+
return [
126+
"signature aow_manage"
127+
];
124128
} elseif ($_SESSION['api_type'] == ApiTypes::WEBFORMS) {
125129
return [
126130
"signature webforms_read webforms_instance_read webforms_instance_write"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?php
2+
3+
namespace DocuSign\Controllers\Examples\Maestro;
4+
5+
use DocuSign\Controllers\MaestroApiBaseController;
6+
use DocuSign\Maestro\Client\ApiException;
7+
use DocuSign\Services\Examples\Maestro\TriggerMaestroWorkflowService;
8+
use DocuSign\Services\ManifestService;
9+
use DocuSign\Maestro\Model\TriggerWorkflowViaPostResponse;
10+
11+
class Eg001TriggerWorkflow extends MaestroApiBaseController
12+
{
13+
const EG = 'mae001'; # reference (and url) for this example
14+
15+
const FILE = __FILE__;
16+
17+
/**
18+
* Create a new controller instance
19+
*
20+
* @return void
21+
* @throws ApiException
22+
*/
23+
public function __construct()
24+
{
25+
parent::__construct();
26+
$this->checkDsToken();
27+
$this->codeExampleText = $this->getPageText(static::EG);
28+
$workflowName = 'Example workflow - send invite to signer';
29+
$accountId = $this->args['account_id'];
30+
$workflowManagementApi = $this->clientService->workflowManagementApi();
31+
32+
try {
33+
$workflowDefinitions = TriggerMaestroWorkflowService::getWorkflowDefinitions(
34+
$workflowManagementApi,
35+
$accountId
36+
);
37+
38+
$this->selectNewestWorkflowByName($workflowDefinitions, $workflowName);
39+
} catch (ApiException $e) {
40+
if ($e->getCode() == 403) {
41+
$this->contactSupportToEnableFeature($e);
42+
}
43+
}
44+
45+
if ($_COOKIE["template_id"] != null && $_SESSION["workflow_id"] === null) {
46+
try {
47+
$createdWorkflowDefinition = TriggerMaestroWorkflowService::createWorkflow(
48+
$workflowManagementApi,
49+
$accountId,
50+
$_COOKIE["template_id"]
51+
);
52+
53+
$_SESSION["workflow_id"] = $createdWorkflowDefinition->getWorkflowDefinitionId();
54+
55+
$publishWorkflowUrl = TriggerMaestroWorkflowService::publishWorkflow(
56+
$workflowManagementApi,
57+
$accountId,
58+
$_SESSION["workflow_id"]
59+
);
60+
61+
$_SESSION["workflow_published"] = true;
62+
$this->openPublishWorkflowPage($publishWorkflowUrl);
63+
} catch (ApiException $e) {
64+
if ($e->getCode() == 403) {
65+
$this->contactSupportToEnableFeature($e);
66+
}
67+
}
68+
}
69+
70+
if ($_SESSION['workflow_published']) {
71+
$publishWorkflowUrl = TriggerMaestroWorkflowService::publishWorkflow(
72+
$workflowManagementApi,
73+
$accountId,
74+
$_SESSION["workflow_id"]
75+
);
76+
77+
if ($publishWorkflowUrl == null) {
78+
$_SESSION["workflow_published"] = false;
79+
} else {
80+
$this->openPublishWorkflowPage($publishWorkflowUrl);
81+
}
82+
}
83+
84+
parent::controller();
85+
}
86+
87+
/**
88+
* Check the access token and call the worker method
89+
* @return void
90+
* @throws ApiException
91+
*/
92+
public function createController(): void
93+
{
94+
$this->getTemplateArgs();
95+
96+
$workflowId = $_SESSION["workflow_id"];
97+
$workflowManagementApi = $this->clientService->workflowManagementApi();
98+
99+
$trigger = new TriggerWorkflowViaPostResponse();
100+
try {
101+
$workflowDefinition = TriggerMaestroWorkflowService::getWorkflowDefinition(
102+
$workflowManagementApi,
103+
$this->args['account_id'],
104+
$workflowId
105+
);
106+
107+
$triggerUrl = $workflowDefinition->getTriggerUrl();
108+
109+
$queryParams = parse_url($triggerUrl, PHP_URL_QUERY);
110+
parse_str($queryParams, $params);
111+
112+
$mtid = $params['mtid'];
113+
$mtsec = $params['mtsec'];
114+
115+
$triggerApi = $this->clientService->workflowTriggerApi();
116+
117+
$trigger = TriggerMaestroWorkflowService::triggerWorkflow(
118+
$triggerApi,
119+
$this->args['account_id'],
120+
$this->args['envelope_args']['instance_name'],
121+
$this->args['envelope_args']['signer_name'],
122+
$this->args['envelope_args']['signer_email'],
123+
$this->args['envelope_args']['cc_name'],
124+
$this->args['envelope_args']['cc_email'],
125+
$mtid,
126+
$mtsec
127+
);
128+
$_SESSION['instance_id'] = $trigger->getInstanceId();
129+
} catch (ApiException $e) {
130+
if ($e->getCode() == 403) {
131+
$this->contactSupportToEnableFeature($e);
132+
}
133+
}
134+
$this->clientService->showDoneTemplateFromManifest(
135+
$this->codeExampleText,
136+
json_encode($trigger->__toString())
137+
);
138+
}
139+
140+
/**
141+
* Get specific template arguments
142+
* @return array
143+
*/
144+
public function getTemplateArgs(): array
145+
{
146+
$envelope_args = [
147+
'instance_name' => $_POST['instance_name'],
148+
'signer_email' => $_POST['signer_email'],
149+
'signer_name' => $_POST['signer_name'],
150+
'cc_email' => $_POST['cc_email'],
151+
'cc_name' => $_POST['cc_name'],
152+
];
153+
return [
154+
'account_id' => $_SESSION['ds_account_id'],
155+
'base_path' => $_SESSION['ds_base_path'],
156+
'ds_access_token' => $_SESSION['ds_access_token'],
157+
'envelope_args' => $envelope_args
158+
];
159+
}
160+
161+
private function contactSupportToEnableFeature($e)
162+
{
163+
$GLOBALS['twig']->display('error.html', [
164+
'error_code' => $e->getCode(),
165+
'error_message' => ManifestService::replacePlaceholders(
166+
'{0}',
167+
'Maestro',
168+
ManifestService::getCommonTexts()['ContactSupportToEnableFeature']
169+
),
170+
'common_texts' => ManifestService::getCommonTexts()
171+
]);
172+
exit;
173+
}
174+
175+
private function selectNewestWorkflowByName($workflowDefinitions, $workflowName)
176+
{
177+
if ($workflowDefinitions['count'] > 0) {
178+
$filteredWorkflows = array_filter(
179+
$workflowDefinitions['value'],
180+
function ($workflow) use ($workflowName) {
181+
return $workflow['name'] === $workflowName;
182+
}
183+
);
184+
185+
usort($filteredWorkflows, function ($wf1, $wf2) {
186+
return strtotime($wf2['lastUpdatedDate']) - strtotime($wf1['lastUpdatedDate']);
187+
});
188+
189+
$workflow = reset($filteredWorkflows);
190+
191+
if ($workflow) {
192+
$_SESSION["workflow_id"] = $workflow['id'];
193+
}
194+
}
195+
}
196+
197+
private function openPublishWorkflowPage($publishWorkflowUrl)
198+
{
199+
$GLOBALS['twig']->display("maestro/eg001_publish_workflow.html", [
200+
'title' => $this->routerService->getTitle(static::EG),
201+
'consent_url' => ManifestService::replacePlaceholders(
202+
'{0}',
203+
$publishWorkflowUrl,
204+
$this->codeExampleText['AdditionalPage'][0]['ResultsPageText']
205+
),
206+
'code_example_text' => $this->codeExampleText,
207+
'common_texts' => $this->getCommonText()
208+
]);
209+
exit();
210+
}
211+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace DocuSign\Controllers\Examples\Maestro;
4+
5+
use DocuSign\Controllers\MaestroApiBaseController;
6+
use DocuSign\Maestro\Client\ApiException;
7+
use DocuSign\Services\Examples\Maestro\CancelMaestroWorkflowService;
8+
use DocuSign\Services\ManifestService;
9+
10+
class Eg002CancelWorkflow extends MaestroApiBaseController
11+
{
12+
const EG = 'mae002'; # reference (and url) for this example
13+
14+
const FILE = __FILE__;
15+
16+
/**
17+
* Create a new controller instance.
18+
*
19+
* @return void
20+
*/
21+
public function __construct()
22+
{
23+
parent::__construct();
24+
parent::controller();
25+
}
26+
27+
/**
28+
* Check the access token and call the worker method
29+
* @return void
30+
* @throws \DocuSign\Maestro\Client\ApiException
31+
*/
32+
public function createController(): void
33+
{
34+
$accountId = $_SESSION['ds_account_id'];
35+
$instanceId = $_SESSION['instance_id'];
36+
$workflowId = $_SESSION["workflow_id"];
37+
$workflowInstanceApi = $this->clientService->workflowInstanceManagementApi();
38+
39+
try {
40+
$isRedirectNeeded = CancelMaestroWorkflowService::getWorkflowInstanceAndCheckItsStatus(
41+
$workflowInstanceApi,
42+
$accountId,
43+
$workflowId,
44+
$instanceId
45+
);
46+
47+
if (!$isRedirectNeeded) {
48+
header('Location: ' . $GLOBALS['app_url'] . 'index.php?page=mae001');
49+
}
50+
51+
$result = CancelMaestroWorkflowService::cancelWorkflowInstance(
52+
$workflowInstanceApi,
53+
$accountId,
54+
$instanceId
55+
);
56+
} catch (ApiException $e) {
57+
if ($e->getCode() == 403) {
58+
$GLOBALS['twig']->display('error.html', [
59+
'error_code' => $e->getCode(),
60+
'error_message' => ManifestService::replacePlaceholders(
61+
'{0}',
62+
'Maestro',
63+
ManifestService::getCommonTexts()['ContactSupportToEnableFeature']
64+
),
65+
'common_texts' => ManifestService::getCommonTexts()
66+
]);
67+
exit;
68+
}
69+
}
70+
71+
$this->clientService->showDoneTemplateFromManifest(
72+
$this->codeExampleText,
73+
json_encode($result->__toString()),
74+
ManifestService::replacePlaceholders('{0}', $instanceId, $this->codeExampleText['ResultsPageText'])
75+
);
76+
}
77+
78+
/**
79+
* Get specific template arguments
80+
* @return array
81+
*/
82+
public function getTemplateArgs(): array
83+
{
84+
return [
85+
'account_id' => $_SESSION['ds_account_id'],
86+
'base_path' => $_SESSION['ds_base_path'],
87+
'ds_access_token' => $_SESSION['ds_access_token'],
88+
];
89+
}
90+
}

0 commit comments

Comments
 (0)