/PHP/DocuSignSample/embeddocusign.php

https://github.com/ashishshakya/DocuSign-eSignature-SDK · PHP · 436 lines · 332 code · 52 blank · 52 comment · 24 complexity · aeb6e7f034797eaf499e33d069fec5f5 MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) DocuSign, Inc. All rights reserved.
  4. *
  5. * This source code is intended only as a supplement to DocuSign SDK
  6. * and/or on-line documentation.
  7. * This sample is designed to demonstrate DocuSign features and is not intended
  8. * for production use. Code and policy for a production application must be
  9. * developed to meet the specific data and security requirements of the
  10. * application.
  11. *
  12. * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  13. * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14. * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  15. * PARTICULAR PURPOSE.
  16. */
  17. /*
  18. * Display embedded document for signing
  19. */
  20. //========================================================================
  21. // Includes
  22. //========================================================================
  23. include_once 'include/session.php'; // initializes session and provides
  24. include 'include/utils.php';
  25. //========================================================================
  26. // globals
  27. //========================================================================
  28. $_oneSigner = true; // Do we want One Signer (=true) or Two (=false)
  29. $_showTwoSignerMessage = false; // Display (or not display) a message before Signer One has signed (only for Two Signer mode)
  30. $_showTransitionMessage = false; // Display (or not display) a message after Signer One has signed (only for Two Signer mode)
  31. //========================================================================
  32. // Functions
  33. //========================================================================
  34. /**
  35. * Creates an embedded signing experience.
  36. */
  37. function createAndSend() {
  38. global $_oneSigner;
  39. $status = "";
  40. // Construct basic envelope
  41. $env = new Envelope();
  42. $env->Subject = "DocuSign API SDK Sample";
  43. $env->EmailBlurb = "This envelope demonstrates embedded signing";
  44. $env->AccountId = $_SESSION["AccountID"];
  45. $env->Recipients = constructRecipients($_oneSigner);
  46. $doc = new Document();
  47. $doc->PDFBytes = file_get_contents("resources/Docusign_Demo_11.pdf");
  48. $doc->Name = "Demo Document";
  49. $doc->ID = "1";
  50. $doc->FileExtension = "pdf";
  51. $env->Documents = array($doc);
  52. $env->Tabs = addTabs(count($env->Recipients));
  53. $api = getAPI();
  54. try {
  55. $csParams = new CreateAndSendEnvelope();
  56. $csParams->Envelope = $env;
  57. $status = $api->CreateAndSendEnvelope($csParams)->CreateAndSendEnvelopeResult;
  58. addEnvelopeID($status->EnvelopeID);
  59. getToken($status, 1);
  60. } catch (SoapFault $e) {
  61. $_SESSION["errorMessage"] = $e;
  62. header("Location: error.php");
  63. }
  64. }
  65. /**
  66. * Construct recipients
  67. *
  68. * @param boolean $oneSigner
  69. * true - create one recipient
  70. * false = create two recipient
  71. *
  72. * @return Recipient[]
  73. */
  74. function constructRecipients() {
  75. global $_oneSigner;
  76. $recipients[] = new Recipient();
  77. $r1 = new Recipient();
  78. $r1->UserName = $_SESSION["UserID"];
  79. $r1->Email = $_SESSION["UserID"];
  80. $r1->ID = 1;
  81. $r1->Type = RecipientTypeCode::Signer;
  82. $r1->CaptiveInfo = new RecipientCaptiveInfo();
  83. $r1->CaptiveInfo->ClientUserId = 1;
  84. array_push($recipients, $r1);
  85. if ($_oneSigner != true) {
  86. $r2 = new Recipient();
  87. $r2->UserName = "DocuSign Recipient2";
  88. $r2->Email = "DocuSignRecipient2@mailinator.com";
  89. $r2->ID = 2;
  90. $r2->Type = RecipientTypeCode::Signer;
  91. $r2->CaptiveInfo = new RecipientCaptiveInfo();
  92. $r2->CaptiveInfo->ClientUserId = 2;
  93. array_push($recipients, $r2);
  94. }
  95. // remove 0th element
  96. array_shift($recipients);
  97. return $recipients;
  98. }
  99. function addTabs($count) {
  100. $tabs[] = new Tab();
  101. $company = new Tab();
  102. $company->Type = TabTypeCode::Company;
  103. $company->DocumentID = "1";
  104. $company->PageNumber = "2";
  105. $company->RecipientID = "1";
  106. $company->XPosition = "342";
  107. $company->YPosition = "303";
  108. array_push($tabs, $company);
  109. $init1 = new Tab();
  110. $init1->Type = TabTypeCode::InitialHere;
  111. $init1->DocumentID = "1";
  112. $init1->PageNumber = "3";
  113. $init1->RecipientID = "1";
  114. $init1->XPosition = "454";
  115. $init1->YPosition = "281";
  116. array_push($tabs, $init1);
  117. $sign1 = new Tab();
  118. $sign1->Type = TabTypeCode::SignHere;
  119. $sign1->DocumentID = "1";
  120. $sign1->PageNumber = "2";
  121. $sign1->RecipientID = "1";
  122. $sign1->XPosition = "338";
  123. $sign1->YPosition = "330";
  124. array_push($tabs, $sign1);
  125. $fullAnchor = new Tab();
  126. $fullAnchor->Type = TabTypeCode::FullName;
  127. $anchor = new AnchorTab();
  128. $anchor->AnchorTabString = "Client Name (printed)";
  129. $anchor->XOffset = -123;
  130. $anchor->YOffset = 31;
  131. $anchor->Unit = UnitTypeCode::Pixels;
  132. $anchor->IgnoreIfNotPresent = true;
  133. $fullAnchor->AnchorTabItem = $anchor;
  134. $fullAnchor->DocumentID = "1";
  135. $fullAnchor->PageNumber = "2";
  136. $fullAnchor->RecipientID = "1";
  137. array_push($tabs, $fullAnchor);
  138. $date1 = new Tab();
  139. $date1->Type = TabTypeCode::DateSigned;
  140. $date1->DocumentID = "1";
  141. $date1->PageNumber = "2";
  142. $date1->RecipientID = "1";
  143. $date1->XPosition = "343";
  144. $date1->YPosition = "492";
  145. array_push($tabs, $date1);
  146. $init2 = new Tab();
  147. $init2->Type = TabTypeCode::InitialHere;
  148. $init2->DocumentID = "1";
  149. $init2->PageNumber = "3";
  150. $init2->RecipientID = "1";
  151. $init2->XPosition = "179";
  152. $init2->YPosition = "583";
  153. $init2->ScaleValue = "0.6";
  154. array_push($tabs, $init2);
  155. if ($count > 1) {
  156. $sign2 = new Tab();
  157. $sign2->Type = TabTypeCode::SignHere;
  158. $sign2->DocumentID = "1";
  159. $sign2->PageNumber = "3";
  160. $sign2->RecipientID = "2";
  161. $sign2->XPosition = "339";
  162. $sign2->YPosition = "97";
  163. array_push($tabs, $sign2);
  164. $date2 = new Tab();
  165. $date2->Type = TabTypeCode::DateSigned;
  166. $date2->DocumentID = "1";
  167. $date2->PageNumber = "3";
  168. $date2->RecipientID = "2";
  169. $date2->XPosition = "339";
  170. $date2->YPosition = "97";
  171. array_push($tabs, $date2);
  172. }
  173. $favColor = new Tab();
  174. $favColor->Type = TabTypeCode::Custom;
  175. $favColor->CustomTabType = CustomTabType::Text;
  176. $favColor->DocumentID = "1";
  177. $favColor->PageNumber = "3";
  178. $favColor->RecipientID = "1";
  179. $favColor->XPosition = "301";
  180. $favColor->YPosition = "416";
  181. array_push($tabs, $favColor);
  182. $fruitNo = new Tab();
  183. $fruitNo->Type = TabTypeCode::Custom;
  184. $fruitNo->CustomTabType = CustomTabType::Radio;
  185. $fruitNo->CustomTabRadioGroupName= "fruit";
  186. $fruitNo->TabLabel = "No";
  187. $fruitNo->Name = "No";
  188. $fruitNo->DocumentID = "1";
  189. $fruitNo->PageNumber = "3";
  190. $fruitNo->RecipientID = "1";
  191. $fruitNo->XPosition = "296";
  192. $fruitNo->YPosition = "508";
  193. array_push($tabs, $fruitNo);
  194. $fruitYes = new Tab();
  195. $fruitYes->Type = TabTypeCode::Custom;
  196. $fruitYes->CustomTabType = CustomTabType::Radio;
  197. $fruitYes->CustomTabRadioGroupName= "fruit";
  198. $fruitYes->TabLabel = "Yes";
  199. $fruitYes->Name = "Yes";
  200. $fruitYes->Value = "Yes";
  201. $fruitYes->DocumentID = "1";
  202. $fruitYes->PageNumber = "3";
  203. $fruitYes->RecipientID = "1";
  204. $fruitYes->XPosition = "202";
  205. $fruitYes->YPosition = "509";
  206. array_push($tabs, $fruitYes);
  207. $data1 = new Tab();
  208. $data1->Type = TabTypeCode::Custom;
  209. $data1->CustomTabType = CustomTabType::Text;
  210. $data1->ConditionalParentLabel = "fruit";
  211. $data1->ConditionalParentValue = "Yes";
  212. $data1->TabLabel = "Preferred Fruit";
  213. $data1->Name = "Fruit";
  214. $data1->DocumentID = "1";
  215. $data1->PageNumber = "3";
  216. $data1->RecipientID = "1";
  217. $data1->XPosition = "265";
  218. $data1->YPosition = "547";
  219. array_push($tabs, $data1);
  220. // eliminate 0th element
  221. array_shift($tabs);
  222. return $tabs;
  223. }
  224. function getToken($status, $clientID) {
  225. global $_oneSigner;
  226. $token = null;
  227. $_SESSION["embedToken"];
  228. // get recipient token
  229. $assertion = new RequestRecipientTokenAuthenticationAssertion();
  230. $assertion->AssertionID = guid();
  231. $assertion->AuthenticationInstant = todayXsdDate();
  232. $assertion->AuthenticationMethod = RequestRecipientTokenAuthenticationAssertionAuthenticationMethod::Password;
  233. $assertion->SecurityDomain = "DocuSign2011Q1Sample";
  234. // Get Recipient fro ClientID
  235. $recipient = false;
  236. foreach($status->RecipientStatuses->RecipientStatus as $rs){
  237. if($rs->ClientUserId == $clientID){
  238. $recipient = $rs;
  239. }
  240. }
  241. if($recipient == false){
  242. $_SESSION["errorMessage"] = "Unable to find Recipient";
  243. header("Location: error.php");
  244. }
  245. $urls = new RequestRecipientTokenClientURLs();
  246. $urlbase = getCallbackURL("pop.html") . "?source=Embedded";
  247. $urls->OnAccessCodeFailed = $urlbase . "&event=AccessCodeFailed&uname=" . $recipient->UserName;
  248. $urls->OnCancel = $urlbase . "&event=Cancel&uname=" . $recipient->UserName;
  249. $urls->OnDecline = $urlbase . "&event=Decline&uname=" . $recipient->UserName;
  250. $urls->OnException = $urlbase . "&event=Exception&uname=" . $recipient->UserName;
  251. $urls->OnFaxPending = $urlbase . "&event=FaxPending&uname=" . $recipient->UserName;
  252. $urls->OnIdCheckFailed = $urlbase . "&event=IdCheckFailed&uname=" . $recipient->UserName;
  253. $urls->OnSessionTimeout = $urlbase . "&event=SessionTimeout&uname=" . $recipient->UserName;
  254. $urls->OnTTLExpired = $urlbase . "&event=TTLExpired&uname=" . $recipient->UserName;
  255. $urls->OnViewingComplete = $urlbase . "&event=ViewingComplete&uname=" . $recipient->UserName;
  256. if ($_oneSigner) {
  257. $urls->OnSigningComplete = $urlbase . "&event=SigningComplete&uname=" . $recipient->UserName;
  258. }
  259. else {
  260. $urls->OnSigningComplete = getCallbackURL("pop2.html") . "?envelopeID=" . $status->EnvelopeID;
  261. }
  262. $api = getAPI();
  263. $rrtParams = new RequestRecipientToken();
  264. $rrtParams->AuthenticationAssertion = $assertion;
  265. $rrtParams->ClientURLs = $urls;
  266. $rrtParams->ClientUserID = $recipient->ClientUserId;
  267. $rrtParams->Email = $recipient->Email;
  268. $rrtParams->EnvelopeID = $status->EnvelopeID;
  269. $rrtParams->Username = $recipient->UserName;
  270. try {
  271. $token = $api->RequestRecipientToken($rrtParams)->RequestRecipientTokenResult;
  272. } catch (SoapFault $e) {
  273. $_SESSION["errorMessage"] = $e;
  274. header("Location: error.php");
  275. }
  276. $_SESSION["embedToken"] = $token;
  277. }
  278. function getStatus($envelopeID) {
  279. $status = null;
  280. $api = getAPI();
  281. $rsParams = new RequestStatus();
  282. $rsParams->EnvelopeID = $envelopeID;
  283. try {
  284. $status = $api->RequestStatus($rsParams)->RequestStatusResult;
  285. } catch (SoapFault $e) {
  286. $_SESSION["errorMessage"] = $e;
  287. header("Location: error.php");
  288. }
  289. return $status;
  290. }
  291. //========================================================================
  292. // Main
  293. //========================================================================
  294. loginCheck();
  295. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  296. $_oneSigner = isset($_POST["OneSigner"]);
  297. createAndSend();
  298. if(!$_oneSigner){
  299. $_showTwoSignerMessage = true;
  300. }
  301. } else if ($_SERVER["REQUEST_METHOD"] == "GET") {
  302. if (isset($_GET["envelopeID"])) {
  303. // Display a message that we are moving on to Signer Number 2
  304. // - unless the message is suppressed (by signing from the GetStatusAndDocs page)
  305. if(isset($_GET['from_gsad'])){
  306. getToken(getStatus($_GET['envelopeID']),$_GET['clientID']);
  307. } else {
  308. $_showTransitionMessage = true;
  309. getToken(getStatus($_GET['envelopeID']), 2);
  310. }
  311. } else {
  312. $_SESSION["embedToken"] = "";
  313. }
  314. }
  315. ?>
  316. <!DOCTYPE html">
  317. <html>
  318. <head>
  319. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  320. <link rel="stylesheet" href="css/default.css" />
  321. <link rel="stylesheet" type="text/css" href="css/homestyle.css" />
  322. <link rel="stylesheet" href="css/jquery.ui.all.css" />
  323. <script type="text/javascript" src="js/jquery-1.4.4.js"></script>
  324. <script type="text/javascript" src="js/jquery.ui.core.js"></script>
  325. <script type="text/javascript" src="js/jquery.ui.widget.js"></script>
  326. <script type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
  327. <script type="text/javascript" src="js/Utils.js"></script>
  328. </head>
  329. <body>
  330. <div class="container">
  331. <div class="authbox">
  332. <span><?php echo $_SESSION["UserID"]; ?></span>
  333. (<a href="index.php?logout">logout</a>)
  334. </div>
  335. <table class="tabs" cellspacing="0" cellpadding="0">
  336. <tr>
  337. <td><a href="senddocument.php">Send Document</a></td>
  338. <td><a href="sendatemplate.php">Send a Template</a></td>
  339. <td class="current">Embed Docusign</td>
  340. <td><a href="getstatusanddocs.php">Get Status and Docs</a></td>
  341. </tr>
  342. </table>
  343. <form method="post" id="EmbedDocuSignForm">
  344. <table width="100%">
  345. <tr>
  346. <td class="rightalign">
  347. <input class="docusignbutton orange" name="OneSigner" id="OneSigner" type="submit" value="Create an Envelope with 1 Signer" />
  348. </td>
  349. <td class="leftalign">
  350. <input class="docusignbutton brown" name="TwoSigners" id="TwoSigners" type="submit" value="Create an Envelope with 2 Signers" />
  351. </td>
  352. </tr>
  353. </table>
  354. <?php
  355. // Display the Two Signer Message (if Two Signer Mode)
  356. if($_showTwoSignerMessage){
  357. ?>
  358. <div class="sampleMessage">
  359. Have the first signer fill out the Envelope (only a signature is required for the first signer)
  360. </div>
  361. <?
  362. }
  363. ?>
  364. <?php
  365. // Display the Transition Message (if required, in Two Signer Mode)
  366. if($_showTransitionMessage){
  367. ?>
  368. <div class="sampleMessage">
  369. The first signer has completed the Envelope. Now the second signer will be asked to fill out details in the Envelope.
  370. </div>
  371. <?
  372. }
  373. // Display the iFrame (if is needed)
  374. if(isset($_SESSION["embedToken"]) && !empty($_SESSION["embedToken"])){
  375. ?>
  376. <iframe class="embediframe" width="100%" height="70%" src="<?php echo $_SESSION["embedToken"]; ?>" id="hostiframe" name="hostiframe"></iframe>
  377. <?
  378. }
  379. ?>
  380. </form>
  381. <?php include 'include/footer.html';?>
  382. </div>
  383. </body>
  384. </html>