|
|
@@ -3,10 +3,18 @@
|
|
|
namespace Controllers;
|
|
|
|
|
|
use Libs\ResponseLib;
|
|
|
+use Models\PaymentModel;
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
|
class WooviWebhookController
|
|
|
{
|
|
|
+ private PaymentModel $paymentModel;
|
|
|
+
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->paymentModel = new PaymentModel();
|
|
|
+ }
|
|
|
+
|
|
|
public function __invoke(ServerRequestInterface $request)
|
|
|
{
|
|
|
$rawBody = (string)$request->getBody();
|
|
|
@@ -14,46 +22,48 @@ class WooviWebhookController
|
|
|
$rawBody = (string)file_get_contents('php://input');
|
|
|
}
|
|
|
|
|
|
- $record = [
|
|
|
- 'received_at' => (new \DateTimeImmutable('now', new \DateTimeZone('UTC')))->format(DATE_ATOM),
|
|
|
- 'headers' => $this->normalizeHeaders($request->getHeaders()),
|
|
|
- 'raw_body' => $rawBody,
|
|
|
- ];
|
|
|
+ $payload = json_decode($rawBody, true);
|
|
|
+ if (!is_array($payload)) {
|
|
|
+ return ResponseLib::sendFail('Payload inválido', [], 'E_VALIDATE')->withStatus(400);
|
|
|
+ }
|
|
|
|
|
|
- $fileName = sprintf(
|
|
|
- 'woovi_webhook_%s_%s.txt',
|
|
|
- (new \DateTimeImmutable('now'))->format('Ymd_His'),
|
|
|
- bin2hex(random_bytes(4))
|
|
|
- );
|
|
|
+ $charge = $payload['charge'] ?? null;
|
|
|
+ if (!is_array($charge)) {
|
|
|
+ return ResponseLib::sendFail('Charge inválida', [], 'E_VALIDATE')->withStatus(400);
|
|
|
+ }
|
|
|
+
|
|
|
+ $correlationId = $charge['correlationID'] ?? null;
|
|
|
+ $status = $charge['status'] ?? null;
|
|
|
|
|
|
- $filePath = dirname(__DIR__) . '/bin/' . $fileName;
|
|
|
+ if (!is_string($correlationId) || $correlationId === '') {
|
|
|
+ return ResponseLib::sendFail('correlationID inválido', [], 'E_VALIDATE')->withStatus(400);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!is_string($status) || $status === '') {
|
|
|
+ return ResponseLib::sendFail('status inválido', [], 'E_VALIDATE')->withStatus(400);
|
|
|
+ }
|
|
|
|
|
|
try {
|
|
|
- $encoded = json_encode($record, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
|
- if ($encoded === false) {
|
|
|
- $encoded = $rawBody;
|
|
|
- }
|
|
|
- file_put_contents($filePath, $encoded, LOCK_EX);
|
|
|
+ $payment = $this->paymentModel->findByExternalId($correlationId);
|
|
|
} catch (\Throwable $e) {
|
|
|
- return ResponseLib::sendFail('Falha ao salvar webhook: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
|
|
|
+ return ResponseLib::sendFail('Falha ao consultar pagamento: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
|
|
|
}
|
|
|
|
|
|
- return ResponseLib::sendOk([
|
|
|
- 'stored_file' => basename($filePath),
|
|
|
- ], 'S_WEBHOOK_SAVED');
|
|
|
- }
|
|
|
+ if (!$payment) {
|
|
|
+ return ResponseLib::sendFail('Pagamento não encontrado', ['correlation_id' => $correlationId], 'E_NOT_FOUND')->withStatus(404);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * @param array<string, array<int, string>> $headers
|
|
|
- * @return array<string, string>
|
|
|
- */
|
|
|
- private function normalizeHeaders(array $headers): array
|
|
|
- {
|
|
|
- $normalized = [];
|
|
|
- foreach ($headers as $name => $values) {
|
|
|
- $normalized[$name] = implode(', ', $values);
|
|
|
+ try {
|
|
|
+ $this->paymentModel->updateStatusId((int)$payment['payment_id'], 1);
|
|
|
+ } catch (\Throwable $e) {
|
|
|
+ return ResponseLib::sendFail('Falha ao atualizar pagamento: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
|
|
|
}
|
|
|
|
|
|
- return $normalized;
|
|
|
+ return ResponseLib::sendOk([
|
|
|
+ 'payment_id' => (int)$payment['payment_id'],
|
|
|
+ 'correlation_id' => $correlationId,
|
|
|
+ 'status' => $status,
|
|
|
+ 'status_id' => 1,
|
|
|
+ ], 'S_PAYMENT_UPDATED');
|
|
|
}
|
|
|
}
|