97 lines
3.0 KiB
PHP
97 lines
3.0 KiB
PHP
<?php
|
|
namespace Incoviba\Service\Venta\MediosPago\Toku;
|
|
|
|
use Incoviba\Repository;
|
|
use Incoviba\Service\Venta\MediosPago\AbstractEndPoint;
|
|
use Psr\Http\Client\ClientInterface;
|
|
|
|
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]);
|
|
}
|
|
|
|
protected 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 (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;
|
|
}
|
|
}
|