2025-05-08 17:17:49 -04:00
|
|
|
<?php
|
2025-05-10 12:38:14 -04:00
|
|
|
namespace Incoviba\Service\Venta\MediosPago;
|
2025-05-08 17:17:49 -04:00
|
|
|
|
|
|
|
use Incoviba\Common\Ideal;
|
|
|
|
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
2025-05-09 18:03:33 -04:00
|
|
|
use Incoviba\Exception\InvalidResult;
|
2025-05-10 12:38:14 -04:00
|
|
|
use Incoviba\Model;
|
2025-05-09 18:03:33 -04:00
|
|
|
use Incoviba\Model\Persona;
|
|
|
|
use Incoviba\Model\Venta\Propietario;
|
2025-05-10 12:38:14 -04:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2025-05-08 17:17:49 -04:00
|
|
|
|
|
|
|
class Toku extends Ideal\Service
|
|
|
|
{
|
|
|
|
public function __construct(LoggerInterface $logger)
|
|
|
|
{
|
|
|
|
parent::__construct($logger);
|
|
|
|
}
|
|
|
|
|
|
|
|
const string CUSTOMER = 'customer';
|
|
|
|
const string SUBSCRIPTION = 'subscription';
|
|
|
|
const string INVOICE = 'invoice';
|
|
|
|
|
|
|
|
protected EndPoint $customer;
|
|
|
|
protected EndPoint $subscription;
|
|
|
|
protected EndPoint $invoice;
|
|
|
|
public function register(string $type, EndPoint $endPoint): self
|
|
|
|
{
|
|
|
|
if (!in_array(strtolower($type), ['customer', 'subscription', 'invoice'])) {
|
|
|
|
throw new InvalidArgumentException("{$type} is not a valid type of EndPoint for " . __CLASS__);
|
|
|
|
}
|
|
|
|
$this->{strtolower($type)} = $endPoint;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-05-09 18:03:33 -04:00
|
|
|
* @param Persona|Propietario $persona
|
2025-05-08 17:17:49 -04:00
|
|
|
* @return array
|
2025-05-09 18:03:33 -04:00
|
|
|
* @throws InvalidResult
|
2025-05-08 17:17:49 -04:00
|
|
|
*/
|
|
|
|
public function sendPersona(Model\Persona|Model\Venta\Propietario $persona): array
|
|
|
|
{
|
|
|
|
$rut = implode('', [$persona->rut, strtoupper($persona->dv)]);
|
|
|
|
try {
|
|
|
|
return $this->customer->getById($rut);
|
2025-05-09 18:03:33 -04:00
|
|
|
} catch (InvalidResult $exception) {
|
2025-05-08 17:17:49 -04:00
|
|
|
$datos = $persona->datos;
|
|
|
|
$customerData = [
|
|
|
|
'rut' => $rut,
|
|
|
|
'nombreCompleto' => $persona->nombreCompleto(),
|
|
|
|
'email' => $datos?->email ?? '',
|
|
|
|
'telefono' => $datos?->telefono ?? ''
|
|
|
|
];
|
2025-05-09 18:03:33 -04:00
|
|
|
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);
|
2025-05-08 17:17:49 -04:00
|
|
|
}
|
|
|
|
return $this->customer->getById($rut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Model\Venta $venta
|
2025-05-09 18:03:33 -04:00
|
|
|
* @return array
|
|
|
|
* @throws InvalidResult
|
2025-05-08 17:17:49 -04:00
|
|
|
*/
|
|
|
|
public function sendVenta(Model\Venta $venta): array
|
|
|
|
{
|
|
|
|
$customer = $this->sendPersona($venta->propietario());
|
|
|
|
try {
|
|
|
|
return $this->subscription->getById($venta->id);
|
2025-05-09 18:03:33 -04:00
|
|
|
} catch (InvalidResult $exception) {
|
2025-05-08 17:17:49 -04:00
|
|
|
$subscriptionData = [
|
|
|
|
'customer' => $customer['toku_id'],
|
|
|
|
'product_id' => $venta->id,
|
|
|
|
'venta' => $venta
|
|
|
|
];
|
2025-05-09 18:03:33 -04:00
|
|
|
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);
|
2025-05-08 17:17:49 -04:00
|
|
|
}
|
|
|
|
return $this->subscription->getById($venta->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Model\Venta $venta
|
2025-05-09 18:03:33 -04:00
|
|
|
* @param array $cuotas_ids
|
2025-05-08 17:17:49 -04:00
|
|
|
* @return array
|
2025-05-09 18:03:33 -04:00
|
|
|
* @throws InvalidResult
|
2025-05-08 17:17:49 -04:00
|
|
|
*/
|
2025-05-09 18:03:33 -04:00
|
|
|
public function sendCuotas(Model\Venta $venta, array $cuotas_ids = []): array
|
2025-05-08 17:17:49 -04:00
|
|
|
{
|
|
|
|
$customer = $this->sendPersona($venta->propietario());
|
|
|
|
$subscription = $this->sendVenta($venta);
|
|
|
|
|
|
|
|
$invoices = [];
|
|
|
|
$errors = [];
|
|
|
|
foreach ($venta->formaPago()->pie->cuotas() as $cuota) {
|
2025-05-09 18:03:33 -04:00
|
|
|
if (count($cuotas_ids) > 0 and !in_array($cuota->id, $cuotas_ids)) {
|
|
|
|
continue;
|
|
|
|
}
|
2025-05-08 17:17:49 -04:00
|
|
|
try {
|
|
|
|
$invoices []= $this->invoice->getById($cuota->id);
|
2025-05-09 18:03:33 -04:00
|
|
|
} catch (InvalidResult $exception) {
|
2025-05-08 17:17:49 -04:00
|
|
|
try {
|
|
|
|
$invoiceData = [
|
|
|
|
'customer' => $customer['toku_id'],
|
|
|
|
'product_id' => $venta->id,
|
|
|
|
'subscription' => $subscription['toku_id'],
|
|
|
|
'cuota' => $cuota
|
|
|
|
];
|
|
|
|
if (!$this->invoice->add($invoiceData)) {
|
|
|
|
throw new EmptyResponse("Could not add Invoice for Cuota {$cuota->id}", $exception);
|
|
|
|
}
|
|
|
|
$invoices []= $this->invoice->getById($cuota->id);
|
|
|
|
} catch (EmptyResponse $exception) {
|
|
|
|
$this->logger->warning($exception);
|
|
|
|
$errors []= $exception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count($errors) > 0) {
|
|
|
|
$this->logger->warning("Revisar el envío de cuotas de la Venta {$venta->id}");
|
|
|
|
}
|
|
|
|
return $invoices;
|
|
|
|
}
|
2025-05-09 18:03:33 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
2025-05-08 17:17:49 -04:00
|
|
|
}
|