From 7714e252703ea0a67821b7b43025f0dd642ca5fa Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Tue, 1 Jul 2025 17:14:53 -0400 Subject: [PATCH] Uso de cuenta al agregar y editar para Toku --- .../Venta/MediosPago/AbstractEndPoint.php | 26 +++- app/src/Service/Venta/MediosPago/EndPoint.php | 6 +- app/src/Service/Venta/MediosPago/Toku.php | 127 +++++++++++------- .../Venta/MediosPago/Toku/Customer.php | 8 +- .../Service/Venta/MediosPago/Toku/Invoice.php | 8 +- .../Venta/MediosPago/Toku/Subscription.php | 8 +- 6 files changed, 118 insertions(+), 65 deletions(-) diff --git a/app/src/Service/Venta/MediosPago/AbstractEndPoint.php b/app/src/Service/Venta/MediosPago/AbstractEndPoint.php index 791e0c3..fc1ac79 100644 --- a/app/src/Service/Venta/MediosPago/AbstractEndPoint.php +++ b/app/src/Service/Venta/MediosPago/AbstractEndPoint.php @@ -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); } diff --git a/app/src/Service/Venta/MediosPago/EndPoint.php b/app/src/Service/Venta/MediosPago/EndPoint.php index 0b49eca..f6cbeaa 100644 --- a/app/src/Service/Venta/MediosPago/EndPoint.php +++ b/app/src/Service/Venta/MediosPago/EndPoint.php @@ -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 diff --git a/app/src/Service/Venta/MediosPago/Toku.php b/app/src/Service/Venta/MediosPago/Toku.php index 091ba33..d484bb8 100644 --- a/app/src/Service/Venta/MediosPago/Toku.php +++ b/app/src/Service/Venta/MediosPago/Toku.php @@ -1,8 +1,10 @@ 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; } } diff --git a/app/src/Service/Venta/MediosPago/Toku/Customer.php b/app/src/Service/Venta/MediosPago/Toku/Customer.php index f65c243..568569c 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Customer.php +++ b/app/src/Service/Venta/MediosPago/Toku/Customer.php @@ -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 { diff --git a/app/src/Service/Venta/MediosPago/Toku/Invoice.php b/app/src/Service/Venta/MediosPago/Toku/Invoice.php index 9f70d2e..c769f29 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Invoice.php +++ b/app/src/Service/Venta/MediosPago/Toku/Invoice.php @@ -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 { diff --git a/app/src/Service/Venta/MediosPago/Toku/Subscription.php b/app/src/Service/Venta/MediosPago/Toku/Subscription.php index f0d9126..8c6ddce 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Subscription.php +++ b/app/src/Service/Venta/MediosPago/Toku/Subscription.php @@ -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 {