Skip to content

Commit 651f546

Browse files
authored
Merge pull request #181 from docusign/multiple-delivery
Send envelope with multiple delivery channels
2 parents f41a6c3 + 0a51559 commit 651f546

File tree

4 files changed

+458
-0
lines changed

4 files changed

+458
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Example 046: Send an envelope with a remote signer and cc recipient o be notified via multiple
4+
* delivery channels (Email and SMS or WhatsApp).
5+
*/
6+
7+
namespace DocuSign\Controllers\Examples\eSignature;
8+
9+
use DocuSign\Controllers\eSignBaseController;
10+
use DocuSign\eSign\Client\ApiException;
11+
use DocuSign\Services\Examples\eSignature\MultipleDeliveryService;
12+
use DocuSign\Services\ManifestService;
13+
14+
class EG046MultipleDelivery extends eSignBaseController
15+
{
16+
const EG = 'eg046'; # reference (and url) for this example
17+
const FILE = __FILE__;
18+
19+
/**
20+
* Create a new controller instance.
21+
*
22+
* @return void
23+
*/
24+
public function __construct()
25+
{
26+
parent::__construct();
27+
parent::controller();
28+
}
29+
30+
/**
31+
* 1. Check the token
32+
* 2. Call the worker method
33+
* 3. Redirect the user to the signing
34+
*
35+
* @return void
36+
*/
37+
public function createController(): void
38+
{
39+
$this->checkDsToken();
40+
41+
try {
42+
$envelopeId = MultipleDeliveryService::multipleDelivery(
43+
$this->args,
44+
$this->clientService,
45+
$this::DEMO_DOCS_PATH,
46+
$GLOBALS['DS_CONFIG']['doc_docx'],
47+
$GLOBALS['DS_CONFIG']['doc_pdf']
48+
);
49+
50+
if ($envelopeId) {
51+
$_SESSION["envelope_id"] = $envelopeId["envelope_id"]; # Save for use by other examples
52+
# which need an envelope_id
53+
$this->clientService->showDoneTemplateFromManifest(
54+
$this->codeExampleText,
55+
null,
56+
ManifestService::replacePlaceholders(
57+
"{0}",
58+
$envelopeId["envelope_id"],
59+
$this->codeExampleText["ResultsPageText"]
60+
)
61+
);
62+
}
63+
} catch (ApiException $e) {
64+
$this->clientService->showErrorTemplate(
65+
new ApiException($this->codeExampleText["CustomErrorTexts"][0]["ErrorMessage"])
66+
);
67+
}
68+
}
69+
70+
/**
71+
* Get specific template arguments
72+
*
73+
* @return array
74+
*/
75+
public function getTemplateArgs(): array
76+
{
77+
$envelope_args = [
78+
'signer_name' => $this->checkInputValues($_POST['signerName']),
79+
'signer_email' => $this->checkEmailInputValue($_POST['signer_email']),
80+
'signer_country_code' => $this->checkInputValues($_POST['countryCode']),
81+
'signer_phone_number' => $this->checkInputValues($_POST['phoneNumber']),
82+
'cc_name' => $this->checkInputValues($_POST['ccName']),
83+
'cc_email' => $this->checkEmailInputValue($_POST['cc_email']),
84+
'cc_country_code' => $this->checkInputValues($_POST['ccCountryCode']),
85+
'cc_phone_number' => $this->checkInputValues($_POST['ccPhoneNumber']),
86+
'deliveryMethod' => $this->checkInputValues($_POST['deliveryMethod']),
87+
'status' => 'sent'
88+
];
89+
return [
90+
'account_id' => $_SESSION['ds_account_id'],
91+
'base_path' => $_SESSION['ds_base_path'],
92+
'ds_access_token' => $_SESSION['ds_access_token'],
93+
'envelope_args' => $envelope_args
94+
];
95+
}
96+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace DocuSign\Services\Examples\eSignature;
4+
5+
use DateTime;
6+
use DocuSign\eSign\Client\ApiException;
7+
use DocuSign\eSign\Model\CarbonCopy;
8+
use DocuSign\eSign\Model\EnvelopeDefinition;
9+
use DocuSign\eSign\Model\RecipientPhoneNumber;
10+
use DocuSign\eSign\Model\RecipientAdditionalNotification;
11+
use DocuSign\eSign\Model\Recipients;
12+
use DocuSign\eSign\Model\Signer;
13+
use DocuSign\eSign\Model\SignHere;
14+
use DocuSign\eSign\Model\Tabs;
15+
16+
class MultipleDeliveryService
17+
{
18+
/**
19+
* Do the work of the example
20+
* 1. Create the envelope request object to send via multiple
21+
* delivery channels (Email and SMS/WhatsApp).
22+
* 2. Send the envelope
23+
*
24+
* @param $args array
25+
* @param $clientService
26+
* @param $demoDocsPath
27+
* @return array ['envelope_id']
28+
*/
29+
30+
#ds-snippet-start:eSign46Step3
31+
public static function multipleDelivery(
32+
array $args,
33+
$clientService,
34+
$demoDocsPath,
35+
$docDocx,
36+
$docPDF
37+
): array {
38+
$envelopeDefinition = self::makeEnvelope(
39+
$args["envelope_args"],
40+
$clientService,
41+
$demoDocsPath,
42+
$docDocx,
43+
$docPDF
44+
);
45+
46+
$envelopeApi = $clientService->getEnvelopeApi();
47+
48+
$envelopeResponse = $envelopeApi->createEnvelopeWithHttpInfo($args['account_id'], $envelopeDefinition);
49+
50+
$remaining = $envelopeResponse[2]['X-RateLimit-Remaining'] ?? null;
51+
$reset = $envelopeResponse[2]['X-RateLimit-Reset'] ?? null;
52+
53+
if ($remaining !== null && $reset !== null) {
54+
$resetInstant = (new DateTime())->setTimestamp((int)$reset);
55+
error_log("API calls remaining: $remaining");
56+
error_log("Next Reset: " . $resetInstant->format(\DateTime::ATOM));
57+
}
58+
59+
return ['envelope_id' => $envelopeResponse[0]->getEnvelopeId()];
60+
}
61+
#ds-snippet-end:eSign46Step3
62+
63+
#ds-snippet-start:eSign46Step2
64+
private static function makeEnvelope(
65+
array $args,
66+
$clientService,
67+
$demoDocsPath,
68+
$docDocx,
69+
$docPDF
70+
): EnvelopeDefinition {
71+
$envelopeDefinition = CreateAnEnvelopeFunctionService::makeEnvelope(
72+
$args,
73+
$clientService,
74+
$demoDocsPath,
75+
$docDocx,
76+
$docPDF
77+
);
78+
79+
$signer = new Signer([
80+
'name' => $args['signer_name'],
81+
'email' => $args['signer_email'],
82+
'additional_notifications' => [
83+
self::buildAdditionalNotification(
84+
$args['signer_country_code'],
85+
$args['signer_phone_number'],
86+
$args['deliveryMethod']
87+
)
88+
],
89+
'recipient_id' => "1",
90+
'routing_order' => "1",
91+
'delivery_method' => "Email"
92+
]);
93+
94+
$cc = new CarbonCopy([
95+
'name' => $args['cc_name'],
96+
'email' => $args['cc_email'],
97+
'additional_notifications' => [
98+
self::buildAdditionalNotification(
99+
$args['cc_country_code'],
100+
$args['cc_phone_number'],
101+
$args['deliveryMethod']
102+
)
103+
],
104+
'recipient_id' => "2",
105+
'routing_order' => "2",
106+
'delivery_method' => "Email"
107+
]);
108+
109+
return self::addSignersToTheDelivery($signer, $cc, $envelopeDefinition, $args);
110+
}
111+
112+
private static function addSignersToTheDelivery($signer, $cc, $envelopeDefinition, $args)
113+
{
114+
$signHere = new SignHere([
115+
'anchor_string' => '**signature_1**',
116+
'anchor_units' => 'pixels',
117+
'anchor_y_offset' => '10',
118+
'anchor_x_offset' => '20'
119+
]);
120+
121+
$signHere2 = new SignHere([
122+
'anchor_string' => '/sn1/',
123+
'anchor_units' => 'pixels',
124+
'anchor_y_offset' => '10',
125+
'anchor_x_offset' => '20'
126+
]);
127+
128+
$signer->setTabs(new Tabs([
129+
'sign_here_tabs' => [$signHere, $signHere2]
130+
]));
131+
132+
$recipients = new Recipients([
133+
'signers' => [$signer],
134+
'carbon_copies' => [$cc]
135+
]);
136+
137+
$envelopeDefinition->setRecipients($recipients);
138+
$envelopeDefinition->setStatus($args["status"]);
139+
140+
return $envelopeDefinition;
141+
}
142+
#ds-snippet-end:eSign46Step2
143+
144+
private static function buildAdditionalNotification(
145+
string $countryCode,
146+
string $phoneNumber,
147+
string $deliveryMethod
148+
): RecipientAdditionalNotification {
149+
$phone = new RecipientPhoneNumber([
150+
'country_code' => $countryCode,
151+
'number' => $phoneNumber
152+
]);
153+
154+
return new RecipientAdditionalNotification([
155+
'secondary_delivery_method' => $deliveryMethod,
156+
'phone_number' => $phone
157+
]);
158+
}
159+
}

src/Services/RouterService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class RouterService implements IRouterService
5858
'eg044' => 'eSignature\EG044FocusedView',
5959
'eg045' => 'eSignature\EG045DeleteEnvelope',
6060
'eg045/RestoreEnvelope' => 'eSignature\EG045RestoreEnvelope',
61+
'eg046' => 'eSignature\EG046MultipleDelivery',
6162
'eg043/AuthRequest' => 'eSignature\EG043AuthRequest',
6263
'eg043/EnvelopesListStatus' => 'eSignature\EG043EnvelopesListStatus',
6364
'reg001' => 'Rooms\EG001CreateRoomWithData',
@@ -152,6 +153,7 @@ class RouterService implements IRouterService
152153
'eg044' => 'esignature/eg044_focused_view.html',
153154
'eg045' => 'esignature/eg045_delete_envelope.html',
154155
'eg045/RestoreEnvelope' => 'esignature/eg045_restore_envelope.html',
156+
'eg046' => 'esignature/eg046_multiple_delivery.html',
155157
'reg001' => 'rooms/eg001_create_room_with_data.html',
156158
'reg002' => 'rooms/eg002_create_room_with_template.html',
157159
'reg003' => 'rooms/eg003_export_data_from_room.html',

0 commit comments

Comments
 (0)