Files
oficial/app/src/Service/Venta/MediosPago/Toku/Customer.php
Juan Pablo Vial abb1ce7299 Mas logging
2025-06-09 12:44:42 -04:00

133 lines
4.4 KiB
PHP

<?php
namespace Incoviba\Service\Venta\MediosPago\Toku;
use PDOException;
use Psr\Http\Client\ClientInterface;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository;
use Incoviba\Service\Venta\MediosPago\AbstractEndPoint;
class Customer extends AbstractEndPoint
{
public function __construct(ClientInterface $client,
protected Repository\Venta\MediosPago\Toku\Customer $customerRepository)
{
parent::__construct($client);
}
public function getById(string $id): array
{
return $this->doGetById([$this->customerRepository, 'fetchByRut'], $id, "No existe toku_id para Persona {$id}");
}
public function getByExternalId(string $id): array
{
return $this->doGetById([$this->customerRepository, 'fetchByTokuId'], $id, "No existe Customer para toku_id {$id}");
}
public function get(string $id): array
{
$request_uri = "/customers/{$id}";
return $this->sendGet($request_uri, [200], [404, 422]);
}
public function add(array $data): bool
{
$request_uri = "/customers";
return $this->sendAdd($request_uri, $data, [200, 201], [400, 422]);
}
public function edit(string $id, array $data): bool
{
$request_uri = "customers/{$id}";
return $this->sendEdit($request_uri, $data, [200], [400, 404, 422]);
}
public function delete(string $id): void
{
$request_uri = "/customers/{$id}";
$this->sendDelete($request_uri, [204], [404, 409]);
}
public function reset(array $skip = []): array
{
try {
$tokuIds = $this->customerRepository->fetchAllTokuIds();
$tokuIds = array_filter($tokuIds, function (string $tokuId) use ($skip) {
return !in_array($tokuId, $skip);
});
} catch (EmptyResult $exception) {
$this->logger->warning($exception);
return [];
}
$this->logger->info('Resetando ' . count($tokuIds) . ' clientes');
foreach ($tokuIds as $tokuId) {
try {
$this->delete($tokuId);
$this->customerRepository->removeByTokuId($tokuId);
} catch (EmptyResponse | PDOException $exception) {
$this->logger->warning($exception, ['customer->toku_id' => $tokuId]);
}
}
return $tokuIds;
}
public function save(array $data): bool
{
return $this->doSave($this->customerRepository, $data);
}
protected function mapParams(array $data): array
{
$paramsMap = [
'government_id' => 'rut',
'external_id' => 'rut',
'mail' => 'email',
'name' => 'nombreCompleto',
'phone_number' => 'telefono',
'pac_mandate_id' => null,
'default_agent' => 'contacto@incoviba.cl',
'send_mail' => false,
'agent_phone_number' => null,
'rfc' => null,
'tax_zip_code' => null,
'fiscal_regime' => null,
'default_receipt_type' => null,
'secondary_emails' => null,
'silenced_until' => null,
'metadata' => null
];
$params = [];
foreach ($paramsMap as $key => $ref) {
if ($ref === null) {
continue;
}
if ($ref === 'telefono') {
$value = $data[$ref];
if ($value === '' or $value === null or $value === '0') {
continue;
}
if (!str_starts_with($value, '+')) {
$value = "+56{$value}";
}
$params[$key] = $value;
continue;
}
if (array_key_exists($ref, $data) and $data[$ref] !== '' and $data[$ref] !== null) {
$params[$key] = $data[$ref];
}
}
return $params;
}
protected function mapSave(array $data): array
{
$responseMap = [
'government_id' => 'rut',
'id' => 'toku_id'
];
$mappedData = [];
foreach ($responseMap as $responseKey => $dataKey) {
if (isset($data[$responseKey])) {
$mappedData[$dataKey] = $data[$responseKey];
}
}
return $mappedData;
}
}