FIX: Cambios a Servicios Toku

This commit is contained in:
Juan Pablo Vial
2025-05-09 18:03:33 -04:00
parent 400b2754bf
commit 8d73987ac3
6 changed files with 111 additions and 21 deletions

View File

@ -2,10 +2,12 @@
namespace Incoviba\Service\MediosPago;
use InvalidArgumentException;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Exception\InvalidResult;
use Incoviba\Model\Persona;
use Incoviba\Model\Venta\Propietario;
use Incoviba\Model;
class Toku extends Ideal\Service
@ -32,16 +34,16 @@ class Toku extends Ideal\Service
}
/**
* @param Model\Persona $persona
* @param Persona|Propietario $persona
* @return array
* @throws EmptyResponse
* @throws InvalidResult
*/
public function sendPersona(Model\Persona|Model\Venta\Propietario $persona): array
{
$rut = implode('', [$persona->rut, strtoupper($persona->dv)]);
try {
return $this->customer->getById($rut);
} catch (EmptyResponse $exception) {
} catch (InvalidResult $exception) {
$datos = $persona->datos;
$customerData = [
'rut' => $rut,
@ -49,30 +51,38 @@ class Toku extends Ideal\Service
'email' => $datos?->email ?? '',
'telefono' => $datos?->telefono ?? ''
];
if (!$this->customer->add($customerData)) {
throw new EmptyResponse("Could not save Customer for Persona {$rut}", $exception);
try {
if (!$this->customer->add($customerData)) {
throw new InvalidResult("Could not save Customer for Persona {$rut}", 409, $exception);
}
} catch (EmptyResponse $exception) {
throw new InvalidResult("Could not save Customer for Persona {$rut}", 409, $exception);
}
return $this->customer->getById($rut);
}
}
/**
* @param Model\Venta $venta
* @return bool
* @throws EmptyResponse
* @return array
* @throws InvalidResult
*/
public function sendVenta(Model\Venta $venta): array
{
$customer = $this->sendPersona($venta->propietario());
try {
return $this->subscription->getById($venta->id);
} catch (EmptyResponse $exception) {
} catch (InvalidResult $exception) {
$subscriptionData = [
'customer' => $customer['toku_id'],
'product_id' => $venta->id,
'venta' => $venta
];
if (!$this->subscription->add($subscriptionData)) {
throw new EmptyResponse("Could not save Subscription for Venta {$venta->id}", $exception);
try {
if (!$this->subscription->add($subscriptionData)) {
throw new InvalidResult("Could not save Subscription for Venta {$venta->id}", 409, $exception);
}
} catch (EmptyResponse $exception) {
throw new InvalidResult("Could not save Subscription for Venta {$venta->id}", 409, $exception);
}
return $this->subscription->getById($venta->id);
}
@ -80,10 +90,11 @@ class Toku extends Ideal\Service
/**
* @param Model\Venta $venta
* @param array $cuotas_ids
* @return array
* @throws EmptyResponse
* @throws InvalidResult
*/
public function sendCuotas(Model\Venta $venta): array
public function sendCuotas(Model\Venta $venta, array $cuotas_ids = []): array
{
$customer = $this->sendPersona($venta->propietario());
$subscription = $this->sendVenta($venta);
@ -91,9 +102,12 @@ class Toku extends Ideal\Service
$invoices = [];
$errors = [];
foreach ($venta->formaPago()->pie->cuotas() as $cuota) {
if (count($cuotas_ids) > 0 and !in_array($cuota->id, $cuotas_ids)) {
continue;
}
try {
$invoices []= $this->invoice->getById($cuota->id);
} catch (EmptyResponse $exception) {
} catch (InvalidResult $exception) {
try {
$invoiceData = [
'customer' => $customer['toku_id'],
@ -116,4 +130,18 @@ class Toku extends Ideal\Service
}
return $invoices;
}
/**
* @param array $request
* @return bool
* @throws InvalidResult
*/
public function updatePago(array $request): bool
{
# If $customer is not found, it will throw an exception and stop
$customer = $this->customer->getByExternalId($request['customer']);
$invoice = $this->invoice->getByExternalId($request['invoice']);
return $this->invoice->update($invoice['id'], $request);
}
}