131 lines
4.3 KiB
PHP
131 lines
4.3 KiB
PHP
<?php
|
|
namespace Incoviba\Service\Venta\MediosPago\Toku;
|
|
|
|
use Psr\Http\Client\ClientInterface;
|
|
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
|
use Incoviba\Common\Implement\Exception\EmptyResult;
|
|
use Incoviba\Model;
|
|
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 = "/customer/{$id}";
|
|
$this->sendDelete($request_uri, [204], [404, 409]);
|
|
}
|
|
public function reset(array $skip = []): array
|
|
{
|
|
try {
|
|
$customers = $this->customerRepository->fetchAll();
|
|
$customers = array_filter($customers, function (Model\Venta\MediosPago\Toku\Customer $customer) use ($skip) {
|
|
return !in_array($customer->toku_id, $skip);
|
|
});
|
|
} catch (EmptyResult $exception) {
|
|
$this->logger->warning($exception);
|
|
return [];
|
|
}
|
|
foreach ($customers as $customer) {
|
|
try {
|
|
$this->delete($customer->toku_id);
|
|
} catch (EmptyResponse $exception) {
|
|
$this->logger->warning($exception, ['customer' => $customer]);
|
|
}
|
|
}
|
|
return $customers;
|
|
}
|
|
|
|
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',
|
|
'email' => '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;
|
|
}
|
|
}
|