Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ on:
jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: ['8.3', '8.4']
steps:
- uses: actions/checkout@v7
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
php-version: ${{ matrix.php-version }}
extensions: mbstring, ctype, json, dom
coverage: pcov
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run PHPUnit
run: vendor/bin/pest --coverage-text --ci
- name: Run tests with coverage
run: vendor/bin/pest --coverage-text --coverage-clover=coverage.xml --ci
run: composer test -- --coverage-text --coverage-clover=coverage.xml --ci
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v7
with:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"require": {
"php": "^8.3",
"spatie/laravel-package-tools": "^1.12",
"illuminate/contracts": "*",
"illuminate/contracts": "^11.0 || ^12.0 || ^13.0",
"symfony/mailer": "^7.0"
},
"config": {
Expand Down
19 changes: 12 additions & 7 deletions config/larawebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,42 @@
| Custom Validators
|--------------------------------------------------------------------------
|
| Register custom webhook validators for additional services.
| Register custom signature validators for supported services.
| A custom validator replaces the built-in validator for its service.
| Each validator must implement WebhookValidatorInterface.
|
| Example:
| 'twilio' => \App\Webhooks\TwilioValidator::class,
| 'stripe' => \App\Webhooks\CustomStripeValidator::class,
|
*/
'custom_validators' => [
// Add your custom validators here
],

'custom_parsers' => [
// 'twilio' => \App\Webhooks\TwilioPayloadParser::class,
],

'services' => [
'stripe' => [
'public' => env('STRIPE_PUBLIC_KEY', 'stripe_public_key_test'),
'secret' => env('STRIPE_SECRET_KEY', 'stripe_secret_key_test'),
'webhook_secret' => env('STRIPE_WEBHOOK_SECRET', 'stripe_webhook_secret_key_test'),
'secret' => env('STRIPE_SECRET_KEY'),
'webhook_secret' => env('STRIPE_WEBHOOK_SECRET'),
'tolerance' => 300,
],

'github' => [
'webhook_secret' => env('GITHUB_WEBHOOK_SECRET', 'github_webhook_secret_key_test'),
'webhook_secret' => env('GITHUB_WEBHOOK_SECRET'),
'tolerance' => 300,
],

'slack' => [
'webhook_secret' => env('SLACK_WEBHOOK_SECRET', 'slack_webhook_secret_test'),
'webhook_secret' => env('SLACK_WEBHOOK_SECRET'),
'tolerance' => env('SLACK_WEBHOOK_TOLERANCE', 300),
],

'shopify' => [
'webhook_secret' => env('SHOPIFY_WEBHOOK_SECRET', 'shopify_webhook_secret_test'),
'webhook_secret' => env('SHOPIFY_WEBHOOK_SECRET'),
'tolerance' => env('SHOPIFY_WEBHOOK_TOLERANCE', 300),
],
],
Expand Down
4 changes: 4 additions & 0 deletions docs/facade-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ For new code, prefer the high-level helpers:

They are deprecated for new code.

Plan their removal when upgrading to the next major version. Existing calls remain
supported in the current major version; migrate them to `validateAndLog()` or the
`RecordWebhookLog` use case before upgrading.

Prefer:

- `validateAndLog()` when validating and recording a webhook;
Expand Down
22 changes: 22 additions & 0 deletions src/Audit/Application/Data/WebhookLogData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Proxynth\Larawebhook\Audit\Application\Data;

final readonly class WebhookLogData
{
public function __construct(
public int|string $id,
public string $service,
public ?string $event,
public string $status,
public ?array $payload,
public ?string $errorMessage,
public int $attempt,
public ?string $externalId,
public ?string $idempotencyKey,
public string $createdAt,
public string $updatedAt,
) {}
}
21 changes: 21 additions & 0 deletions src/Audit/Application/Data/WebhookLogPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Proxynth\Larawebhook\Audit\Application\Data;

use Proxynth\Larawebhook\Audit\Application\ReadModels\WebhookLogSummary;

final readonly class WebhookLogPage
{
/** @param list<WebhookLogSummary> $items */
public function __construct(
public array $items,
public int $total,
public int $perPage,
public int $currentPage,
public int $lastPage,
/** @var array{first:?string,last:?string,prev:?string,next:?string} */
public array $links,
) {}
}
4 changes: 2 additions & 2 deletions src/Audit/Application/Ports/WebhookAuditLogWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Proxynth\Larawebhook\Audit\Application\Ports;

use Proxynth\Larawebhook\Audit\Application\Commands\RecordWebhookLogCommand;
use Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Persistence\Models\WebhookLog;
use Proxynth\Larawebhook\Audit\Application\Data\WebhookLogData;

interface WebhookAuditLogWriter
{
public function record(RecordWebhookLogCommand $command): WebhookLog;
public function record(RecordWebhookLogCommand $command): WebhookLogData;
}
4 changes: 2 additions & 2 deletions src/Audit/Application/Ports/WebhookLogReadRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Proxynth\Larawebhook\Audit\Application\Ports;

use Illuminate\Pagination\LengthAwarePaginator;
use Proxynth\Larawebhook\Audit\Application\Data\WebhookLogPage;
use Proxynth\Larawebhook\Audit\Application\Queries\ListWebhookLogsQuery;
use Proxynth\Larawebhook\Audit\Application\ReadModels\WebhookFailureDetails;
use Proxynth\Larawebhook\Audit\Application\ReadModels\WebhookLogDetails;

interface WebhookLogReadRepository
{
public function paginateSummaries(ListWebhookLogsQuery $query): LengthAwarePaginator;
public function paginateSummaries(ListWebhookLogsQuery $query): WebhookLogPage;

public function findDetails(int|string $id): WebhookLogDetails;

Expand Down
4 changes: 2 additions & 2 deletions src/Audit/Application/Queries/ListWebhookLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Proxynth\Larawebhook\Audit\Application\Queries;

use Illuminate\Pagination\LengthAwarePaginator;
use Proxynth\Larawebhook\Audit\Application\Data\WebhookLogPage;
use Proxynth\Larawebhook\Audit\Application\Ports\WebhookLogReadRepository;

final readonly class ListWebhookLogs
Expand All @@ -13,7 +13,7 @@ public function __construct(
private WebhookLogReadRepository $readRepository,
) {}

public function handle(ListWebhookLogsQuery $query): LengthAwarePaginator
public function handle(ListWebhookLogsQuery $query): WebhookLogPage
{
return $this->readRepository->paginateSummaries($query);
}
Expand Down
16 changes: 0 additions & 16 deletions src/Audit/Application/ReadModels/WebhookFailureDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Proxynth\Larawebhook\Audit\Application\ReadModels;

use Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Persistence\Models\WebhookLog;

final readonly class WebhookFailureDetails
{
public function __construct(
Expand All @@ -20,18 +18,4 @@ public function __construct(
public string $createdAt,
) {}

public static function fromModel(WebhookLog $log): self
{
return new self(
id: $log->getKey(),
service: $log->service,
event: $log->event,
status: $log->status,
errorMessage: $log->error_message ?? 'Unknown webhook failure.',
attempt: $log->attempt,
externalId: $log->external_id,
idempotencyKey: $log->idempotency_key,
createdAt: $log->created_at->toISOString() ?? '',
);
}
}
18 changes: 0 additions & 18 deletions src/Audit/Application/ReadModels/WebhookLogDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Proxynth\Larawebhook\Audit\Application\ReadModels;

use Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Persistence\Models\WebhookLog;

final readonly class WebhookLogDetails
{
public function __construct(
Expand All @@ -22,20 +20,4 @@ public function __construct(
public string $updatedAt,
) {}

public static function fromModel(WebhookLog $log): self
{
return new self(
id: $log->id,
service: $log->service,
event: $log->event,
status: $log->status,
payload: $log->payload,
errorMessage: $log->error_message,
attempt: $log->attempt,
externalId: $log->external_id,
idempotencyKey: $log->idempotency_key,
createdAt: $log->created_at->toISOString() ?? '',
updatedAt: $log->updated_at->toISOString() ?? '',
);
}
}
15 changes: 0 additions & 15 deletions src/Audit/Application/ReadModels/WebhookLogSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Proxynth\Larawebhook\Audit\Application\ReadModels;

use Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Persistence\Models\WebhookLog;

final readonly class WebhookLogSummary
{
public function __construct(
Expand All @@ -19,17 +17,4 @@ public function __construct(
public string $createdAt,
) {}

public static function fromModel(WebhookLog $log): self
{
return new self(
id: $log->getKey(),
service: $log->service,
event: $log->event,
status: $log->status,
attempt: $log->attempt,
externalId: $log->external_id,
idempotencyKey: $log->idempotency_key,
createdAt: $log->created_at->toISOString() ?? '',
);
}
}
4 changes: 2 additions & 2 deletions src/Audit/Application/UseCases/RecordWebhookLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
namespace Proxynth\Larawebhook\Audit\Application\UseCases;

use Proxynth\Larawebhook\Audit\Application\Commands\RecordWebhookLogCommand;
use Proxynth\Larawebhook\Audit\Application\Data\WebhookLogData;
use Proxynth\Larawebhook\Audit\Application\Ports\WebhookAuditLogWriter;
use Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Persistence\Models\WebhookLog;

final readonly class RecordWebhookLog
{
public function __construct(
private WebhookAuditLogWriter $writer,
) {}

public function handle(RecordWebhookLogCommand $command): WebhookLog
public function handle(RecordWebhookLogCommand $command): WebhookLogData
{
return $this->writer->record($command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Proxynth\Larawebhook\Processing\Application\Commands\ReplayWebhookCommand;
use Proxynth\Larawebhook\Processing\Application\UseCases\ReplayWebhook;
use Proxynth\Larawebhook\Shared\Application\Ports\EventBus;
use Proxynth\Larawebhook\Shared\Domain\Enums\WebhookService;
use Proxynth\Larawebhook\Shared\Domain\ValueObjects\ConfiguredWebhookService;
use RuntimeException;
use Throwable;

Expand All @@ -46,19 +46,14 @@ public function index(Request $request): JsonResponse
));

return response()->json([
'data' => WebhookLogResource::collection($logs),
'data' => WebhookLogResource::collection($logs->items),
'meta' => [
'total' => $logs->total(),
'per_page' => $logs->perPage(),
'current_page' => $logs->currentPage(),
'last_page' => $logs->lastPage(),
],
'links' => [
'first' => $logs->url(1),
'last' => $logs->url($logs->lastPage()),
'prev' => $logs->previousPageUrl(),
'next' => $logs->nextPageUrl(),
'total' => $logs->total,
'per_page' => $logs->perPage,
'current_page' => $logs->currentPage,
'last_page' => $logs->lastPage,
],
'links' => $logs->links,
]);
}

Expand Down Expand Up @@ -121,7 +116,7 @@ private function extractSignatureFromPayload(WebhookLog $log): Signature
// For now, we'll regenerate the signature for replay purposes
// In a real implementation, you'd store the original signature
$payload = json_encode($log->payload);
$secret = $this->secretResolver->resolve(WebhookService::fromString($log->service));
$secret = $this->secretResolver->resolve(ConfiguredWebhookService::resolve($log->service));

if ($secret === null) {
throw new RuntimeException('No secret configured for service: '.$log->service);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Proxynth\Larawebhook\Audit\Application\ReadModels\WebhookLogDetails;
use Proxynth\Larawebhook\Audit\Application\ReadModels\WebhookLogSummary;
use Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Persistence\Models\WebhookLog;
use Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Persistence\WebhookLogReadModelFactory;

/**
* @mixin WebhookLog
Expand All @@ -34,7 +35,7 @@ public function toArray(Request $request): array
$this->resource instanceof WebhookLogSummary => $this->summary($this->resource),
$this->resource instanceof WebhookLogDetails => $this->details($this->resource),
$this->resource instanceof WebhookFailureDetails => $this->failureDetails($this->resource),
$this->resource instanceof WebhookLog => $this->details(WebhookLogDetails::fromModel($this->resource)),
$this->resource instanceof WebhookLog => $this->details((new WebhookLogReadModelFactory)->details($this->resource)),
default => throw new InvalidArgumentException(sprintf(
'WebhookLogResource expects [%s], [%s], [%s] or [%s], [%s] given.',
WebhookLogSummary::class,
Expand Down
Loading