|
| 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 | +} |
0 commit comments