Skip to content

Commit

Permalink
Added Organizations class
Browse files Browse the repository at this point in the history
  • Loading branch information
Humano Morfico authored and javorosas committed Aug 22, 2018
1 parent 3917eab commit 73179ca
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Facturapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
42 changes: 42 additions & 0 deletions src/Http/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
175 changes: 175 additions & 0 deletions src/Resources/Organizations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

namespace Facturapi\Resources;

use Facturapi\Http\BaseClient;
use Facturapi\Exceptions\Facturapi_Exception;

class Organizations extends BaseClient {
protected $ENDPOINT = 'organizations';
protected $API_VERSION = 'v1';


/**
* Get all Organizations
*
* @param Search parameters
*
* @return JSON objects for all Organizations
*
* @throws Facturapi_Exception
**/
public function all( $params = null ) {
try {
return json_decode( $this->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 );
}
}

}

0 comments on commit 73179ca

Please sign in to comment.