Uso de cuenta al agregar y editar para Toku

This commit is contained in:
Juan Pablo Vial
2025-07-01 17:14:53 -04:00
parent 8a043b21bc
commit 7714e25270
6 changed files with 118 additions and 65 deletions

View File

@ -76,15 +76,24 @@ abstract class AbstractEndPoint extends LoggerEnabled implements EndPoint
* @param array $data
* @param array $validStatus
* @param array $invalidStatus
* @param string|null $accountKey
* @return bool
* @throws EmptyResponse
*/
protected function sendAdd(string $request_uri, array $data, array $validStatus, array $invalidStatus): bool
protected function sendAdd(string $request_uri, array $data, array $validStatus, array $invalidStatus, ?string $accountKey = null): bool
{
$params = $this->mapParams($data);
$this->logger->info('Send Add', ['uri' => $request_uri, 'params' => $params]);
try {
$response = $this->client->post($request_uri, ['json' => $params]);
$options = [
'json' => $params
];
if ($accountKey !== null) {
$options['headers'] = [
'X-Account-Key' => $accountKey
];
}
$response = $this->client->post($request_uri, $options);
} catch (ClientExceptionInterface $exception) {
throw new EmptyResponse($request_uri, $exception);
}
@ -111,14 +120,23 @@ abstract class AbstractEndPoint extends LoggerEnabled implements EndPoint
* @param array $data
* @param array $validStatus
* @param array $invalidStatus
* @param string|null $accountKey
* @return bool
* @throws EmptyResponse
*/
protected function sendEdit(string $request_uri, array $data, array $validStatus, array $invalidStatus): bool
protected function sendEdit(string $request_uri, array $data, array $validStatus, array $invalidStatus, ?string $accountKey = null): bool
{
$params = $this->mapParams($data);
try {
$response = $this->client->put($request_uri, ['json' => $params]);
$options = [
'json' => $params
];
if ($accountKey !== null) {
$options['headers'] = [
'X-Account-Key' => $accountKey
];
}
$response = $this->client->put($request_uri, $options);
} catch (ClientExceptionInterface $exception) {
throw new EmptyResponse($request_uri, $exception);
}

View File

@ -28,18 +28,20 @@ interface EndPoint
/**
* @param array $data
* @param string|null $accountKey
* @return bool
* @throws EmptyResponse
*/
public function add(array $data): bool;
public function add(array $data, ?string $accountKey = null): bool;
/**
* @param string $id
* @param array $data
* @param string|null $accountKey
* @return bool
* @throws EmptyResponse
*/
public function edit(string $id, array $data): bool;
public function edit(string $id, array $data, ?string $accountKey = null): bool;
/**
* @param string $id

View File

@ -1,8 +1,10 @@
<?php
namespace Incoviba\Service\Venta\MediosPago;
use Incoviba\Common\Implement\Exception\EmptyResult;
use InvalidArgumentException;
use PDO;
use PDOException;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Define\Connection;
use Incoviba\Common\Ideal;
@ -80,13 +82,18 @@ class Toku extends Ideal\Service
try {
return $this->subscription->getById($venta->id);
} catch (InvalidResult $exception) {
$inmobiliaria = $venta->proyecto()->inmobiliaria();
$accountKey = null;
try {
$accountKey = $this->getAccountKey($inmobiliaria->rut);
} catch (EmptyResult) {}
$subscriptionData = [
'customer' => $customer['toku_id'],
'product_id' => $venta->id,
'venta' => $venta
];
try {
if (!$this->subscription->add($subscriptionData)) {
if (!$this->subscription->add($subscriptionData, $accountKey)) {
throw new InvalidResult("Could not save Subscription for Venta {$venta->id}", 409, $exception);
}
} catch (EmptyResponse $exception) {
@ -95,7 +102,6 @@ class Toku extends Ideal\Service
return $this->subscription->getById($venta->id);
}
}
/**
* @param Model\Venta $venta
* @param array $cuotas_ids
@ -115,6 +121,12 @@ class Toku extends Ideal\Service
});
} catch (EmptyResponse) {}
$inmobiliaria = $venta->proyecto()->inmobiliaria();
$accountKey = null;
try {
$accountKey = $this->getAccountKey($inmobiliaria->rut);
} catch (EmptyResult) {}
$invoices = [];
$errors = [];
foreach ($venta->formaPago()->pie->cuotas() as $cuota) {
@ -142,7 +154,7 @@ class Toku extends Ideal\Service
'cuota' => $cuota,
'venta' => $venta
];
if (!$this->invoice->add($invoiceData)) {
if (!$this->invoice->add($invoiceData, $accountKey)) {
throw new EmptyResponse("Could not add Invoice for Cuota {$cuota->id}", $exception);
}
$invoices []= $this->invoice->getById($cuota->id);
@ -290,6 +302,61 @@ class Toku extends Ideal\Service
return $queues;
}
/**
* @param ServerRequestInterface $request
* @param array $tokenConfig
* @return bool
*/
public function validateToken(ServerRequestInterface $request, array $tokenConfig): bool
{
if (!$request->hasHeader('User-Agent') or !str_starts_with($request->getHeaderLine('User-Agent'), 'Toku-Webhooks')) {
return false;
}
if (!$request->hasHeader('X-Datadog-Tags') or !$request->hasHeader('Tracestate')) {
return false;
}
if (!$request->hasHeader('Toku-Signature')) {
return false;
}
$tokuSignature = $request->getHeaderLine('Toku-Signature');
try {
list($timestamp, $signature) = array_map(function($elem) {
return explode('=', $elem)[1];
}, explode(',', $tokuSignature));
$body = $request->getBody()->getContents();
$json = json_decode($body, true);
if (!is_array($json)) {
return false;
}
if (!array_key_exists('id', $json)) {
return false;
}
$eventId = $json['id'];
$eventType = $json['event_type'];
$query = $this->connection->getQueryBuilder()
->select('secret')
->from('toku_webhooks')
->where('enabled = ? AND JSON_SEARCH(events, "one", ?) IS NOT NULL');
$params = [true, $eventType];
$statement = $this->connection->prepare($query);
$statement->execute($params);
$results = $statement->fetchAll(PDO::FETCH_COLUMN);
if (count($results) === 0) {
return false;
}
if (array_any($results, fn($secret) => $this->hmac->validate($timestamp, $signature, $eventId, $secret))) {
return true;
}
} catch (Throwable $throwable) {
$this->logger->error($throwable);
}
return false;
}
/**
* @param array $request
* @return bool
@ -406,54 +473,20 @@ class Toku extends Ideal\Service
$data['date'] = $data['transaction_date'];
return $data;
}
public function validateToken(ServerRequestInterface $request, array $tokenConfig): bool
protected function getAccountKey(int $sociedad_rut): string
{
if (!$request->hasHeader('User-Agent') or !str_starts_with($request->getHeaderLine('User-Agent'), 'Toku-Webhooks')) {
return false;
}
if (!$request->hasHeader('X-Datadog-Tags') or !$request->hasHeader('Tracestate')) {
return false;
}
if (!$request->hasHeader('Toku-Signature')) {
return false;
}
$tokuSignature = $request->getHeaderLine('Toku-Signature');
$query = $this->connection->getQueryBuilder()
->select('account_key')
->from('toku_accounts')
->where('enabled = ? AND sociedad_rut = ?');
$params = [true, $sociedad_rut];
try {
list($timestamp, $signature) = array_map(function($elem) {
return explode('=', $elem)[1];
}, explode(',', $tokuSignature));
$body = $request->getBody()->getContents();
$json = json_decode($body, true);
if (!is_array($json)) {
return false;
}
if (!array_key_exists('id', $json)) {
return false;
}
$eventId = $json['id'];
$eventType = $json['event_type'];
$query = $this->connection->getQueryBuilder()
->select('secret')
->from('toku_webhooks')
->where('enabled = ? AND JSON_SEARCH(events, "one", ?) IS NOT NULL');
$params = [true, $eventType];
$statement = $this->connection->prepare($query);
$statement->execute($params);
$results = $statement->fetchAll(PDO::FETCH_COLUMN);
if (count($results) === 0) {
return false;
}
if (array_any($results, fn($secret) => $this->hmac->validate($timestamp, $signature, $eventId, $secret))) {
return true;
}
} catch (Throwable $throwable) {
$this->logger->error($throwable);
return $statement->fetchColumn();
} catch (PDOException $exception) {
$this->logger->error($exception);
throw new EmptyResult($query, $exception);
}
return false;
}
}

View File

@ -29,15 +29,15 @@ class Customer extends AbstractEndPoint
$request_uri = "/customers/{$id}";
return $this->sendGet($request_uri, [200], [404, 422]);
}
public function add(array $data): bool
public function add(array $data, ?string $accountKey = null): bool
{
$request_uri = "/customers";
return $this->sendAdd($request_uri, $data, [200, 201], [400, 422]);
return $this->sendAdd($request_uri, $data, [200, 201], [400, 422], $accountKey);
}
public function edit(string $id, array $data): bool
public function edit(string $id, array $data, ?string $accountKey = null): bool
{
$request_uri = "customers/{$id}";
return $this->sendEdit($request_uri, $data, [200], [400, 404, 422]);
return $this->sendEdit($request_uri, $data, [200], [400, 404, 422], $accountKey);
}
public function delete(string $id): void
{

View File

@ -39,15 +39,15 @@ class Invoice extends AbstractEndPoint
$request_uri = "/invoices/{$id}";
return $this->sendGet($request_uri, [200], [404]);
}
public function add(array $data): bool
public function add(array $data, ?string $accountKey = null): bool
{
$request_uri = "/invoices";
return $this->sendAdd($request_uri, $data, [200, 201], [400, 409, 422]);
return $this->sendAdd($request_uri, $data, [200, 201], [400, 409, 422], $accountKey);
}
public function edit(string $id, array $data): bool
public function edit(string $id, array $data, ?string $accountKey = null): bool
{
$request_uri = "/invoices/{$id}";
return $this->sendEdit($request_uri, $data, [200], [400, 404, 409, 422]);
return $this->sendEdit($request_uri, $data, [200], [400, 404, 409, 422], $accountKey);
}
public function delete(string $id): void
{

View File

@ -34,15 +34,15 @@ class Subscription extends AbstractEndPoint
$request_uri = "/subscriptions/{$id}";
return $this->sendGet($request_uri, [200], [401, 404, 422]);
}
public function add(array $data): bool
public function add(array $data, ?string $accountKey = null): bool
{
$request_uri = '/subscriptions';
return $this->sendAdd($request_uri, $data, [200, 201], [401, 404, 409, 422]);
return $this->sendAdd($request_uri, $data, [200, 201], [401, 404, 409, 422], $accountKey);
}
public function edit(string $id, array $data): bool
public function edit(string $id, array $data, ?string $accountKey = null): bool
{
$request_uri = "/subscriptions/{$id}";
return $this->sendEdit($request_uri, $data, [200], [401, 404, 409, 422]);
return $this->sendEdit($request_uri, $data, [200], [401, 404, 409, 422], $accountKey);
}
public function delete(string $id): void
{