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-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-10 12:38:14 -04:00
|
|
|
use Psr\Http\Client\ClientInterface;
|
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,
|
|
|
|
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-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) {
|
|
|
|
throw new InvalidResult("No existe Invoice para toku_id {$invoice_toku_id}", 404, $exception);
|
|
|
|
}
|
|
|
|
if ($data['status'] !== 'AUTHORIZED') {
|
|
|
|
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-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-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
|
|
|
}
|
|
|
|
$valor = $data['amount'] / $uf;
|
|
|
|
if (abs($valor - $invoice->cuota->pago->valor()) >= 0.0001) {
|
|
|
|
throw new InvalidResult("Valor en UF no coincide: {$data['amount']}", 422);
|
|
|
|
}
|
|
|
|
return $this->pagoService->depositar($invoice->cuota->pago, $date);
|
|
|
|
}
|
|
|
|
|
2025-05-08 16:17:26 -04:00
|
|
|
protected function save(array $data): bool
|
|
|
|
{
|
|
|
|
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',
|
|
|
|
'is_paid' => null,
|
|
|
|
'is_void' => null,
|
|
|
|
'link_payment' => null,
|
|
|
|
'metadata' => 'datosCuota',
|
|
|
|
'receipt_type' => null,
|
|
|
|
'id_receipt' => null,
|
|
|
|
'disable_automatic_payment' => null,
|
|
|
|
'currency_code' => 'CLF',
|
|
|
|
'invoice_external_id' => 'cuota_id'
|
|
|
|
];
|
|
|
|
|
|
|
|
$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-09 18:03:33 -04:00
|
|
|
$params[$key] = $data['cuota']->pago->valor();
|
2025-05-08 16:17:26 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($ref === 'datosCuota') {
|
|
|
|
$params[$key] = $this->datosCuota($data['cuota']);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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-12 14:04:58 -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
|
|
|
}
|
|
|
|
}
|