Code Monkey home page Code Monkey logo

consulta-cpe's Introduction

Consulta Integrada CPE

CI

PHP Client para API de CONSULTA INTEGRADA DE COMPROBANTE DE PAGO expuesta por SUNAT.

Requerimientos

  • PHP 7.1 o posterior
  • curl extension habilitado.

Instalación

Utilizando Composer:

composer require greenter/consulta-cpe

Uso

Primero es necesario obtener el client_id, client_secret desde el portal de SUNAT, puedes seguir la guía oficial.

  1. Solicitud de token.
<?php

$apiInstance = new \Greenter\Sunat\ConsultaCpe\Api\AuthApi(
    new \GuzzleHttp\Client()
);

$grant_type = 'client_credentials'; // Constante
$scope = 'https://api.sunat.gob.pe/v1/contribuyente/contribuyentes'; // Constante
$client_id = 'client_id_example'; // client_id generado en menú sol
$client_secret = 'client_secret_example'; // client_secret generado en menú sol

try {
    $result = $apiInstance->getToken($grant_type, $scope, $client_id, $client_secret);
        
    echo 'Token: '.$result->getAccessToken().PHP_EOL;
    echo 'Expira: '.$result->getExpiresIn().' segundos'.PHP_EOL;
} catch (Exception $e) {
    echo 'Excepcion cuando invocaba AuthApi->getToken: ', $e->getMessage(), PHP_EOL;
}

No necesitas solicitar un token por cada consulta, puedes usar el mismo durante el tiempo de expiración, generalmente 3600 seg (1h).

  1. Consulta de CPE.
<?php

// Token generado en el ejemplo anterior
$token = 'xxxxxxxx';

$config = \Greenter\Sunat\ConsultaCpe\Configuration::getDefaultConfiguration()->setAccessToken($token);

$apiInstance = new \Greenter\Sunat\ConsultaCpe\Api\ConsultaApi(
    new GuzzleHttp\Client(),
    $config->setHost($config->getHostFromSettings(1))
);
$ruc = '20000000001'; // RUC de quién realiza la consulta

$cpeFilter = (new \Greenter\Sunat\ConsultaCpe\Model\CpeFilter())
            ->setNumRuc('20000000001') // RUC del emisor
            ->setCodComp('01') // Tipo de comprobante
            ->setNumeroSerie('F001')
            ->setNumero('1')
            ->setFechaEmision('20/10/2020')
            ->setMonto('100.00');

try {
    $result = $apiInstance->consultarCpe($ruc, $cpeFilter);
    if (!$result->getSuccess()) {
        echo $result->getMessage();
        return;
    }

    $data = $result->getData();
    switch ($data->getEstadoCp()) {
        case '0': echo 'NO EXISTE'; break;
        case '1': echo 'ACEPTADO'; break;
        case '2': echo 'ANULADO'; break;
        case '3': echo 'AUTORIZADO'; break;
        case '4': echo 'NO AUTORIZADO'; break;
    }

    echo PHP_EOL.'Estado RUC: '.$data->getEstadoRuc();
    echo PHP_EOL.'Condicion RUC: '.$data->getCondDomiRuc();

} catch (Exception $e) {
    echo 'Excepcion cuando invocaba ConsultaApi->consultarCpe: ', $e->getMessage(), PHP_EOL;
}

Tabla de códigos

Tipo de comprobante

Código Descripción
01 Factura
03 Boleta de venta
04 Liquidación de compra
07 Nota de crédito
08 Nota de débito
R1 Recibo por honorarios
R7 Nota de crédito de recibos

Estado del comprobante (Códigos devuelto en $data->getEstadoCp())

Código Descripción
0 NO EXISTE (Comprobante no informado)
1 ACEPTADO (Comprobante aceptado)
2 ANULADO (Comunicado en una baja)
3 AUTORIZADO (con autorización de imprenta)
4 NO AUTORIZADO (no autorizado por imprenta)

Estado del contribuyente (Códigos devuelto en $data->getEstadoRuc())

Código Descripción
00 ACTIVO
01 BAJA PROVISIONAL
02 BAJA PROV. POR OFICIO
03 SUSPENSION TEMPORAL
10 BAJA DEFINITIVA
11 BAJA DE OFICIO
22 INHABILITADO-VENT.UNICA

Condición de Domicilio del Contribuyente (Códigos devuelto en $data->getCondDomiRuc())

Código Descripción
00 HABIDO
09 PENDIENTE
11 POR VERIFICAR
12 NO HABIDO
20 NO HALLADO

FAQ

  • ¿Puedo consultar los comprobantes de cualquier RUC con el mismo client_id?
    Solo se pueden consultar los comprobantes en donde el RUC que generó el client_id, fue emisor o receptor.

Docs Models

consulta-cpe's People

Contributors

arturor72 avatar giansalex avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

consulta-cpe's Issues

La funcion $data->getCondDomiRuc(), solo devuelve "true"

La funcion $data->getCondDomiRuc(), solo devuelve "true".
En la documentación muestran otros códigos:
Condición de Domicilio del Contribuyente (Códigos devuelto en $data->getCondDomiRuc())
{ "estadoCp": "1", "estadoRuc": "00", "condDomiRuc": true }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.