Skip to content

Commit 5802de2

Browse files
committed
Send envelope with multiple delivery channels
1 parent f41a6c3 commit 5802de2

File tree

4 files changed

+451
-0
lines changed

4 files changed

+451
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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(new ApiException($this->codeExampleText["CustomErrorTexts"][0]["ErrorMessage"]));
65+
}
66+
}
67+
68+
/**
69+
* Get specific template arguments
70+
*
71+
* @return array
72+
*/
73+
public function getTemplateArgs(): array
74+
{
75+
$envelope_args = [
76+
'signer_name' => $this->checkInputValues($_POST['signerName']),
77+
'signer_email' => $this->checkEmailInputValue($_POST['signer_email']),
78+
'signer_country_code' => $this->checkInputValues($_POST['countryCode']),
79+
'signer_phone_number' => $this->checkInputValues($_POST['phoneNumber']),
80+
'cc_name' => $this->checkInputValues($_POST['ccName']),
81+
'cc_email' => $this->checkEmailInputValue($_POST['cc_email']),
82+
'cc_country_code' => $this->checkInputValues($_POST['ccCountryCode']),
83+
'cc_phone_number' => $this->checkInputValues($_POST['ccPhoneNumber']),
84+
'deliveryMethod' => $this->checkInputValues($_POST['deliveryMethod']),
85+
'status' => 'sent'
86+
];
87+
return [
88+
'account_id' => $_SESSION['ds_account_id'],
89+
'base_path' => $_SESSION['ds_base_path'],
90+
'ds_access_token' => $_SESSION['ds_access_token'],
91+
'envelope_args' => $envelope_args
92+
];
93+
}
94+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
public static function multipleDelivery(
30+
array $args,
31+
$clientService,
32+
$demoDocsPath,
33+
$docDocx,
34+
$docPDF): array
35+
{
36+
$envelopeDefinition = self::makeEnvelope(
37+
$args["envelope_args"],
38+
$clientService,
39+
$demoDocsPath,
40+
$docDocx,
41+
$docPDF
42+
);
43+
44+
$envelopeApi = $clientService->getEnvelopeApi();
45+
46+
$envelopeResponse = $envelopeApi->createEnvelopeWithHttpInfo($args['account_id'], $envelopeDefinition);
47+
48+
$remaining = $envelopeResponse[2]['X-RateLimit-Remaining'] ?? null;
49+
$reset = $envelopeResponse[2]['X-RateLimit-Reset'] ?? null;
50+
51+
if ($remaining !== null && $reset !== null) {
52+
$resetInstant = (new DateTime())->setTimestamp((int)$reset);
53+
error_log("API calls remaining: $remaining");
54+
error_log("Next Reset: " . $resetInstant->format(\DateTime::ATOM));
55+
}
56+
57+
return ['envelope_id' => $envelopeResponse[0]->getEnvelopeId()];
58+
}
59+
60+
private static function makeEnvelope(
61+
array $args,
62+
$clientService,
63+
$demoDocsPath,
64+
$docDocx,
65+
$docPDF
66+
): EnvelopeDefinition {
67+
$envelopeDefinition = CreateAnEnvelopeFunctionService::makeEnvelope(
68+
$args,
69+
$clientService,
70+
$demoDocsPath,
71+
$docDocx,
72+
$docPDF
73+
);
74+
75+
$signer = new Signer([
76+
'name' => $args['signer_name'],
77+
'email' => $args['signer_email'],
78+
'additional_notifications' => [
79+
self::buildAdditionalNotification(
80+
$args['signer_country_code'],
81+
$args['signer_phone_number'],
82+
$args['deliveryMethod']
83+
)
84+
],
85+
'recipient_id' => "1",
86+
'routing_order' => "1",
87+
'delivery_method' => "Email"
88+
]);
89+
90+
$cc = new CarbonCopy([
91+
'name' => $args['cc_name'],
92+
'email' => $args['cc_email'],
93+
'additional_notifications' => [
94+
self::buildAdditionalNotification(
95+
$args['cc_country_code'],
96+
$args['cc_phone_number'],
97+
$args['deliveryMethod']
98+
)
99+
],
100+
'recipient_id' => "2",
101+
'routing_order' => "2",
102+
'delivery_method' => "Email"
103+
]);
104+
105+
return self::addSignersToTheDelivery($signer, $cc, $envelopeDefinition, $args);
106+
}
107+
108+
private static function addSignersToTheDelivery($signer, $cc, $envelopeDefinition, $args)
109+
{
110+
$signHere = new SignHere([
111+
'anchor_string' => '**signature_1**',
112+
'anchor_units' => 'pixels',
113+
'anchor_y_offset' => '10',
114+
'anchor_x_offset' => '20'
115+
]);
116+
117+
$signHere2 = new SignHere([
118+
'anchor_string' => '/sn1/',
119+
'anchor_units' => 'pixels',
120+
'anchor_y_offset' => '10',
121+
'anchor_x_offset' => '20'
122+
]);
123+
124+
$signer->setTabs(new Tabs([
125+
'sign_here_tabs' => [$signHere, $signHere2]
126+
]));
127+
128+
$recipients = new Recipients([
129+
'signers' => [$signer],
130+
'carbon_copies' => [$cc]
131+
]);
132+
133+
$envelopeDefinition->setRecipients($recipients);
134+
$envelopeDefinition->setStatus($args["status"]);
135+
136+
return $envelopeDefinition;
137+
}
138+
139+
private static function buildAdditionalNotification(
140+
string $countryCode,
141+
string $phoneNumber,
142+
string $deliveryMethod
143+
): RecipientAdditionalNotification {
144+
$phone = new RecipientPhoneNumber([
145+
'country_code' => $countryCode,
146+
'number' => $phoneNumber
147+
]);
148+
149+
return new RecipientAdditionalNotification([
150+
'secondary_delivery_method' => $deliveryMethod,
151+
'phone_number' => $phone
152+
]);
153+
}
154+
}

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)