Skip to content

Commit

Permalink
feat: Add optional notification ID to events (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrayston-paddle authored Sep 18, 2024
1 parent bc1d683 commit 750cac7
Show file tree
Hide file tree
Showing 89 changed files with 5,204 additions and 115 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Check our main [developer changelog](https://developer.paddle.com/?utm_source=dx
- `TransactionsClient::getInvoicePDF` now supports `disposition` parameter, see [related changelog](https://developer.paddle.com/changelog/2024/invoice-pdf-open-in-browser)
- Support notification settings pagination, see [related changelog](https://developer.paddle.com/changelog/2024/notification-settings-pagination)
- Support notification settings `active` filter
- Support for `notification_id` on notification events

### Fixed

Expand Down
12 changes: 12 additions & 0 deletions examples/webhook_verification_PSR7.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/

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

Expand All @@ -21,6 +23,16 @@

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) {
$transactionId = $event->transaction->id;
}
} else {
echo "Webhook is not verified\n";
}
18 changes: 14 additions & 4 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 All @@ -26,7 +25,7 @@ public static function from(array $data): self
$entity = $type[0] ?? 'Unknown';
$identifier = str_replace('_', '', ucwords(implode('_', $type), '_'));

/** @var class-string<Event> $entity */
/** @var class-string<Event> $event */
$event = sprintf('\Paddle\SDK\Notifications\Events\%s', $identifier);

if (! class_exists($event) || ! is_subclass_of($event, self::class)) {
Expand All @@ -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;
}
8 changes: 5 additions & 3 deletions src/Notifications/Events/AddressCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Address $data,
public readonly Address $address,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $address, $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);
}
}
36 changes: 36 additions & 0 deletions src/Notifications/Events/AddressImported.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Events;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Entities\Event\EventTypeName;
use Paddle\SDK\Notifications\Entities\Address;
use Paddle\SDK\Notifications\Entities\Entity;

final class AddressImported extends Event
{
private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
public readonly Address $address,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $address, $notificationId);
}

/**
* @param Address $data
*/
public static function fromEvent(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
8 changes: 5 additions & 3 deletions src/Notifications/Events/AddressUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Address $data,
public readonly Address $address,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $address, $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);
}
}
8 changes: 5 additions & 3 deletions src/Notifications/Events/AdjustmentCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Adjustment $data,
public readonly Adjustment $adjustment,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $adjustment, $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);
}
}
8 changes: 5 additions & 3 deletions src/Notifications/Events/AdjustmentUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Adjustment $data,
public readonly Adjustment $adjustment,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $adjustment, $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);
}
}
8 changes: 5 additions & 3 deletions src/Notifications/Events/BusinessCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Business $data,
public readonly Business $business,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $business, $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);
}
}
36 changes: 36 additions & 0 deletions src/Notifications/Events/BusinessImported.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Events;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Entities\Event\EventTypeName;
use Paddle\SDK\Notifications\Entities\Business;
use Paddle\SDK\Notifications\Entities\Entity;

final class BusinessImported extends Event
{
private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
public readonly Business $business,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $business, $notificationId);
}

/**
* @param Business $data
*/
public static function fromEvent(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
8 changes: 5 additions & 3 deletions src/Notifications/Events/BusinessUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Business $data,
public readonly Business $business,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $business, $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);
}
}
8 changes: 5 additions & 3 deletions src/Notifications/Events/CustomerCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Customer $data,
public readonly Customer $customer,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $customer, $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);
}
}
36 changes: 36 additions & 0 deletions src/Notifications/Events/CustomerImported.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Events;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Entities\Event\EventTypeName;
use Paddle\SDK\Notifications\Entities\Customer;
use Paddle\SDK\Notifications\Entities\Entity;

final class CustomerImported extends Event
{
private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
public readonly Customer $customer,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $customer, $notificationId);
}

/**
* @param Customer $data
*/
public static function fromEvent(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
string|null $notificationId = null,
): static {
return new self($eventId, $eventType, $occurredAt, $data, $notificationId);
}
}
8 changes: 5 additions & 3 deletions src/Notifications/Events/CustomerUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ private function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Customer $data,
public readonly Customer $customer,
string|null $notificationId,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $customer, $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 750cac7

Please sign in to comment.