Advertisement
Tanoro

Docusign Send Document Method

Jan 28th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. class DocuSign
  2. {
  3. var $config = array();
  4. var $loginUrl = 'https://demo.docusign.net/restapi/v2/login_information';
  5.  
  6. // Settings
  7. var $logTrans = true;
  8. var $logfile = 'apilog.txt';
  9. var $testmode = false;
  10.  
  11. // Recipient information
  12. var $recipientName = '';
  13. var $recipientEmail = '';
  14. var $recipientId = null; // userid
  15.  
  16. // Transaction components
  17. var $filename = '';
  18. var $basename = '';
  19. var $documentId = null; // Contract ID number
  20. var $emailSubject = '';
  21. var $header = '';
  22. var $accountId = null;
  23. var $baseUrl = '';
  24.  
  25. // Input form location
  26. var $pageNumber = 1;
  27. var $xPosition = 100;
  28. var $yPosition = 100;
  29.  
  30. // Diagnostics
  31. var $log = array();
  32. var $rawResponse = '';
  33. var $response = array();
  34. var $errors = array();
  35.  
  36.  
  37. /**
  38. * Hold onto the configs
  39. *
  40. * @param array The CMS configs array
  41. **/
  42. public function __construct($config)
  43. {
  44. $this->config = $config;
  45. }
  46.  
  47.  
  48. /**
  49. * Construct the transaction header
  50. **/
  51. function constructHeader()
  52. {
  53. $this->header = '<DocuSignCredentials>';
  54. $this->header .= '<Username>' . $this->config['docusign']['username'] . '</Username>';
  55. $this->header .= '<Password>' . $this->config['docusign']['password'] . '</Password>';
  56. $this->header .= '<IntegratorKey>' . $this->config['docusign']['integratorKey'] . '</IntegratorKey>';
  57. $this->header .= '</DocuSignCredentials>';
  58. }
  59.  
  60.  
  61. /**
  62. * Execute a login to DocuSign
  63. **/
  64. function login()
  65. {
  66. $this->constructHeader();
  67.  
  68. $ch = curl_init($this->loginUrl);
  69.  
  70. curl_setopt($ch, CURLOPT_HEADER, false);
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  72. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-DocuSign-Authentication: ' . $this->header));
  73.  
  74. $this->rawResponse = curl_exec($ch);
  75. $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  76.  
  77. if ($status != 200)
  78. {
  79. $this->errors[] = 'ERROR! HTTP request returned ' . $status;
  80. return false;
  81. }
  82.  
  83. $this->response = json_decode($this->rawResponse, true);
  84. $this->accountId = $this->response['loginAccounts'][0]['accountId'];
  85. $this->baseUrl = $this->response['loginAccounts'][0]['baseUrl'];
  86.  
  87. curl_close($ch);
  88.  
  89. return $this->response;
  90. }
  91.  
  92.  
  93. /**
  94. * Send a document to Docusign to be signed
  95. *
  96. * @param string The filename of the document to send to DocuSign
  97. **/
  98. function sendDocument($filename='')
  99. {
  100. if (!empty($filename))
  101. {
  102. $this->filename = $filename;
  103. }
  104.  
  105. $this->basename = basename($this->filename);
  106.  
  107. // Send document
  108. $data = array (
  109. 'emailSubject' => $this->emailSubject,
  110. 'documents' => array(
  111. array(
  112. 'documentId' => $this->documentId,
  113. 'name' => 'Contract'//$this->filename
  114. )
  115. ),
  116. 'recipients' => array(
  117. 'signers' => array(
  118. array(
  119. 'email' => $this->recipientEmail,
  120. 'name' => $this->recipientName,
  121. 'recipientId' => $this->recipientId, // userid
  122. 'tabs' => array(
  123. 'signHereTabs' => array(
  124. array(
  125. 'xPosition' => $this->xPosition,
  126. 'yPosition' => $this->yPosition,
  127. 'documentId' => $this->documentId,
  128. 'pageNumber' => $this->pageNumber
  129. )
  130. )
  131. )
  132. )
  133. )
  134. ),
  135. 'status' => 'sent'
  136. );
  137.  
  138. $data_string = json_encode($data);
  139. $file_contents = file_get_contents($this->filename);
  140.  
  141. // Put together a request body that Docusign can understand
  142. $requestBody = "\r\n";
  143. $requestBody .= "\r\n";
  144. $requestBody .= "--myboundary\r\n";
  145. $requestBody .= "Content-Type: application/json\r\n";
  146. $requestBody .= "Content-Disposition: form-data\r\n";
  147. $requestBody .= "\r\n";
  148. $requestBody .= $data_string . "\r\n";
  149. $requestBody .= "--myboundary\r\n";
  150. $requestBody .= "Content-Type:application/pdf\r\n";
  151. $requestBody .= "Content-Disposition: file; filename=\"" . $this->basename . "\"; documentid=" . $this->documentId . " \r\n";
  152. $requestBody .= "\r\n";
  153. $requestBody .= $file_contents . "\r\n";
  154. $requestBody .= "--myboundary--\r\n";
  155. $requestBody .= "\r\n";
  156.  
  157. // *** append "/envelopes" to baseUrl and as signature request endpoint
  158. $curl = curl_init($this->baseUrl . '/envelopes');
  159.  
  160. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  161. curl_setopt($curl, CURLOPT_POST, true);
  162. curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
  163. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  164. 'Content-Type: multipart/form-data;boundary=myboundary',
  165. 'Content-Length: ' . strlen($requestBody),
  166. 'X-DocuSign-Authentication: ' . $this->header
  167. )
  168. );
  169.  
  170. $this->response = curl_exec($curl);
  171. $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  172.  
  173. curl_close($curl);
  174.  
  175. if ( $status != 201 )
  176. {
  177. $this->errors[] = 'ERROR! HTTP request returned ' . $status;
  178. return false;
  179. }
  180.  
  181. $response = json_decode($this->response, true);
  182.  
  183. //--- display results
  184. return $response['envelopeId'];
  185. }
  186.  
  187.  
  188. /*
  189. * Get the URL with which to embed a signing request
  190. *
  191. * @param string The envelope ID
  192. */
  193. function embeddedSigning($envelopeId)
  194. {
  195. // Get the Embedded Singing View
  196. $data = array(
  197. 'returnUrl' => 'http://www.cgleasedevelopment.com',
  198. 'authenticationMethod' => 'Email',
  199. 'email' => $this->recipientEmail,
  200. 'userName' => $this->recipientName,
  201. 'clientUserId' => $this->recipientId // userid
  202. );
  203.  
  204. $data_string = json_encode($data);
  205.  
  206. $curl = curl_init($this->baseUrl . '/envelopes/' . $envelopeId . '/views/recipient');
  207.  
  208. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  209. curl_setopt($curl, CURLOPT_POST, true);
  210. curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  211. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  212. 'Content-Type: application/json',
  213. 'Content-Length: ' . strlen($data_string),
  214. 'X-DocuSign-Authentication: ' . $this->header
  215. ));
  216. curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  217.  
  218. $this->response = curl_exec($curl);
  219. $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  220.  
  221. if ($status != 201)
  222. {
  223. $this->errors[] = 'ERROR! HTTP request returned ' . $status;
  224. $this->errors[] = $this->response;
  225. return false;
  226. }
  227.  
  228. curl_close($curl);
  229.  
  230. $response = json_decode($this->response, true);
  231.  
  232. return $response['url'];
  233. }
  234. }
  235. ?>
  236.  
  237. <?php
  238. require_once('config.inc.php');
  239.  
  240. $DocuSign = new DocuSign($config);
  241.  
  242. if ($DocuSign->login())
  243. {
  244. $DocuSign->recipientName = 'Kraven Morehead';
  245. $DocuSign->recipientEmail = 'myemail@email.com';
  246. $DocuSign->recipientId = 1;
  247.  
  248. $DocuSign->filename = '/path/to/file/_test.pdf';
  249. $DocuSign->documentId = 1234;
  250. $DocuSign->emailSubject = 'This is a test document';
  251.  
  252. $envelopeId = $DocuSign->sendDocument();
  253.  
  254. if ($envelopeId)
  255. {
  256. echo 'Success! Envelope #' . $envelopeId . PHP_EOL . PHP_EOL;
  257. }
  258.  
  259. $signingURL = $DocuSign->embeddedSigning($envelopeId);
  260.  
  261. if ($signingURL)
  262. {
  263. echo 'Success! Signing URL: ' . $signingURL . PHP_EOL . PHP_EOL;
  264. }
  265. }
  266.  
  267. echo PHP_EOL;
  268. echo '--DIAGNOSTICS-------------------------------------------------------------------' . PHP_EOL . PHP_EOL;
  269.  
  270. if (sizeof($DocuSign->log) > 0)
  271. {
  272. echo 'Log:' . PHP_EOL;
  273.  
  274. foreach($DocuSign->log AS $k => $v)
  275. {
  276. echo $v . PHP_EOL;
  277. }
  278. }
  279.  
  280. echo PHP_EOL . PHP_EOL;
  281.  
  282. if (sizeof($DocuSign->errors) > 0)
  283. {
  284. echo 'Errors:' . PHP_EOL;
  285.  
  286. foreach($DocuSign->errors AS $k => $v)
  287. {
  288. echo $v . PHP_EOL;
  289. }
  290. }
  291.  
  292. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement