Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DocuSign
- {
- var $config = array();
- var $loginUrl = 'https://demo.docusign.net/restapi/v2/login_information';
- // Settings
- var $logTrans = true;
- var $logfile = 'apilog.txt';
- var $testmode = false;
- // Recipient information
- var $recipientName = '';
- var $recipientEmail = '';
- var $recipientId = null; // userid
- // Transaction components
- var $filename = '';
- var $basename = '';
- var $documentId = null; // Contract ID number
- var $emailSubject = '';
- var $header = '';
- var $accountId = null;
- var $baseUrl = '';
- // Input form location
- var $pageNumber = 1;
- var $xPosition = 100;
- var $yPosition = 100;
- // Diagnostics
- var $log = array();
- var $rawResponse = '';
- var $response = array();
- var $errors = array();
- /**
- * Hold onto the configs
- *
- * @param array The CMS configs array
- **/
- public function __construct($config)
- {
- $this->config = $config;
- }
- /**
- * Construct the transaction header
- **/
- function constructHeader()
- {
- $this->header = '<DocuSignCredentials>';
- $this->header .= '<Username>' . $this->config['docusign']['username'] . '</Username>';
- $this->header .= '<Password>' . $this->config['docusign']['password'] . '</Password>';
- $this->header .= '<IntegratorKey>' . $this->config['docusign']['integratorKey'] . '</IntegratorKey>';
- $this->header .= '</DocuSignCredentials>';
- }
- /**
- * Execute a login to DocuSign
- **/
- function login()
- {
- $this->constructHeader();
- $ch = curl_init($this->loginUrl);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-DocuSign-Authentication: ' . $this->header));
- $this->rawResponse = curl_exec($ch);
- $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- if ($status != 200)
- {
- $this->errors[] = 'ERROR! HTTP request returned ' . $status;
- return false;
- }
- $this->response = json_decode($this->rawResponse, true);
- $this->accountId = $this->response['loginAccounts'][0]['accountId'];
- $this->baseUrl = $this->response['loginAccounts'][0]['baseUrl'];
- curl_close($ch);
- return $this->response;
- }
- /**
- * Send a document to Docusign to be signed
- *
- * @param string The filename of the document to send to DocuSign
- **/
- function sendDocument($filename='')
- {
- if (!empty($filename))
- {
- $this->filename = $filename;
- }
- $this->basename = basename($this->filename);
- // Send document
- $data = array (
- 'emailSubject' => $this->emailSubject,
- 'documents' => array(
- array(
- 'documentId' => $this->documentId,
- 'name' => 'Contract'//$this->filename
- )
- ),
- 'recipients' => array(
- 'signers' => array(
- array(
- 'email' => $this->recipientEmail,
- 'name' => $this->recipientName,
- 'recipientId' => $this->recipientId, // userid
- 'tabs' => array(
- 'signHereTabs' => array(
- array(
- 'xPosition' => $this->xPosition,
- 'yPosition' => $this->yPosition,
- 'documentId' => $this->documentId,
- 'pageNumber' => $this->pageNumber
- )
- )
- )
- )
- )
- ),
- 'status' => 'sent'
- );
- $data_string = json_encode($data);
- $file_contents = file_get_contents($this->filename);
- // Put together a request body that Docusign can understand
- $requestBody = "\r\n";
- $requestBody .= "\r\n";
- $requestBody .= "--myboundary\r\n";
- $requestBody .= "Content-Type: application/json\r\n";
- $requestBody .= "Content-Disposition: form-data\r\n";
- $requestBody .= "\r\n";
- $requestBody .= $data_string . "\r\n";
- $requestBody .= "--myboundary\r\n";
- $requestBody .= "Content-Type:application/pdf\r\n";
- $requestBody .= "Content-Disposition: file; filename=\"" . $this->basename . "\"; documentid=" . $this->documentId . " \r\n";
- $requestBody .= "\r\n";
- $requestBody .= $file_contents . "\r\n";
- $requestBody .= "--myboundary--\r\n";
- $requestBody .= "\r\n";
- // *** append "/envelopes" to baseUrl and as signature request endpoint
- $curl = curl_init($this->baseUrl . '/envelopes');
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- 'Content-Type: multipart/form-data;boundary=myboundary',
- 'Content-Length: ' . strlen($requestBody),
- 'X-DocuSign-Authentication: ' . $this->header
- )
- );
- $this->response = curl_exec($curl);
- $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- curl_close($curl);
- if ( $status != 201 )
- {
- $this->errors[] = 'ERROR! HTTP request returned ' . $status;
- return false;
- }
- $response = json_decode($this->response, true);
- //--- display results
- return $response['envelopeId'];
- }
- /*
- * Get the URL with which to embed a signing request
- *
- * @param string The envelope ID
- */
- function embeddedSigning($envelopeId)
- {
- // Get the Embedded Singing View
- $data = array(
- 'returnUrl' => 'http://www.cgleasedevelopment.com',
- 'authenticationMethod' => 'Email',
- 'email' => $this->recipientEmail,
- 'userName' => $this->recipientName,
- 'clientUserId' => $this->recipientId // userid
- );
- $data_string = json_encode($data);
- $curl = curl_init($this->baseUrl . '/envelopes/' . $envelopeId . '/views/recipient');
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($data_string),
- 'X-DocuSign-Authentication: ' . $this->header
- ));
- curl_setopt($curl, CURLINFO_HEADER_OUT, true);
- $this->response = curl_exec($curl);
- $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if ($status != 201)
- {
- $this->errors[] = 'ERROR! HTTP request returned ' . $status;
- $this->errors[] = $this->response;
- return false;
- }
- curl_close($curl);
- $response = json_decode($this->response, true);
- return $response['url'];
- }
- }
- ?>
- <?php
- require_once('config.inc.php');
- $DocuSign = new DocuSign($config);
- if ($DocuSign->login())
- {
- $DocuSign->recipientName = 'Kraven Morehead';
- $DocuSign->recipientEmail = 'myemail@email.com';
- $DocuSign->recipientId = 1;
- $DocuSign->filename = '/path/to/file/_test.pdf';
- $DocuSign->documentId = 1234;
- $DocuSign->emailSubject = 'This is a test document';
- $envelopeId = $DocuSign->sendDocument();
- if ($envelopeId)
- {
- echo 'Success! Envelope #' . $envelopeId . PHP_EOL . PHP_EOL;
- }
- $signingURL = $DocuSign->embeddedSigning($envelopeId);
- if ($signingURL)
- {
- echo 'Success! Signing URL: ' . $signingURL . PHP_EOL . PHP_EOL;
- }
- }
- echo PHP_EOL;
- echo '--DIAGNOSTICS-------------------------------------------------------------------' . PHP_EOL . PHP_EOL;
- if (sizeof($DocuSign->log) > 0)
- {
- echo 'Log:' . PHP_EOL;
- foreach($DocuSign->log AS $k => $v)
- {
- echo $v . PHP_EOL;
- }
- }
- echo PHP_EOL . PHP_EOL;
- if (sizeof($DocuSign->errors) > 0)
- {
- echo 'Errors:' . PHP_EOL;
- foreach($DocuSign->errors AS $k => $v)
- {
- echo $v . PHP_EOL;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement