Servicio Toku para enviar datos
This commit is contained in:
119
app/src/Service/MediosPago/Toku.php
Normal file
119
app/src/Service/MediosPago/Toku.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user