Servicios para Toku

This commit is contained in:
Juan Pablo Vial
2025-05-08 16:17:26 -04:00
parent 5ad4fc4038
commit a5c1d60819
6 changed files with 872 additions and 0 deletions

View File

@ -0,0 +1,92 @@
<?php
namespace Incoviba\Service\MediosPago\Toku;
use Psr\Http\Client\ClientInterface;
use Incoviba\Repository;
use Incoviba\Service\MediosPago\AbstractEndPoint;
class Customer extends AbstractEndPoint
{
public function __construct(ClientInterface $client,
protected Repository\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 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)) {
$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;
}
}

View File

@ -0,0 +1,115 @@
<?php
namespace Incoviba\Service\MediosPago\Toku;
use Incoviba\Model\Venta\Cuota;
use Psr\Http\Client\ClientInterface;
use Incoviba\Repository;
use Incoviba\Service\MediosPago\AbstractEndPoint;
class Invoice extends AbstractEndPoint
{
public function __construct(ClientInterface $client, protected Repository\MediosPago\Toku\Invoice $invoiceRepository)
{
parent::__construct($client);
}
public function getById(string $id): array
{
return $this->doGetById([$this->invoiceRepository, 'fetchByCuota'], $id, "No existe toku_id para Cuota {$id}");
}
public function get(string $id): array
{
$request_uri = "/invoices/{$id}";
return $this->sendGet($request_uri, [200], [404]);
}
public function add(array $data): bool
{
$request_uri = "/invoices";
return $this->sendAdd($request_uri, $data, [200, 201], [400, 409, 422]);
}
public function edit(string $id, array $data): bool
{
$request_uri = "/invoices/{$id}";
return $this->sendEdit($request_uri, $data, [200], [400, 404, 409, 422]);
}
public function delete(string $id): void
{
$request_uri = "/invoices/{$id}";
$this->sendDelete($request_uri, [204], [404, 409]);
}
protected function save(array $data): bool
{
return $this->doSave($this->invoiceRepository, $data);
}
protected function mapParams(array $data): array
{
$paramsMap = [
'customer' => 'customer',
'product_id' => 'product_id',
'due_date' => 'fecha',
'subscription' => 'subscription',
'amount' => 'valor',
'is_paid' => null,
'is_void' => null,
'link_payment' => null,
'metadata' => 'datosCuota',
'receipt_type' => null,
'id_receipt' => null,
'disable_automatic_payment' => null,
'currency_code' => 'CLF',
'invoice_external_id' => 'cuota_id'
];
$params = [];
foreach ($paramsMap as $key => $ref) {
if ($ref === null) {
continue;
}
if ($ref === 'fecha') {
$params[$key] = $data['cuota']->pago->fecha->format('Y-m-d');
continue;
}
if ($ref === 'valor') {
$params[$key] = $data['cuota']->pago->valor;
continue;
}
if ($ref === 'datosCuota') {
$params[$key] = $this->datosCuota($data['cuota']);
continue;
}
if ($ref === 'cuota_id') {
$params[$key] = $data['cuota']->id;
continue;
}
if (array_key_exists($ref, $data)) {
$params[$key] = $data[$ref];
}
}
return $params;
}
protected function mapSave(array $data): array
{
$responseMap = [
'invoice_external_id' => 'cuota_id',
'id' => 'toku_id'
];
$mappedData = [];
foreach ($responseMap as $responseKey => $dataKey) {
if (isset($data[$responseKey])) {
$mappedData[$dataKey] = $data[$responseKey];
}
}
return $mappedData;
}
protected function datosCuota(Cuota $cuota): string
{
return json_encode([
'Numero' => $cuota->numero,
'valor' => $cuota->pago->valor,
'UF' => $cuota->pago->valor()
]);
}
}

View File

@ -0,0 +1,100 @@
<?php
namespace Incoviba\Service\MediosPago\Toku;
use Psr\Http\Client\ClientInterface;
use Incoviba\Model\Venta;
use Incoviba\Repository;
use Incoviba\Service\MediosPago\AbstractEndPoint;
class Subscription extends AbstractEndPoint
{
public function __construct(ClientInterface $client, protected Repository\MediosPago\Toku\Subscription $subscriptionRepsitory)
{
parent::__construct($client);
}
public function getById(string $id): array
{
return $this->doGetById([$this->subscriptionRepsitory, 'fetchByVenta'], $id, "No existe toku_id para Venta {$id}");
}
public function get(string $id): array
{
$request_uri = "/subscriptions/{$id}";
return $this->sendGet($request_uri, [200], [401, 404, 422]);
}
public function add(array $data): bool
{
$request_uri = '/subscriptions';
return $this->sendAdd($request_uri, $data, [200, 201], [401, 404, 409, 422]);
}
public function edit(string $id, array $data): bool
{
$request_uri = "/subscriptions/{$id}";
return $this->sendEdit($request_uri, $data, [200], [401, 404, 409, 422]);
}
public function delete(string $id): void
{
$request_uri = "/subscriptions/{$id}";
$this->sendDelete($request_uri, [204], [404, 409]);
}
protected function save(array $data): bool
{
return $this->doSave($this->subscriptionRepsitory, $data);
}
protected function mapParams(array $data): array
{
$paramsMap = [
'customer' => 'customer',
'product_id' => 'product_id',
'pac_mandate_id' => null,
'is_recurring' => null,
'due_day' => null,
'amount' => 'pieValor',
'receipt_product_code' => null,
'metadata' => 'datosVenta'
];
$params = [];
foreach ($paramsMap as $key => $ref) {
if ($ref === null) {
continue;
}
if ($ref === 'pieValor') {
$params[$key] = $data['venta']->formaPago()?->pie?->valor ?? 0;
continue;
}
if ($ref === 'datosVenta') {
$params[$key] = $this->datosVenta($data['venta']);
continue;
}
if (array_key_exists($ref, $data)) {
$params[$key] = $data[$ref];
}
}
return $params;
}
protected function mapSave(array $data): array
{
$responseMap = [
'product_id' => 'venta_id',
'id' => 'toku_id'
];
$mappedData = [];
foreach ($responseMap as $responseKey => $dataKey) {
if (isset($data[$responseKey])) {
$mappedData[$dataKey] = $data[$responseKey];
}
}
return $mappedData;
}
protected function datosVenta(Venta $venta): string
{
$datos = [
'proyecto' => $venta->proyecto()->descripcion,
'propiedad' => $venta->propiedad()->summary()
];
return json_encode($datos);
}
}