Servicio Toku para enviar datos

This commit is contained in:
Juan Pablo Vial
2025-05-08 17:17:49 -04:00
parent 26b6862955
commit 679b401101
2 changed files with 363 additions and 0 deletions

View File

@ -0,0 +1,119 @@
<?php
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\Model;
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;
}
/**
* @param Model\Persona $persona
* @return array
* @throws EmptyResponse
*/
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) {
$datos = $persona->datos;
$customerData = [
'rut' => $rut,
'nombreCompleto' => $persona->nombreCompleto(),
'email' => $datos?->email ?? '',
'telefono' => $datos?->telefono ?? ''
];
if (!$this->customer->add($customerData)) {
throw new EmptyResponse("Could not save Customer for Persona {$rut}", $exception);
}
return $this->customer->getById($rut);
}
}
/**
* @param Model\Venta $venta
* @return bool
* @throws EmptyResponse
*/
public function sendVenta(Model\Venta $venta): array
{
$customer = $this->sendPersona($venta->propietario());
try {
return $this->subscription->getById($venta->id);
} catch (EmptyResponse $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);
}
return $this->subscription->getById($venta->id);
}
}
/**
* @param Model\Venta $venta
* @return array
* @throws EmptyResponse
*/
public function sendCuotas(Model\Venta $venta): array
{
$customer = $this->sendPersona($venta->propietario());
$subscription = $this->sendVenta($venta);
$invoices = [];
$errors = [];
foreach ($venta->formaPago()->pie->cuotas() as $cuota) {
try {
$invoices []= $this->invoice->getById($cuota->id);
} catch (EmptyResponse $exception) {
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;
}
}