diff --git a/examples/webhook_verification_PSR7.php b/examples/webhook_verification_PSR7.php index 9e25cd2..25c2159 100644 --- a/examples/webhook_verification_PSR7.php +++ b/examples/webhook_verification_PSR7.php @@ -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; @@ -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"; } diff --git a/src/Entities/Event.php b/src/Entities/Event.php index 92bd3a1..6b11c2a 100644 --- a/src/Entities/Event.php +++ b/src/Entities/Event.php @@ -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, ) { } @@ -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; } diff --git a/src/Notifications/Events/AddressCreated.php b/src/Notifications/Events/AddressCreated.php index aab43b4..21b2416 100644 --- a/src/Notifications/Events/AddressCreated.php +++ b/src/Notifications/Events/AddressCreated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/AddressUpdated.php b/src/Notifications/Events/AddressUpdated.php index 5f6eb9b..8980671 100644 --- a/src/Notifications/Events/AddressUpdated.php +++ b/src/Notifications/Events/AddressUpdated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/AdjustmentCreated.php b/src/Notifications/Events/AdjustmentCreated.php index a9cbb9b..0aa7f20 100644 --- a/src/Notifications/Events/AdjustmentCreated.php +++ b/src/Notifications/Events/AdjustmentCreated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/AdjustmentUpdated.php b/src/Notifications/Events/AdjustmentUpdated.php index 38de8bd..28f4502 100644 --- a/src/Notifications/Events/AdjustmentUpdated.php +++ b/src/Notifications/Events/AdjustmentUpdated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/BusinessCreated.php b/src/Notifications/Events/BusinessCreated.php index aaa3166..46d9bee 100644 --- a/src/Notifications/Events/BusinessCreated.php +++ b/src/Notifications/Events/BusinessCreated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/BusinessUpdated.php b/src/Notifications/Events/BusinessUpdated.php index c8cbf0c..f79ecb5 100644 --- a/src/Notifications/Events/BusinessUpdated.php +++ b/src/Notifications/Events/BusinessUpdated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/CustomerCreated.php b/src/Notifications/Events/CustomerCreated.php index bcb366a..f2e3fa1 100644 --- a/src/Notifications/Events/CustomerCreated.php +++ b/src/Notifications/Events/CustomerCreated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/CustomerUpdated.php b/src/Notifications/Events/CustomerUpdated.php index 23d2749..fca4565 100644 --- a/src/Notifications/Events/CustomerUpdated.php +++ b/src/Notifications/Events/CustomerUpdated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/DiscountCreated.php b/src/Notifications/Events/DiscountCreated.php index eb982ca..34f3e83 100644 --- a/src/Notifications/Events/DiscountCreated.php +++ b/src/Notifications/Events/DiscountCreated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/DiscountImported.php b/src/Notifications/Events/DiscountImported.php index 127450a..d2cedfa 100644 --- a/src/Notifications/Events/DiscountImported.php +++ b/src/Notifications/Events/DiscountImported.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/DiscountUpdated.php b/src/Notifications/Events/DiscountUpdated.php index 9fb296b..db1dbb0 100644 --- a/src/Notifications/Events/DiscountUpdated.php +++ b/src/Notifications/Events/DiscountUpdated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/PayoutCreated.php b/src/Notifications/Events/PayoutCreated.php index afa3bb4..11ec54a 100644 --- a/src/Notifications/Events/PayoutCreated.php +++ b/src/Notifications/Events/PayoutCreated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/PayoutPaid.php b/src/Notifications/Events/PayoutPaid.php index 82bd3dc..8c16997 100644 --- a/src/Notifications/Events/PayoutPaid.php +++ b/src/Notifications/Events/PayoutPaid.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/PriceCreated.php b/src/Notifications/Events/PriceCreated.php index fa8f870..971b01c 100644 --- a/src/Notifications/Events/PriceCreated.php +++ b/src/Notifications/Events/PriceCreated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/PriceUpdated.php b/src/Notifications/Events/PriceUpdated.php index a6e8350..c95e5dd 100644 --- a/src/Notifications/Events/PriceUpdated.php +++ b/src/Notifications/Events/PriceUpdated.php @@ -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); } /** @@ -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); } } diff --git a/src/Notifications/Events/ProductCreated.php b/src/Notifications/Events/ProductCreated.php index 1be7ad2..628db11 100644 --- a/src/Notifications/Events/ProductCreated.php +++ b/src/Notifications/Events/ProductCreated.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Product $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/ProductUpdated.php b/src/Notifications/Events/ProductUpdated.php index 1b38299..5b23a8f 100644 --- a/src/Notifications/Events/ProductUpdated.php +++ b/src/Notifications/Events/ProductUpdated.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Product $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/ReportCreated.php b/src/Notifications/Events/ReportCreated.php index 063081b..6ac5285 100644 --- a/src/Notifications/Events/ReportCreated.php +++ b/src/Notifications/Events/ReportCreated.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Report $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/ReportUpdated.php b/src/Notifications/Events/ReportUpdated.php index 15ae4f0..c437f69 100644 --- a/src/Notifications/Events/ReportUpdated.php +++ b/src/Notifications/Events/ReportUpdated.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Report $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/SubscriptionActivated.php b/src/Notifications/Events/SubscriptionActivated.php index 39746ae..4098edd 100644 --- a/src/Notifications/Events/SubscriptionActivated.php +++ b/src/Notifications/Events/SubscriptionActivated.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Subscription $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/SubscriptionCanceled.php b/src/Notifications/Events/SubscriptionCanceled.php index 0d7ee51..9103629 100644 --- a/src/Notifications/Events/SubscriptionCanceled.php +++ b/src/Notifications/Events/SubscriptionCanceled.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Subscription $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/SubscriptionCreated.php b/src/Notifications/Events/SubscriptionCreated.php index c47ca12..5e1c311 100644 --- a/src/Notifications/Events/SubscriptionCreated.php +++ b/src/Notifications/Events/SubscriptionCreated.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Subscription $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/SubscriptionImported.php b/src/Notifications/Events/SubscriptionImported.php index f228530..68925a0 100644 --- a/src/Notifications/Events/SubscriptionImported.php +++ b/src/Notifications/Events/SubscriptionImported.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Subscription $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/SubscriptionPastDue.php b/src/Notifications/Events/SubscriptionPastDue.php index 8864405..3cc5329 100644 --- a/src/Notifications/Events/SubscriptionPastDue.php +++ b/src/Notifications/Events/SubscriptionPastDue.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Subscription $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/SubscriptionPaused.php b/src/Notifications/Events/SubscriptionPaused.php index 78c3497..4266c19 100644 --- a/src/Notifications/Events/SubscriptionPaused.php +++ b/src/Notifications/Events/SubscriptionPaused.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Subscription $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/SubscriptionResumed.php b/src/Notifications/Events/SubscriptionResumed.php index 9071fee..163570b 100644 --- a/src/Notifications/Events/SubscriptionResumed.php +++ b/src/Notifications/Events/SubscriptionResumed.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Subscription $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/SubscriptionTrialing.php b/src/Notifications/Events/SubscriptionTrialing.php index 13519d2..a91931a 100644 --- a/src/Notifications/Events/SubscriptionTrialing.php +++ b/src/Notifications/Events/SubscriptionTrialing.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Subscription $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/SubscriptionUpdated.php b/src/Notifications/Events/SubscriptionUpdated.php index bb5b526..1817227 100644 --- a/src/Notifications/Events/SubscriptionUpdated.php +++ b/src/Notifications/Events/SubscriptionUpdated.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Subscription $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/TransactionBilled.php b/src/Notifications/Events/TransactionBilled.php index 474e3a5..3ab1c36 100644 --- a/src/Notifications/Events/TransactionBilled.php +++ b/src/Notifications/Events/TransactionBilled.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Transaction $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/TransactionCanceled.php b/src/Notifications/Events/TransactionCanceled.php index cb28a30..e18dd05 100644 --- a/src/Notifications/Events/TransactionCanceled.php +++ b/src/Notifications/Events/TransactionCanceled.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Transaction $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/TransactionCompleted.php b/src/Notifications/Events/TransactionCompleted.php index 83d0271..c73cdcb 100644 --- a/src/Notifications/Events/TransactionCompleted.php +++ b/src/Notifications/Events/TransactionCompleted.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Transaction $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/TransactionCreated.php b/src/Notifications/Events/TransactionCreated.php index ded7eab..3dd3ae2 100644 --- a/src/Notifications/Events/TransactionCreated.php +++ b/src/Notifications/Events/TransactionCreated.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Transaction $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/TransactionPaid.php b/src/Notifications/Events/TransactionPaid.php index 1517279..cc06d4d 100644 --- a/src/Notifications/Events/TransactionPaid.php +++ b/src/Notifications/Events/TransactionPaid.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Transaction $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/TransactionPastDue.php b/src/Notifications/Events/TransactionPastDue.php index 5700484..f3052b1 100644 --- a/src/Notifications/Events/TransactionPastDue.php +++ b/src/Notifications/Events/TransactionPastDue.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Transaction $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/TransactionPaymentFailed.php b/src/Notifications/Events/TransactionPaymentFailed.php index 2eabd3a..f8ad64d 100644 --- a/src/Notifications/Events/TransactionPaymentFailed.php +++ b/src/Notifications/Events/TransactionPaymentFailed.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Transaction $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/TransactionReady.php b/src/Notifications/Events/TransactionReady.php index 09fad11..778e829 100644 --- a/src/Notifications/Events/TransactionReady.php +++ b/src/Notifications/Events/TransactionReady.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Transaction $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/src/Notifications/Events/TransactionUpdated.php b/src/Notifications/Events/TransactionUpdated.php index e2a2caa..822615c 100644 --- a/src/Notifications/Events/TransactionUpdated.php +++ b/src/Notifications/Events/TransactionUpdated.php @@ -16,8 +16,9 @@ private function __construct( EventTypeName $eventType, \DateTimeInterface $occurredAt, Transaction $data, + string|null $notificationId, ) { - parent::__construct($eventId, $eventType, $occurredAt, $data); + parent::__construct($eventId, $eventType, $occurredAt, $data, $notificationId); } /** @@ -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); } } diff --git a/tests/Unit/Entities/EventTest.php b/tests/Unit/Entities/EventTest.php new file mode 100644 index 0000000..c5d3b97 --- /dev/null +++ b/tests/Unit/Entities/EventTest.php @@ -0,0 +1,59 @@ +notificationId); + + self::assertInstanceOf(BusinessUpdated::class, $event); + self::assertInstanceOf(Entity::class, $event->data); + self::assertSame('evt_01h8bzakzx3hm2fmen703n5q45', $event->eventId); + self::assertSame('2023-08-21T11:57:47.390+00:00', $event->occurredAt->format(DATE_RFC3339_EXTENDED)); + self::assertSame('business.updated', $event->eventType->getValue()); + + $business = $event->data; + self::assertInstanceOf(Business::class, $business); + self::assertSame('biz_01h84a7hr4pzhsajkm8tev89ev', $business->id); + self::assertSame('ChatApp Inc.', $business->name); + self::assertSame('active', $business->status->getValue()); + } + + /** @test */ + public function it_creates_from_request(): void + { + $requestStream = $this->createMock(StreamInterface::class); + $requestStream + ->method('__toString') + ->willReturn(self::readRawJsonFixture('notification_business_updated')); + + $request = $this->createMock(ServerRequestInterface::class); + $request + ->method('getBody') + ->willReturn($requestStream); + + $notification = Event::fromRequest($request); + + self::assertSame('ntf_01h8bzam1z32agrxjwhjgqk8w6', $notification->notificationId); + } +} diff --git a/tests/Unit/Entities/_fixtures/notification_business_updated.json b/tests/Unit/Entities/_fixtures/notification_business_updated.json new file mode 100644 index 0000000..c3c93c8 --- /dev/null +++ b/tests/Unit/Entities/_fixtures/notification_business_updated.json @@ -0,0 +1,29 @@ +{ + "data": { + "id": "biz_01h84a7hr4pzhsajkm8tev89ev", + "name": "ChatApp Inc.", + "status": "active", + "contacts": [ + { + "name": "Parker Jones", + "email": "parker@example.com" + }, + { + "name": "Jo Riley", + "email": "jo@example.com" + }, + { + "name": "Jesse Garcia", + "email": "jo@example.com" + } + ], + "created_at": "2023-08-18T12:34:25.668Z", + "updated_at": "2023-08-21T11:57:47.03542Z", + "company_number": "555775291485", + "tax_identifier": null + }, + "event_id": "evt_01h8bzakzx3hm2fmen703n5q45", + "event_type": "business.updated", + "occurred_at": "2023-08-21T11:57:47.390028Z", + "notification_id": "ntf_01h8bzam1z32agrxjwhjgqk8w6" +}