From 73179caeebcb36c8c2699e886547ef08949d3848 Mon Sep 17 00:00:00 2001 From: Humano Morfico Date: Tue, 21 Aug 2018 20:53:08 -0600 Subject: [PATCH] Added Organizations class --- src/Facturapi.php | 10 +- src/Http/BaseClient.php | 42 ++++++++ src/Resources/Organizations.php | 175 ++++++++++++++++++++++++++++++++ 3 files changed, 224 insertions(+), 3 deletions(-) create mode 100644 src/Resources/Organizations.php diff --git a/src/Facturapi.php b/src/Facturapi.php index 3856b37..3bfbcce 100644 --- a/src/Facturapi.php +++ b/src/Facturapi.php @@ -2,22 +2,26 @@ namespace Facturapi; require_once 'Resources/Customers.php'; +require_once 'Resources/Organizations.php'; require_once 'Resources/Products.php'; require_once 'Resources/Invoices.php'; use Facturapi\Resources\Customers; +use Facturapi\Resources\Organizations; use Facturapi\Resources\Products; use Facturapi\Resources\Invoices; class Facturapi { public $Customers; + public $Organizations; public $Products; public $Invoices; public function __construct( $api_key ) { - $this->Customers = new Customers( $api_key ); - $this->Products = new Products( $api_key ); - $this->Invoices = new Invoices( $api_key ); + $this->Customers = new Customers( $api_key ); + $this->Organizations = new Organizations( $api_key ); + $this->Products = new Products( $api_key ); + $this->Invoices = new Invoices( $api_key ); } } \ No newline at end of file diff --git a/src/Http/BaseClient.php b/src/Http/BaseClient.php index b80c018..d133e7b 100644 --- a/src/Http/BaseClient.php +++ b/src/Http/BaseClient.php @@ -231,6 +231,48 @@ protected function execute_JSON_put_request( $url, $body ) { } } + /** + * Executes HTTP PUT request + * + * @param URL String value for the URL to PUT to + * @param array $body + * + * @return Body of request result + * + * @throws Facturapi_Exception + */ + protected function execute_data_put_request( $url, $body ) { + $headers[] = 'Authorization: Basic ' . $this->FACTURAPI_KEY; + $headers[] = 'Content-Type: multipart/form-data'; + + $data = is_array( $body ) ? array( + 'cer' => new \CURLFile($body['cerFile']), + 'key' => new \CURLFile($body['keyFile']), + 'password' => $body['password'] + ) : array( + 'file' => new \CURLFile($body) + ); + + $ch = curl_init(); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT" ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); + + $result = curl_exec( $ch ); + $errno = curl_errno( $ch ); + $error = curl_error( $ch ); + $this->setLastStatusFromCurl( $ch ); + curl_close( $ch ); + if ( $errno > 0 ) { + throw new Facturapi_Exception( 'cURL error: ' . $error ); + } else { + return $result; + } + } + /** * Executes HTTP DELETE request * diff --git a/src/Resources/Organizations.php b/src/Resources/Organizations.php new file mode 100644 index 0000000..540a15b --- /dev/null +++ b/src/Resources/Organizations.php @@ -0,0 +1,175 @@ +execute_get_request( $this->get_request_url( $params ) ) ); + } catch ( Facturapi_Exception $e ) { + throw new Facturapi_Exception( 'Unable to get organizations: ' . $e ); + } + } + + /** + * Get a Organization by ID + * + * @param id : Unique ID for Organization + * + * @return JSON object for requested Organization + * + * @throws Facturapi_Exception + **/ + public function retrieve( $id ) { + try { + return json_decode( $this->execute_get_request( $this->get_request_url( $id ) ) ); + } catch ( Facturapi_Exception $e ) { + throw new Facturapi_Exception( 'Unable to get organization: ' . $e ); + } + } + + /** + * Create a Organization + * + * @param params : array of properties and property values for new Organization + * + * @return Response body with JSON object + * for created Organization from HTTP POST request + * + * @throws Facturapi_Exception + **/ + public function create( $params ) { + try { + return json_decode( $this->execute_JSON_post_request( $this->get_request_url(), $params ) ); + } catch ( Facturapi_Exception $e ) { + throw new Facturapi_Exception( 'Unable to create organization: ' . $e ); + } + } + + /** + * Update a Organization's legal information + * + * @param $id + * @param $params array of properties and property values for Organization's legal information + * + * @return Response body from HTTP POST request + * + * @throws Facturapi_Exception + * + */ + public function updateLegal( $id, $params ) { + try { + return json_decode( $this->execute_JSON_put_request( $this->get_request_url( $id ) . "/legal", $params ) ); + } catch ( Facturapi_Exception $e ) { + throw new Facturapi_Exception( 'Unable to update organization\'s legal information: ' . $e ); + } + } + + /** + * Update a Organization's customization information + * + * @param $id + * @param $params array of properties and property values for Organization's customization information + * + * @return Response body from HTTP POST request + * + * @throws Facturapi_Exception + * + */ + public function updateCustomization( $id, $params ) { + try { + return json_decode( $this->execute_JSON_put_request( $this->get_request_url( $id ) . "/customization", $params ) ); + } catch ( Facturapi_Exception $e ) { + throw new Facturapi_Exception( 'Unable to update organization\'s customization information: ' . $e ); + } + } + + /** + * Uploads the organization's logo + * + * @param $id + * @param $params array of properties and property values for Organization's logo + * + * @return Response body from HTTP POST request + * + * @throws Facturapi_Exception + * + */ + public function uploadLogo( $id, $params ) { + try { + return json_decode( $this->execute_data_put_request( $this->get_request_url( $id ) . "/logo", $params ) ); + } catch ( Facturapi_Exception $e ) { + throw new Facturapi_Exception( 'Unable to upload organization\'s logo: ' . $e ); + } + } + + /** + * Uploads the organization's certificate (CSD) + * + * @param $id + * @param $params array of properties and property values for organization's certificate (CSD) + * + * @return Response body from HTTP POST request + * + * @throws Facturapi_Exception + * + */ + public function uploadCertificate( $id, $params ) { + try { + return json_decode( $this->execute_data_put_request( $this->get_request_url( $id ) . "/certificate", $params ) ); + } catch ( Facturapi_Exception $e ) { + throw new Facturapi_Exception( 'Unable to upload organization\'s certificate (CSD): ' . $e ); + } + } + + /** + * Get the Api Keys for an Organization + * + * @param id : Unique ID for Organization + * + * @return JSON object for requested Organization + * + * @throws Facturapi_Exception + **/ + public function getApiKeys( $id ) { + try { + return json_decode( $this->execute_get_request( $this->get_request_url( $id ) . "/apikeys" ) ); + } catch ( Facturapi_Exception $e ) { + throw new Facturapi_Exception( 'Unable to get organization\'s api keys: ' . $e ); + } + } + + /** + * Delete a Organization + * + * @param id : Unique ID for the Organization + * + * @return Response body from HTTP POST request + * + * @throws Facturapi_Exception + **/ + public function delete( $id ) { + try { + return json_decode( $this->execute_delete_request( $this->get_request_url( $id ), null ) ); + } catch ( Facturapi_Exception $e ) { + throw new Facturapi_Exception( 'Unable to delete organization: ' . $e ); + } + } + +} \ No newline at end of file