From b6e90b2df5df4ac2604ae36ddae2766e786029d8 Mon Sep 17 00:00:00 2001 From: Bruno Ferme Gasparin Date: Wed, 22 Feb 2017 19:03:21 -0300 Subject: [PATCH 1/4] Updated nfe-php Library to compliance the current Nfe API --- lib/NFe/APIRequest.php | 15 ++++++++++++--- lib/NFe/APIResource.php | 23 +++++++++++++++-------- lib/NFe/Error.php | 6 ++++-- lib/NFe/Object.php | 2 +- lib/NFe/Utilities.php | 9 ++++++--- test/NFe/CompanyTest.php | 23 +++++++++++------------ test/NFe/LegalPersonTest.php | 1 - test/NFe/ServiceInvoiceTest.php | 17 ++++++++++------- test/NFe/WebhookTest.php | 19 +++++++++---------- 9 files changed, 68 insertions(+), 47 deletions(-) diff --git a/lib/NFe/APIRequest.php b/lib/NFe/APIRequest.php index 215e63a..6720555 100755 --- a/lib/NFe/APIRequest.php +++ b/lib/NFe/APIRequest.php @@ -28,21 +28,30 @@ public function request( $method, $url, $data = array() ) { $headers = $this->_defaultHeaders(); list( $response_body, $response_code ) = $this->requestWithCURL( $method, $url, $headers, $data ); + $response = json_encode("{}"); + if ( $response_code == 302 ) { $response = $response_body; } - else { + else if (!empty($response_body)) { $response = json_decode($response_body); + if ( json_last_error() != JSON_ERROR_NONE ) { + throw new NFeInvalidResponseBody($response_body); + } } - if ( json_last_error() != JSON_ERROR_NONE ) { - throw new NFeObjectNotFound($response_body); + if ( $response_code == 409 ) { + throw new NFeObjectAlreadyExists($response_body); } if ( $response_code == 404 ) { throw new NFeObjectNotFound($response_body); } + if ( $response_code == 400 ) { + throw new NFeException($response->message); + } + if ( isset($response->errors) ) { if ( ( gettype($response->errors) != "string") && count( get_object_vars($response->errors) ) == 0 ) { unset($response->errors); diff --git a/lib/NFe/APIResource.php b/lib/NFe/APIResource.php index ff94a72..b0321ae 100755 --- a/lib/NFe/APIResource.php +++ b/lib/NFe/APIResource.php @@ -39,22 +39,30 @@ public static function API() { public static function endpointAPI( $object = null, $uri_path = '' ) { $path = ''; + $resource = self::objectBaseURI(); if ( is_string($object) || is_integer($object) ) { $path = '/' . $object; } elseif (is_array($object) && isset($object['company_id'])) { - $uri_path = '/companies/' . $object['company_id']; - } - elseif (is_object($object) && isset($object->provider) && isset($object->provider->id) ) { - $uri_path = '/companies/' . $object->provider->id; + $uri_path = '/companies/' . $object['company_id']; + + if (isset($object['id'])) { + $path = '/' . $object['id']; + } } + elseif (is_object($object)) { + if (isset($object->provider) && isset($object->provider->id)) { + $uri_path = '/companies/' . $object->provider->id; + } - if (isset($object['id'])) { - $path = '/' . $object['id']; + if (!$object->is_new()) { + $path = '/' . $object->id; + } } - return strtolower( NFe::getBaseURI() . $uri_path . '/' . self::objectBaseURI() . $path ); + + return strtolower( NFe::getBaseURI() . $uri_path . '/' . $resource . $path ); } public static function url( $object = null ) { @@ -74,7 +82,6 @@ protected function deleteAPI() { // if ( $this['id'] == null ) { // $this['id'] // return false; // } - try { $response = self::API()->request( 'DELETE', static::url($this) ); diff --git a/lib/NFe/Error.php b/lib/NFe/Error.php index bcbdf05..6953990 100644 --- a/lib/NFe/Error.php +++ b/lib/NFe/Error.php @@ -1,5 +1,7 @@ $resource)) { + return new $class_name( (array) $response->$resource ); + } + return new $class_name( (array) $response ); } - return null; } } diff --git a/test/NFe/CompanyTest.php b/test/NFe/CompanyTest.php index c3c5fec..334a89a 100644 --- a/test/NFe/CompanyTest.php +++ b/test/NFe/CompanyTest.php @@ -5,7 +5,7 @@ class NFe_CompanyTest extends NFe_TestCase { public function testCreateAndDelete() { $attributes = array( - 'federalTaxNumber' => 48527553000123, // Generate CNPJ here: http://www.geradordecnpj.org/ + 'federalTaxNumber' => 97625117000100, // Generate CNPJ here: http://www.geradordecnpj.org/ 'name' => 'TEST Company Name', 'tradeName' => 'Company Name', 'email' => 'nfe@mailinator.com', @@ -28,38 +28,37 @@ public function testCreateAndDelete() { $object = NFe_Company::create( $attributes ); $this->assertNotNull($object); - $this->assertNotNull( $object->companies->name ); - $this->assertEqual( $object->companies->name, 'TEST Company Name' ); - - self::$id = $object->companies->id; + $this->assertNotNull( $object->name ); + $this->assertEqual( $object->name, 'TEST Company Name' ); + self::$id = $object->id; } public function testGet() { $object = NFe_Company::fetch( self::$id ); $this->assertNotNull($object); - $this->assertNotNull($object->companies->name); - $this->assertEqual($object->companies->name, 'TEST Company Name'); + $this->assertNotNull($object->name); + $this->assertEqual($object->name, 'TEST Company Name'); } public function testUpdate() { $object = NFe_Company::fetch( self::$id ); - $object->companies->name = 'BB SA'; + $object->name = 'BB SA'; // @todo Check it out why this is giving error - // $this->assertTrue( $object->save() ); + // $this->assertTrue( $object->save() ); $this->assertNotNull($object); - $this->assertNotNull($object->companies->name); - $this->assertEqual($object->companies->name, 'BB SA'); + $this->assertNotNull($object->name); + $this->assertEqual($object->name, 'BB SA'); } public function testDelete() { $object = NFe_Company::fetch( self::$id ); $this->assertNotNull($object); - $this->assertNotNull($object->companies->name); + $this->assertNotNull($object->name); $this->assertTrue( $object->delete() ); } } diff --git a/test/NFe/LegalPersonTest.php b/test/NFe/LegalPersonTest.php index d577bb9..4c8d65b 100755 --- a/test/NFe/LegalPersonTest.php +++ b/test/NFe/LegalPersonTest.php @@ -13,7 +13,6 @@ public function testFetchFail() { $this->expectException('NFeObjectNotFound'); $result = NFe_LegalPerson::fetch( self::$company_id, self::$company_id ); - $this->assertNull($result); } diff --git a/test/NFe/ServiceInvoiceTest.php b/test/NFe/ServiceInvoiceTest.php index eea108f..24b04a5 100755 --- a/test/NFe/ServiceInvoiceTest.php +++ b/test/NFe/ServiceInvoiceTest.php @@ -2,10 +2,12 @@ class NFe_ServiceInvoiceTest extends NFe_TestCase { + protected $invoice; + public function testIssue() { $this->invoice = NFe_ServiceInvoice::create( // ID da empresa, você deve copiar exatamente como está no painel - "54244e0ee340420fdc94ad09", + "58ab266128718d082845e4be", // Dados da nota fiscal de serviço array( @@ -58,9 +60,10 @@ public function testIssue() { $this->assertEqual($this->invoice->cityServiceCode, '2690'); } - public function testFetchInvoice() { + public function testFetchInvoice() { + $fetched_invoice = NFe_ServiceInvoice::fetch( - "54244e0ee340420fdc94ad09", + "58ab266128718d082845e4be", $this->invoice->id ); @@ -72,16 +75,16 @@ public function testFetchInvoice() { public function testDownloadPdfInvoice() { $url = NFe_ServiceInvoice::pdf( - "54244e0ee340420fdc94ad09", + "58ab266128718d082845e4be", $this->invoice->id ); - + $this->assertTrue( strpos($url, "pdf") ); } public function testDownloadXmlInvoice() { $url = NFe_ServiceInvoice::xml( - "54244e0ee340420fdc94ad09", + "58ab266128718d082845e4be", $this->invoice->id ); @@ -90,7 +93,7 @@ public function testDownloadXmlInvoice() { public function testCancelInvoice() { $fetched_invoice = NFe_ServiceInvoice::fetch( - "54244e0ee340420fdc94ad09", + "58ab266128718d082845e4be", $this->invoice->id ); diff --git a/test/NFe/WebhookTest.php b/test/NFe/WebhookTest.php index 5a95574..6254dad 100644 --- a/test/NFe/WebhookTest.php +++ b/test/NFe/WebhookTest.php @@ -16,37 +16,37 @@ public function testCreateAndDelete() { $object = NFe_Webhook::create( $attributes ); $this->assertNotNull($object); - $this->assertNotNull($object->hooks->url); - $this->assertEqual($object->hooks->url, $attributes['url']); + $this->assertNotNull($object->url); + $this->assertEqual($object->url, $attributes['url']); - self::$id = $object->hooks->id; + self::$id = $object->id; } public function testGet() { $object = NFe_Webhook::fetch( self::$id ); $this->assertNotNull( $object ); - $this->assertNotNull( $object['hooks'][0]->url ); - $this->assertEqual( $object['hooks'][0]->url, 'http://google.com/test' ); + $this->assertNotNull( $object->url ); + $this->assertEqual( $object->url, 'http://google.com/test' ); } public function testUpdate() { $object = NFe_Webhook::fetch( self::$id ); $new_url = 'http://google.com/test2'; - $object->hooks->url = $new_url; + $object->url = $new_url; $this->assertTrue($object->save()); $this->assertNotNull($object); - $this->assertNotNull($object->hooks->url); - $this->assertEqual($object->hooks->url, $new_url); + $this->assertNotNull($object->url); + $this->assertEqual($object->url, $new_url); } public function testDelete() { $object = NFe_Webhook::fetch( self::$id ); $this->assertNotNull($object); - $this->assertNotNull( $object->hooks->url ); + $this->assertNotNull( $object->url ); $this->assertTrue($object->delete()); } @@ -54,6 +54,5 @@ public function testSearch() { $hooks = NFe_Webhook::search(); $this->assertNotNull( $hooks ); - $this->assertNotNull( $hooks->hooks ); } } From 702940376f74b20f437c377aa8af05e1620c18ca Mon Sep 17 00:00:00 2001 From: Bruno Ferme Gasparin Date: Thu, 23 Feb 2017 16:26:54 -0300 Subject: [PATCH 2/4] Change company id for tests --- test/NFe/ServiceInvoiceTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/NFe/ServiceInvoiceTest.php b/test/NFe/ServiceInvoiceTest.php index 24b04a5..68b4eb9 100755 --- a/test/NFe/ServiceInvoiceTest.php +++ b/test/NFe/ServiceInvoiceTest.php @@ -7,7 +7,7 @@ class NFe_ServiceInvoiceTest extends NFe_TestCase { public function testIssue() { $this->invoice = NFe_ServiceInvoice::create( // ID da empresa, você deve copiar exatamente como está no painel - "58ab266128718d082845e4be", + "54244e0ee340420fdc94ad09", // Dados da nota fiscal de serviço array( @@ -63,7 +63,7 @@ public function testIssue() { public function testFetchInvoice() { $fetched_invoice = NFe_ServiceInvoice::fetch( - "58ab266128718d082845e4be", + "54244e0ee340420fdc94ad09", $this->invoice->id ); @@ -75,7 +75,7 @@ public function testFetchInvoice() { public function testDownloadPdfInvoice() { $url = NFe_ServiceInvoice::pdf( - "58ab266128718d082845e4be", + "54244e0ee340420fdc94ad09", $this->invoice->id ); @@ -84,7 +84,7 @@ public function testDownloadPdfInvoice() { public function testDownloadXmlInvoice() { $url = NFe_ServiceInvoice::xml( - "58ab266128718d082845e4be", + "54244e0ee340420fdc94ad09", $this->invoice->id ); @@ -93,7 +93,7 @@ public function testDownloadXmlInvoice() { public function testCancelInvoice() { $fetched_invoice = NFe_ServiceInvoice::fetch( - "58ab266128718d082845e4be", + "54244e0ee340420fdc94ad09", $this->invoice->id ); From 715813ddc8889511928d5d05ad7303962e5fc7bc Mon Sep 17 00:00:00 2001 From: Bruno Ferme Gasparin Date: Thu, 23 Feb 2017 17:54:50 -0300 Subject: [PATCH 3/4] Changed Binding Classes to return arrays when the API return array --- lib/NFe/Utilities.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/NFe/Utilities.php b/lib/NFe/Utilities.php index 14d5975..aa81059 100755 --- a/lib/NFe/Utilities.php +++ b/lib/NFe/Utilities.php @@ -89,9 +89,18 @@ public static function createFromResponse( $object_type, $response ) { return new NFe_SearchResult( $results, count($results) ); } elseif ( is_object($response) ) { - $resource = $class_name::objectBaseURI(); - if(isset($response->$resource)) { - return new $class_name( (array) $response->$resource ); + $resources = $class_name::objectBaseURI(); + if(isset($response->$resources)) { + $result = []; + if (is_array($response->$resources)) { + foreach($response->$resources as $resource) { + $result[] = new $class_name( (array) $resource ); + } + + return $result; + } + + return new $class_name( (array) $response->$resources ); } return new $class_name( (array) $response ); } From 11d0094249e84a955d237ae6a083d9181bb1db3b Mon Sep 17 00:00:00 2001 From: Bruno Ferme Gasparin Date: Fri, 9 Jun 2017 17:04:56 -0300 Subject: [PATCH 4/4] Added Authorization treatment --- lib/NFe/APIRequest.php | 4 ++++ lib/NFe/APIResource.php | 2 +- lib/NFe/Company.php | 16 ++++++++-------- lib/NFe/Error.php | 1 + 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/NFe/APIRequest.php b/lib/NFe/APIRequest.php index 6720555..0eabe85 100755 --- a/lib/NFe/APIRequest.php +++ b/lib/NFe/APIRequest.php @@ -40,6 +40,10 @@ public function request( $method, $url, $data = array() ) { } } + if ( $response_code == 403 ) { + throw new NFeAuthorizationException($response_body); + } + if ( $response_code == 409 ) { throw new NFeObjectAlreadyExists($response_body); } diff --git a/lib/NFe/APIResource.php b/lib/NFe/APIResource.php index b0321ae..5dec605 100755 --- a/lib/NFe/APIResource.php +++ b/lib/NFe/APIResource.php @@ -109,7 +109,7 @@ protected static function searchAPI( $options = array() ) { protected static function fetchAPI($key) { try { - $response = self::API()->request( 'GET', static::url($key) ); + $response = self::API()->request( 'GET', static::url($key) ); return self::createFromResponse($response); } catch ( NFeObjectNotFound $e ) { diff --git a/lib/NFe/Company.php b/lib/NFe/Company.php index e3d253f..e8afffc 100644 --- a/lib/NFe/Company.php +++ b/lib/NFe/Company.php @@ -6,22 +6,22 @@ public static function create( $attributes = array() ) { } public static function fetch($key) { - return self::fetchAPI($key); + return self::fetchAPI($key); } - public function save() { + public function save() { return $this->saveAPI(); } - public function delete() { - return $this->deleteAPI(); + public function delete() { + return $this->deleteAPI(); } - public function refresh() { - return $this->refreshAPI(); + public function refresh() { + return $this->refreshAPI(); } - public static function search( $options = array() ) { - return self::searchAPI($options); + public static function search( $options = array() ) { + return self::searchAPI($options); } } diff --git a/lib/NFe/Error.php b/lib/NFe/Error.php index 6953990..409ca9b 100644 --- a/lib/NFe/Error.php +++ b/lib/NFe/Error.php @@ -1,6 +1,7 @@