2025-05-08 16:17:26 -04:00
< ? php
2025-05-10 12:38:14 -04:00
namespace Incoviba\Service\Venta\MediosPago\Toku ;
2025-05-08 16:17:26 -04:00
2025-05-09 18:03:33 -04:00
use DateMalformedStringException ;
2025-05-10 12:38:14 -04:00
use DateTimeImmutable ;
2025-05-30 17:10:59 -04:00
use DateTimeZone ;
2025-05-16 14:36:20 -04:00
use Psr\Http\Client\ClientInterface ;
use Incoviba\Common\Implement\Exception\EmptyResponse ;
2025-05-09 18:03:33 -04:00
use Incoviba\Common\Implement\Exception\EmptyResult ;
2025-05-10 12:38:14 -04:00
use Incoviba\Exception\InvalidResult ;
2025-05-09 18:03:33 -04:00
use Incoviba\Model ;
2025-05-08 16:17:26 -04:00
use Incoviba\Repository ;
2025-05-09 18:03:33 -04:00
use Incoviba\Service\UF ;
2025-05-10 12:38:14 -04:00
use Incoviba\Service\Venta\MediosPago\AbstractEndPoint ;
2025-05-09 18:03:33 -04:00
use Incoviba\Service\Venta\Pago ;
2025-05-08 16:17:26 -04:00
class Invoice extends AbstractEndPoint
{
2025-05-10 12:38:14 -04:00
public function __construct ( ClientInterface $client ,
protected Repository\Venta\MediosPago\Toku\Invoice $invoiceRepository ,
2025-05-16 19:14:20 -04:00
protected Pago $pagoService ,
protected UF $ufService )
2025-05-08 16:17:26 -04:00
{
parent :: __construct ( $client );
}
public function getById ( string $id ) : array
{
return $this -> doGetById ([ $this -> invoiceRepository , 'fetchByCuota' ], $id , " No existe toku_id para Cuota { $id } " );
}
2025-05-09 18:03:33 -04:00
public function getByExternalId ( string $id ) : array
{
return $this -> doGetById ([ $this -> invoiceRepository , 'fetchByTokuId' ], $id , " No existe Invoice para toku_id { $id } " );
}
2025-05-08 16:17:26 -04:00
public function get ( string $id ) : array
{
$request_uri = " /invoices/ { $id } " ;
return $this -> sendGet ( $request_uri , [ 200 ], [ 404 ]);
}
public function add ( array $data ) : bool
{
$request_uri = " /invoices " ;
return $this -> sendAdd ( $request_uri , $data , [ 200 , 201 ], [ 400 , 409 , 422 ]);
}
public function edit ( string $id , array $data ) : bool
{
$request_uri = " /invoices/ { $id } " ;
return $this -> sendEdit ( $request_uri , $data , [ 200 ], [ 400 , 404 , 409 , 422 ]);
}
public function delete ( string $id ) : void
{
$request_uri = " /invoices/ { $id } " ;
$this -> sendDelete ( $request_uri , [ 204 ], [ 404 , 409 ]);
}
2025-05-27 18:17:56 -04:00
public function reset ( array $skip = []) : array
{
try {
$invoices = $this -> invoiceRepository -> fetchAll ();
$invoices = array_filter ( $invoices , function ( Model\Venta\MediosPago\Toku\Invoice $invoice ) use ( $skip ) {
return ! in_array ( $invoice -> toku_id , $skip );
});
} catch ( EmptyResult $exception ) {
$this -> logger -> warning ( $exception );
return [];
}
foreach ( $invoices as $invoice ) {
try {
$this -> delete ( $invoice -> toku_id );
2025-05-27 18:35:06 -04:00
$this -> invoiceRepository -> remove ( $invoice );
2025-05-27 18:17:56 -04:00
} catch ( EmptyResponse $exception ) {
$this -> logger -> warning ( $exception , [ 'invoice' => $invoice ]);
}
}
return $invoices ;
}
2025-05-08 16:17:26 -04:00
2025-05-16 14:36:20 -04:00
/**
* @ param string $customer_id
* @ return array
* @ throws EmptyResponse
*/
public function getByCustomer ( string $customer_id ) : array
{
$request_uri = " /invoices/customer/ { $customer_id } " ;
return $this -> sendGet ( $request_uri , [ 200 ], [ 404 ]);
}
2025-05-09 18:03:33 -04:00
/**
* @ param string $invoice_toku_id
* @ param array $data
* @ return bool
* @ throws InvalidResult
*/
public function update ( string $invoice_toku_id , array $data ) : bool
{
try {
$invoice = $this -> invoiceRepository -> fetchByTokuId ( $invoice_toku_id );
} catch ( EmptyResult $exception ) {
2025-05-19 16:06:44 -04:00
$this -> logger -> warning ( $exception , [ 'invoice_toku_id' => $invoice_toku_id , 'data' => $data ]);
2025-05-09 18:03:33 -04:00
throw new InvalidResult ( " No existe Invoice para toku_id { $invoice_toku_id } " , 404 , $exception );
}
if ( $data [ 'status' ] !== 'AUTHORIZED' ) {
2025-05-19 16:06:44 -04:00
$this -> logger -> warning ( " Pago no autorizado " , [ 'invoice_toku_id' => $invoice_toku_id , 'data' => $data ]);
2025-05-09 18:03:33 -04:00
throw new InvalidResult ( " Pago no autorizado " , 422 );
}
2025-05-12 19:46:09 -04:00
$dateString = $data [ 'date' ];
2025-05-09 18:03:33 -04:00
try {
2025-05-12 19:46:09 -04:00
$date = new DateTimeImmutable ( $dateString );
2025-05-09 18:03:33 -04:00
} catch ( DateMalformedStringException $exception ) {
2025-05-19 16:06:44 -04:00
$this -> logger -> warning ( $exception , [ 'invoice_toku_id' => $invoice_toku_id , 'data' => $data ]);
2025-05-12 19:46:09 -04:00
throw new InvalidResult ( " Fecha no válida: { $dateString } " , 422 , $exception );
2025-05-09 18:03:33 -04:00
}
$uf = $this -> ufService -> get ( $date );
if ( $uf === 0.0 ) {
2025-05-19 16:06:44 -04:00
$this -> logger -> warning ( " No hay UF para la fecha: { $dateString } " , [ 'invoice_toku_id' => $invoice_toku_id , 'data' => $data ]);
2025-05-12 19:46:09 -04:00
throw new InvalidResult ( " No hay UF para la fecha: { $dateString } " , 422 );
2025-05-09 18:03:33 -04:00
}
2025-05-15 10:12:34 -04:00
$valor = $data [ 'amount' ];
if ( $valor > 1000 ) {
$valor = $data [ 'amount' ] / $uf ;
}
2025-05-09 18:03:33 -04:00
if ( abs ( $valor - $invoice -> cuota -> pago -> valor ()) >= 0.0001 ) {
2025-05-19 16:06:44 -04:00
$this -> logger -> warning ( " Valor en UF no coincide: { $data [ 'amount' ] } , { $valor } <=> { $invoice -> cuota -> pago -> valor () } " , [ 'invoice_toku_id' => $invoice_toku_id , 'data' => $data ]);
2025-05-15 10:12:34 -04:00
throw new InvalidResult ( " Valor en UF no coincide: { $data [ 'amount' ] } , { $valor } <=> { $invoice -> cuota -> pago -> valor () } " , 422 );
}
if ( $invoice -> cuota -> pago -> isPagado ()) {
return true ;
2025-05-09 18:03:33 -04:00
}
return $this -> pagoService -> depositar ( $invoice -> cuota -> pago , $date );
}
2025-05-16 14:36:20 -04:00
public function save ( array $data ) : bool
2025-05-08 16:17:26 -04:00
{
return $this -> doSave ( $this -> invoiceRepository , $data );
}
protected function mapParams ( array $data ) : array
{
$paramsMap = [
'customer' => 'customer' ,
'product_id' => 'product_id' ,
'due_date' => 'fecha' ,
'subscription' => 'subscription' ,
'amount' => 'valor' ,
2025-05-13 20:17:53 -04:00
'is_paid' => 'isPagada' ,
'is_void' => 'isRechazada' ,
2025-05-08 16:17:26 -04:00
'link_payment' => null ,
'metadata' => 'datosCuota' ,
'receipt_type' => null ,
'id_receipt' => null ,
'disable_automatic_payment' => null ,
'currency_code' => 'CLF' ,
'invoice_external_id' => 'cuota_id'
];
2025-05-30 17:10:59 -04:00
$today = new DateTimeImmutable ( 'now' , new DateTimeZone ( 'America/Santiago' ));
2025-05-08 16:17:26 -04:00
$params = [];
foreach ( $paramsMap as $key => $ref ) {
if ( $ref === null ) {
continue ;
}
if ( $ref === 'fecha' ) {
$params [ $key ] = $data [ 'cuota' ] -> pago -> fecha -> format ( 'Y-m-d' );
continue ;
}
if ( $ref === 'valor' ) {
2025-05-30 17:10:59 -04:00
$valor = 0 ;
if ( $data [ 'cuota' ] -> pago -> fecha <= $today ) {
$valor = $data [ 'cuota' ] -> pago -> valor ();
}
2025-05-16 14:36:20 -04:00
if ( $valor === 0 ) {
$valor = $data [ 'cuota' ] -> pago -> valor / $data [ 'venta' ] -> uf ;
}
$params [ $key ] = $valor ;
2025-05-08 16:17:26 -04:00
continue ;
}
if ( $ref === 'datosCuota' ) {
$params [ $key ] = $this -> datosCuota ( $data [ 'cuota' ]);
continue ;
}
2025-05-13 20:17:53 -04:00
if ( $ref === 'isPagada' ) {
$params [ $key ] = $data [ 'cuota' ] -> isPagada ();
}
if ( $ref === 'isRechazada' ) {
$params [ $key ] = $data [ 'cuota' ] -> isRechazada ();
}
2025-05-08 16:17:26 -04:00
if ( $ref === 'cuota_id' ) {
$params [ $key ] = $data [ 'cuota' ] -> id ;
continue ;
}
2025-05-13 20:12:26 -04:00
if ( array_key_exists ( $ref , $data ) and $data [ $ref ] !== '' and $data [ $ref ] !== null ) {
2025-05-08 16:17:26 -04:00
$params [ $key ] = $data [ $ref ];
}
}
return $params ;
}
protected function mapSave ( array $data ) : array
{
$responseMap = [
'invoice_external_id' => 'cuota_id' ,
'id' => 'toku_id'
];
$mappedData = [];
foreach ( $responseMap as $responseKey => $dataKey ) {
if ( isset ( $data [ $responseKey ])) {
$mappedData [ $dataKey ] = $data [ $responseKey ];
}
}
return $mappedData ;
}
2025-05-13 20:12:26 -04:00
protected function datosCuota ( Model\Venta\Cuota $cuota ) : array
2025-05-08 16:17:26 -04:00
{
2025-05-13 20:12:26 -04:00
return [
2025-05-27 18:27:37 -04:00
'Numero' => $cuota -> numero ,
'Monto_CLP' => $cuota -> pago -> valor
2025-05-13 20:12:26 -04:00
];
2025-05-08 16:17:26 -04:00
}
}