Skip to content

Commit

Permalink
Feature: Add optional notification ID to events
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrayston-paddle committed Sep 16, 2024
1 parent af2dfb0 commit a624e1d
Show file tree
Hide file tree
Showing 41 changed files with 264 additions and 77 deletions.
15 changes: 15 additions & 0 deletions examples/webhook_verification_PSR7.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/

use GuzzleHttp\Psr7\ServerRequest;
use Paddle\SDK\Entities\Event;
use Paddle\SDK\Entities\Transaction;
use Paddle\SDK\Notifications\Events\TransactionUpdated;
use Paddle\SDK\Notifications\Secret;
use Paddle\SDK\Notifications\Verifier;

Expand All @@ -21,6 +24,18 @@

if ($isVerified) {
echo "Webhook is verified\n";

$event = Event::fromRequest($request);
$id = $event->notificationId;
$eventId = $event->eventId;
$eventType = $event->eventType;
$occurredAt = $event->occurredAt;

if ($event instanceof TransactionUpdated) {
/** @var Transaction $transaction */
$transaction = $event->data;
$transactionId = $transaction->id;
}
} else {
echo "Webhook is not verified\n";
}
16 changes: 13 additions & 3 deletions src/Entities/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

use Paddle\SDK\Entities\Event\EventTypeName;
use Paddle\SDK\Notifications\Entities\Entity as NotificationEntity;
use Psr\Http\Message\ServerRequestInterface;

abstract class Event implements Entity
{
/**
* @internal
*/
protected function __construct(
public string $eventId,
public EventTypeName $eventType,
public \DateTimeInterface $occurredAt,
public NotificationEntity $data,
public string|null $notificationId = null,
) {
}

Expand Down Expand Up @@ -45,13 +44,24 @@ public static function from(array $data): self
EventTypeName::from($data['event_type']),
DateTime::from($data['occurred_at']),
$entity::from($data['data']),
$data['notification_id'] ?? null,
);
}

public static function fromRequest(ServerRequestInterface $request): self
{
return self::from(json_decode(
(string) $request->getBody(),
true,
JSON_THROW_ON_ERROR,
));
}

abstract public static function fromEvent(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
NotificationEntity $data,
string|null $notificationId = null,
): static;
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/AddressCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Address $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/AddressUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Address $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/AdjustmentCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Adjustment $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/AdjustmentUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Adjustment $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/BusinessCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Business $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/BusinessUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Business $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/CustomerCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Customer $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/CustomerUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Customer $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/DiscountCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Discount $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/DiscountImported.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Discount $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/DiscountUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Discount $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/PayoutCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Payout $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/PayoutPaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Payout $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/PriceCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Price $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Events/PriceUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ private function __construct(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Price $data,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId);
}

/**
Expand All @@ -28,7 +29,8 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data);
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
Loading

0 comments on commit a624e1d

Please sign in to comment.