Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions tests/Integration/WebhookSignatureIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,49 @@ protected function setUp(): void
$this->secret = $this->requireEnv('NFE_SDK_E2E_WEBHOOK_SECRET');
}

public function test_constructEvent_aceita_payload_assinado_corretamente(): void
public function test_constructEvent_aceita_envelope_v2_assinado_corretamente(): void
{
// Envelope v2 real da NFE.io: { action, payload } — `data` é o payload
// aninhado (não o corpo inteiro).
$payload = json_encode([
'type' => 'invoice.issued',
'data' => ['id' => 'inv-123', 'flowStatus' => 'Issued'],
'id' => 'evt-001',
'action' => 'service_invoice.issued_successfully',
'payload' => ['id' => 'inv-123', 'flowStatus' => 'Issued'],
'id' => 'evt-001',
'createdAt' => '2026-01-01T00:00:00Z',
], JSON_THROW_ON_ERROR);

$sig = 'sha1=' . hash_hmac('sha1', $payload, $this->secret);

$event = Webhook::constructEvent(payload: $payload, sigHeader: $sig, secret: $this->secret);

$this->assertSame('invoice.issued', $event->type);
$this->assertSame('service_invoice.issued_successfully', $event->type);
$this->assertSame('evt-001', $event->id);
$this->assertSame('inv-123', $event->data['id']);
$this->assertSame('Issued', $event->data['flowStatus']);
}

public function test_constructEvent_payload_flat_usa_o_corpo_todo_como_data(): void
{
// Sem envelope v2 (`action`/`payload`), o helper cai no fallback flat:
// `data` passa a ser o corpo decodificado inteiro, e `type` vem de
// type/event_type/action. Este teste pina esse contrato.
$payload = json_encode([
'type' => 'service_invoice.issued_successfully',
'id' => 'inv-999',
], JSON_THROW_ON_ERROR);

$sig = 'sha1=' . hash_hmac('sha1', $payload, $this->secret);

$event = Webhook::constructEvent(payload: $payload, sigHeader: $sig, secret: $this->secret);

$this->assertSame('service_invoice.issued_successfully', $event->type);
$this->assertSame('inv-999', $event->data['id']); // data = corpo inteiro
$this->assertSame('inv-999', $event->id);
}

public function test_constructEvent_recusa_assinatura_invalida(): void
{
$payload = '{"type":"invoice.issued"}';
$payload = '{"action":"service_invoice.issued_successfully","payload":{}}';
$sig = 'sha1=' . hash_hmac('sha1', $payload, 'wrong-secret');

$this->expectException(SignatureVerificationException::class);
Expand Down
Loading