Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDR status service #8

Merged
merged 5 commits into from
Sep 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Resources/wsdl/billConsultService.ns1.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns:tns="http://service.sunat.gob.pe" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.sunat.gob.pe">
<types>
<xsd:schema>
<xsd:import namespace="http://service.sunat.gob.pe" schemaLocation="billConsultService.xsd"/>
</xsd:schema>
</types>
<message name="getStatus">
<part name="parameters" element="tns:getStatus"/>
</message>
<message name="getStatusResponse">
<part name="parameters" element="tns:getStatusResponse"/>
</message>
<message name="getStatusCdr">
<part name="parameters" element="tns:getStatusCdr"/>
</message>
<message name="getStatusCdrResponse">
<part name="parameters" element="tns:getStatusCdrResponse"/>
</message>
<portType name="billService">
<operation name="getStatus">
<input xmlns:ns1="http://www.w3.org/2007/05/addressing/metadata" ns1:Action="urn:getStatus" message="tns:getStatus"/>
<output xmlns:ns2="http://www.w3.org/2007/05/addressing/metadata" ns2:Action="http://service.sunat.gob.pe/billService/getStatusResponse" message="tns:getStatusResponse"/>
</operation>
<operation name="getStatusCdr">
<input xmlns:ns3="http://www.w3.org/2007/05/addressing/metadata" ns3:Action="urn:getStatusCdr" message="tns:getStatusCdr"/>
<output xmlns:ns4="http://www.w3.org/2007/05/addressing/metadata" ns4:Action="http://service.sunat.gob.pe/billService/getStatusCdrResponse" message="tns:getStatusCdrResponse"/>
</operation>
</portType>
</definitions>
30 changes: 30 additions & 0 deletions src/Resources/wsdl/billConsultService.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.ws.consulta.comppago.electronico.registro.servicio2.sunat.gob.pe/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.ws.consulta.comppago.electronico.registro.servicio2.sunat.gob.pe/" name="billConsultService">
<import namespace="http://service.sunat.gob.pe" location="billConsultService.ns1.wsdl"/>
<binding xmlns:ns1="http://service.sunat.gob.pe" name="BillConsultServicePortBinding" type="ns1:billService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getStatusCdr">
<soap:operation soapAction="urn:getStatusCdr"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="getStatus">
<soap:operation soapAction="urn:getStatus"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="billConsultService">
<port name="BillConsultServicePort" binding="tns:BillConsultServicePortBinding">
<soap:address location="https://e-factura.sunat.gob.pe/ol-it-wsconscpegem/billConsultService"/>
</port>
</service>
</definitions>
41 changes: 41 additions & 0 deletions src/Resources/wsdl/billConsultService.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version='1.0' encoding='UTF-8'?>
<xsd:schema xmlns:ns0="http://service.sunat.gob.pe" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://service.sunat.gob.pe">
<xsd:complexType name="getStatus">
<xsd:sequence>
<xsd:element name="rucComprobante" type="xsd:string" minOccurs="0"/>
<xsd:element name="tipoComprobante" type="xsd:string" minOccurs="0"/>
<xsd:element name="serieComprobante" type="xsd:string" minOccurs="0"/>
<xsd:element name="numeroComprobante" type="xsd:int" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="getStatusResponse">
<xsd:sequence>
<xsd:element name="status" type="ns0:statusResponse" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="getStatusCdrResponse">
<xsd:sequence>
<xsd:element name="statusCdr" type="ns0:statusResponse" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="statusResponse">
<xsd:sequence>
<xsd:element name="content" type="xsd:base64Binary" minOccurs="0"/>
<xsd:element name="statusCode" type="xsd:string" minOccurs="0"/>
<xsd:element name="statusMessage" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="getStatusCdr">
<xsd:sequence>
<xsd:element name="rucComprobante" type="xsd:string" minOccurs="0"/>
<xsd:element name="tipoComprobante" type="xsd:string" minOccurs="0"/>
<xsd:element name="serieComprobante" type="xsd:string" minOccurs="0"/>
<xsd:element name="numeroComprobante" type="xsd:int" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="getStatus" type="ns0:getStatus"/>
<xsd:element name="getStatusResponse" type="ns0:getStatusResponse"/>
<xsd:element name="getStatusCdrResponse" type="ns0:getStatusCdrResponse"/>
<xsd:element name="getStatusCdr" type="ns0:getStatusCdr"/>
</xsd:schema>
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Resources/wsdl/billService.wsdl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap11="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp200409="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsp200607="http://www.w3.org/2006/07/ws-policy" xmlns:ns0="http://service.gem.factura.comppago.registro.servicio.sunat.gob.pe/" xmlns:ns1="http://service.sunat.gob.pe" targetNamespace="http://service.gem.factura.comppago.registro.servicio.sunat.gob.pe/">
<wsdl:import location="ns1.wsdl" namespace="http://service.sunat.gob.pe"/>
<wsdl:import location="billService.ns1.wsdl" namespace="http://service.sunat.gob.pe"/>
<wsdl:binding name="BillServicePortBinding" type="ns1:billService">
<soap11:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="getStatus">
Expand Down
77 changes: 77 additions & 0 deletions src/Ws/Services/ConsultCdrService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Created by PhpStorm.
* User: Administrador
* Date: 02/10/2017
* Time: 10:10 AM.
*/

namespace Greenter\Ws\Services;

use Greenter\Model\Response\StatusCdrResult;

/**
* Class ConsultCdrService.
*/
class ConsultCdrService extends BaseSunat
{
/**
* Obtiene el estado del comprobante.
*
* @param string $ruc
* @param string $tipo
* @param string $serie
* @param int $numero
*
* @return StatusCdrResult
*/
public function getStatus($ruc, $tipo, $serie, $numero)
{
return $this->getStatusResult('getStatus', 'status', $ruc, $tipo, $serie, $numero);
}

/**
* Obtiene el CDR del comprobante.
*
* @param string $ruc
* @param string $tipo
* @param string $serie
* @param int $numero
*
* @return StatusCdrResult
*/
public function getStatusCdr($ruc, $tipo, $serie, $numero)
{
return $this->getStatusResult('getStatusCdr', 'statusCdr', $ruc, $tipo, $serie, $numero);
}

private function getStatusResult($method, $resultName, $ruc, $tipo, $serie, $numero)
{
$client = $this->getClient();
$result = new StatusCdrResult();

try {
$params = [
'rucComprobante' => $ruc,
'tipoComprobante' => $tipo,
'serieComprobante' => $serie,
'numeroComprobante' => $numero,
];
$response = $client->call($method, ['parameters' => $params]);
$statusCdr = $response->{$resultName};

$result->setCode($statusCdr->statusCode)
->setMessage($statusCdr->statusMessage)
->setCdrZip($statusCdr->content)
->setSuccess(true);

if ($statusCdr->content) {
$result->setCdrResponse($this->extractResponse($statusCdr->content));
}
} catch (\SoapFault $e) {
$result->setError($this->getErrorFromFault($e));
}

return $result;
}
}
4 changes: 3 additions & 1 deletion src/Ws/Services/ExtService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ public function getStatus($ticket)
}

/**
* @deprecated Use instead \Greenter\Ws\Services\ConsultCdrService
*
* @param string $ruc
* @param string $tipo
* @param string $serie
* @param string $numero
* @param int $numero
*
* @return StatusCdrResult
*/
Expand Down
61 changes: 61 additions & 0 deletions tests/Ws/Services/ConsultCdrServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Created by PhpStorm.
* User: Giansalex
* Date: 09/09/2018
* Time: 15:16
*/

namespace Tests\Greenter\Ws\Services;

class ConsultCdrServiceTest extends \PHPUnit_Framework_TestCase
{
use ConsultCdrServiceTrait;

public function testGetStatusInvalidUser()
{
$service = $this->getConsultService();

$result = $service->getStatus('20000000001', '01','F001', 1);

$this->assertFalse($result->isSuccess());
$this->assertNotNull($result->getError());
$this->assertEquals('00103', $result->getError()->getCode());
}

public function testGetStatusCdrInvalidUser()
{
$service = $this->getConsultService();

$result = $service->getStatusCdr('20000000001', '01','F001', 1);

$this->assertFalse($result->isSuccess());
$this->assertNull($result->getCdrZip());
$this->assertNotNull($result->getError());
$this->assertEquals('00103', $result->getError()->getCode());
}

public function testGetStatus()
{
$wss = $this->getConsultSender();
$result = $wss->getStatus('20000000001', '01', 'F001', '1');

$this->assertTrue($result->isSuccess());
$this->assertEquals('0', $result->getCode());
$this->assertNotEmpty($result->getMessage());
$this->assertNull($result->getCdrResponse());
$this->assertContains('aceptada', strtolower($result->getMessage()));
}

public function testGetCdrStatus()
{
$wss = $this->getConsultSender();
$result = $wss->getStatusCdr('20000000001', '01', 'F001', '1');

$this->assertTrue($result->isSuccess());
$this->assertEquals('0', $result->getCode());
$this->assertNotEmpty($result->getMessage());
$this->assertNotNull($result->getCdrResponse());
$this->assertContains('aceptada', $result->getCdrResponse()->getDescription());
}
}
71 changes: 71 additions & 0 deletions tests/Ws/Services/ConsultCdrServiceTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Created by PhpStorm.
* User: Giansalex
* Date: 09/09/2018
* Time: 15:19
*/

namespace Tests\Greenter\Ws\Services;

use Greenter\Ws\Services\ConsultCdrService;
use Greenter\Ws\Services\SoapClient;
use Greenter\Ws\Services\SunatEndpoints;
use Greenter\Ws\Services\WsClientInterface;

/**
* Trait ConsultCdrServiceTrait
*
* @method \PHPUnit_Framework_MockObject_MockBuilder getMockBuilder($className)
*/
trait ConsultCdrServiceTrait
{
/**
* @return ConsultCdrService
*/
private function getConsultService()
{
$client = new SoapClient(__DIR__.'/../../../src/Resources/wsdl/billConsultService.wsdl');
$client->setService(SunatEndpoints::FE_CONSULTA_CDR);
$client->setCredentials('20000000001MODATOS', 'modatos');

$sunat = new ConsultCdrService();
$sunat->setClient($client);

return $sunat;
}

/**
* @return ConsultCdrService
*/
private function getConsultSender()
{
$stub = $this->getMockBuilder(WsClientInterface::class)
->getMock();

$stub->method('call')
->will($this->returnCallback(function ($func, $params) {
$obj = new \stdClass();
if ($func == 'getStatus') {
$obj->status = new \stdClass();
$obj->status->statusCode = '0';
$obj->status->statusMessage = 'ACEPTADA';
$obj->status->content = null;
} elseif ($func == 'getStatusCdr') {
$zipContent = file_get_contents(__DIR__.'/../../Resources/cdrBaja.zip');
$obj->statusCdr = new \stdClass();
$obj->statusCdr->statusCode = '0';
$obj->statusCdr->statusMessage = 'ACEPTADA';
$obj->statusCdr->content = $zipContent;
}

return $obj;
}));

/**@var $stub WsClientInterface */
$sunat = new ConsultCdrService();
$sunat->setClient($stub);

return $sunat;
}
}