Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Esignature {
- protected $url = 'https://capi-eval.signnow.com/api';
- protected $encoded_client_credentials = 'MGZjY2RiYzczNTgxY2EwZjliZjhjMzc5ZTZhOTY4MTM6MzcxOWExMjRiY2ZjMDNjNTM0ZDRmNWMwNWI1YTE5NmI=';
- protected $access_token;
- protected $data;
- protected $document_id;
- public function __construct($params)
- {
- $this->data = $params;
- }
- public function send()
- {
- if (!$this->getToken()) {
- return false;
- }
- if (!$this->uploadDocument()) {
- return false;
- }
- if (!$this->addSignature()) {
- return false;
- }
- if (!$this->sendInvite()) {
- return false;
- }
- }
- /**
- * Fetch auth token
- *
- * @return bool
- */
- protected function getToken()
- {
- $headers = array(
- "Authorization: Basic ".$this->encoded_client_credentials."",
- "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
- );
- $fields = array(
- 'username' => urlencode('gafitescu@gmail.com'),
- 'password' => urlencode('pass2sign'),
- 'grant_type' => urlencode('password')
- );
- $fields_string = '';
- foreach ($fields as $key => $value) {
- $fields_string .= $key.'='.$value.'&';
- }
- rtrim($fields_string, '&');
- $arResponse = $this->curl('/oauth2/token', $headers, $fields_string);
- if (isset($arResponse->access_token)) {
- $this->access_token = $arResponse->access_token;
- return true;
- } else {
- return false;
- }
- }
- protected function uploadDocument()
- {
- $headers = array(
- "Authorization: Bearer ".$this->access_token,
- "Content-Type: multipart/form-data"
- );
- $file = $this->getCurlValue($this->data['file']);
- $data = array('file' => $file);
- $arResponse = $this->curl('/document', $headers, $data);
- if (isset($arResponse->id)) {
- $this->document_id = $arResponse->id;
- return true;
- } else {
- return false;
- }
- }
- private function getCurlValue($filename)
- {
- $contentType = 'application/pdf' ;
- $postname = 'contract_'.date("d-m-Y H:i:s").'.pdf';
- if (function_exists('curl_file_create')) {
- return curl_file_create($filename, $contentType, $postname);
- }
- // Use the old style if using an older version of PHP
- $value = "@{$this->filename};filename=" . $postname;
- if ($contentType) {
- $value .= ';type=' . $contentType;
- }
- return $value;
- }
- protected function addSignature()
- {
- $nrPages = $this->getNrOfPagesFromPdf($this->data['file']) - 1; // 0 index pages
- $json_encoded_fields = array(
- array(
- "x" => 90,
- "y" => 786,
- "width" => 285,
- "height" => 25,
- "page_number" => $nrPages,
- "required"=> true,
- "role" => "Client",
- "type" => "signature"
- )
- );
- $data = json_encode(
- array(
- 'fields' => $json_encoded_fields
- )
- );
- $headers = array(
- "Authorization: Bearer ".$this->access_token,
- "Content-Type: application/json"
- );
- $arCustomOption = array(
- CURLOPT_CUSTOMREQUEST => "PUT"
- );
- $arResponse = $this->curl('/document/'.$this->document_id, $headers, $data, $arCustomOption);
- if (isset($arResponse->id)) {
- return true;
- } else {
- return false;
- }
- }
- protected function sendInvite()
- {
- $headers = array(
- "Authorization: Bearer ".$this->access_token,
- "Content-Type: application/json"
- );
- $data = json_encode(array(
- "to" => 'receiver@gmail.com',
- "from" => "gafitescu@email.com"
- ));
- $arResponse = $this->curl('/document/'.$this->document_id.'/invite?email=disable', $headers, $data);
- echo __METHOD__;
- var_dump($arResponse);
- }
- protected function fetchUser()
- {
- $headers = array(
- "Authorization: Basic ".$this->encoded_client_credentials,
- "Content-Type: application/json"
- );
- $data = json_encode(
- array(
- 'email' => 'usefortorrent@gmail.com',
- 'password' => 'password',
- 'first_name' => 'James',
- 'last_name' => 'Joyce'
- )
- );
- $arResponse = $this->curl('/user', $headers, $data);
- var_dump($arResponse);
- }
- private function getNrOfPagesFromPdf($pdfname)
- {
- $pdftext = file_get_contents($pdfname);
- $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
- return $num;
- }
- private function curl($resource, $headers, $data, $arCustomOptions = array())
- {
- $ch = curl_init();
- $options = array(
- CURLOPT_URL => $this->url.$resource,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_HTTPHEADER => $headers,
- CURLINFO_HEADER_OUT => false,
- CURLOPT_HEADER => false,
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS => $data
- );
- if (!empty($arCustomOptions)) {
- foreach ($arCustomOptions as $option => $value) {
- $options[$option] = $value;
- }
- }
- curl_setopt_array($ch, $options);
- $result = curl_exec($ch);
- var_dump($result);
- curl_close($ch);
- return json_decode($result);
- }
- }
- // usage:
- $params = array('file' => $file);
- $this->load->library('Esignature', $params,'esignature');
- $success = $this->esignature->send();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement