Reorden Toku
This commit is contained in:
96
app/src/Service/Venta/MediosPago/Toku/Customer.php
Normal file
96
app/src/Service/Venta/MediosPago/Toku/Customer.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?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)) {
|
||||
$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;
|
||||
}
|
||||
}
|
159
app/src/Service/Venta/MediosPago/Toku/Invoice.php
Normal file
159
app/src/Service/Venta/MediosPago/Toku/Invoice.php
Normal file
@ -0,0 +1,159 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Venta\MediosPago\Toku;
|
||||
|
||||
use DateMalformedStringException;
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Exception\InvalidResult;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service\UF;
|
||||
use Incoviba\Service\Venta\MediosPago\AbstractEndPoint;
|
||||
use Incoviba\Service\Venta\Pago;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
|
||||
class Invoice extends AbstractEndPoint
|
||||
{
|
||||
public function __construct(ClientInterface $client,
|
||||
protected Repository\Venta\MediosPago\Toku\Invoice $invoiceRepository,
|
||||
protected Pago $pagoService, protected UF $ufService)
|
||||
{
|
||||
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 getByExternalId(string $id): array
|
||||
{
|
||||
return $this->doGetById([$this->invoiceRepository, 'fetchByTokuId'], $id, "No existe Invoice para toku_id {$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]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $invoice_toku_id
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws InvalidResult
|
||||
*/
|
||||
public function update(string $invoice_toku_id, array $data): bool
|
||||
{
|
||||
try {
|
||||
$invoice = $this->invoiceRepository->fetchByTokuId($invoice_toku_id);
|
||||
} catch (EmptyResult $exception) {
|
||||
throw new InvalidResult("No existe Invoice para toku_id {$invoice_toku_id}", 404, $exception);
|
||||
}
|
||||
if ($data['status'] !== 'AUTHORIZED') {
|
||||
throw new InvalidResult("Pago no autorizado", 422);
|
||||
}
|
||||
try {
|
||||
$date = new DateTimeImmutable($data['transaction_date']);
|
||||
} catch (DateMalformedStringException $exception) {
|
||||
throw new InvalidResult("Fecha no válida: {$data['transaction_date']}", 422, $exception);
|
||||
}
|
||||
$uf = $this->ufService->get($date);
|
||||
if ($uf === 0.0) {
|
||||
throw new InvalidResult("No hay UF para la fecha: {$data['transaction_date']}", 422);
|
||||
}
|
||||
$valor = $data['amount'] / $uf;
|
||||
if (abs($valor - $invoice->cuota->pago->valor()) >= 0.0001) {
|
||||
throw new InvalidResult("Valor en UF no coincide: {$data['amount']}", 422);
|
||||
}
|
||||
return $this->pagoService->depositar($invoice->cuota->pago, $date);
|
||||
}
|
||||
|
||||
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(Model\Venta\Cuota $cuota): string
|
||||
{
|
||||
return json_encode([
|
||||
'Numero' => $cuota->numero,
|
||||
'valor' => $cuota->pago->valor,
|
||||
'UF' => $cuota->pago->valor()
|
||||
]);
|
||||
}
|
||||
}
|
105
app/src/Service/Venta/MediosPago/Toku/Subscription.php
Normal file
105
app/src/Service/Venta/MediosPago/Toku/Subscription.php
Normal file
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Venta\MediosPago\Toku;
|
||||
|
||||
use Incoviba\Model\Venta;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service\Venta\MediosPago\AbstractEndPoint;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
|
||||
class Subscription extends AbstractEndPoint
|
||||
{
|
||||
public function __construct(ClientInterface $client, protected Repository\Venta\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 getByExternalId(string $id): array
|
||||
{
|
||||
return $this->doGetById([$this->subscriptionRepsitory, 'fetchByTokuId'], $id, "No existe Subscription para toku_id {$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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user