116 lines
3.5 KiB
PHP
116 lines
3.5 KiB
PHP
![]() |
<?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()
|
||
|
]);
|
||
|
}
|
||
|
}
|