From 11a8de71f93edae4be0b1ee4cdf9bab82271cdee Mon Sep 17 00:00:00 2001 From: David Grayston Date: Thu, 12 Dec 2024 17:52:23 +0000 Subject: [PATCH] fix: Subscription discount now supports null starts_at --- CHANGELOG.md | 6 +++ src/Client.php | 2 +- .../Subscription/SubscriptionDiscount.php | 4 +- .../Subscription/SubscriptionDiscount.php | 4 +- .../_fixtures/response/list_default.json | 12 ++++- tests/Unit/Entities/EventTest.php | 50 ++++++++++++++++++- .../entity/subscription.activated.json | 6 ++- .../entity/subscription.canceled.json | 6 ++- 8 files changed, 79 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 081ee04..2f8c135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), Check our main [developer changelog](https://developer.paddle.com/?utm_source=dx&utm_medium=paddle-php-sdk) for information about changes to the Paddle Billing platform, the Paddle API, and other developer tools. +## [1.7.1] - 2024-12-13 + +### Fixed + +- Subscription discount now supports null `starts_at` + ## [1.7.0] - 2024-12-11 ### Added diff --git a/src/Client.php b/src/Client.php index 5e8dadd..b8836ca 100644 --- a/src/Client.php +++ b/src/Client.php @@ -60,7 +60,7 @@ class Client { - private const SDK_VERSION = '1.7.0'; + private const SDK_VERSION = '1.7.1'; public readonly LoggerInterface $logger; public readonly Options $options; diff --git a/src/Entities/Subscription/SubscriptionDiscount.php b/src/Entities/Subscription/SubscriptionDiscount.php index 2db8a34..14be1a6 100644 --- a/src/Entities/Subscription/SubscriptionDiscount.php +++ b/src/Entities/Subscription/SubscriptionDiscount.php @@ -17,7 +17,7 @@ class SubscriptionDiscount { private function __construct( public string $id, - public \DateTimeInterface $startsAt, + public \DateTimeInterface|null $startsAt, public \DateTimeInterface|null $endsAt, ) { } @@ -26,7 +26,7 @@ public static function from(array $data): self { return new self( $data['id'], - DateTime::from($data['starts_at']), + isset($data['starts_at']) ? DateTime::from($data['starts_at']) : null, isset($data['ends_at']) ? DateTime::from($data['ends_at']) : null, ); } diff --git a/src/Notifications/Entities/Subscription/SubscriptionDiscount.php b/src/Notifications/Entities/Subscription/SubscriptionDiscount.php index bcb4dd9..e87c18c 100644 --- a/src/Notifications/Entities/Subscription/SubscriptionDiscount.php +++ b/src/Notifications/Entities/Subscription/SubscriptionDiscount.php @@ -17,7 +17,7 @@ class SubscriptionDiscount { private function __construct( public string $id, - public \DateTimeInterface $startsAt, + public \DateTimeInterface|null $startsAt, public \DateTimeInterface|null $endsAt, ) { } @@ -26,7 +26,7 @@ public static function from(array $data): self { return new self( $data['id'], - DateTime::from($data['starts_at']), + isset($data['starts_at']) ? DateTime::from($data['starts_at']) : null, isset($data['ends_at']) ? DateTime::from($data['ends_at']) : null, ); } diff --git a/tests/Functional/Resources/Subscriptions/_fixtures/response/list_default.json b/tests/Functional/Resources/Subscriptions/_fixtures/response/list_default.json index 43ed30e..678e531 100644 --- a/tests/Functional/Resources/Subscriptions/_fixtures/response/list_default.json +++ b/tests/Functional/Resources/Subscriptions/_fixtures/response/list_default.json @@ -323,7 +323,11 @@ "update_payment_method": null, "cancel": "https://sandbox-buyer-portal.paddle.com/subscriptions/sub_01hp463gxfvndqjjyqn2n7tkth/cancel" }, - "discount": null, + "discount": { + "id": "dsc_01hv6scyf7qdnzcdq01t2y8dx4", + "starts_at": null, + "ends_at": null + }, "import_meta": null }, { @@ -467,7 +471,11 @@ "update_payment_method": "https://buyer-portal.paddle.com/subscriptions/sub_01hn0epy6nc46wt9hw92pp2kmt/update-payment-method", "cancel": "https://buyer-portal.paddle.com/subscriptions/sub_01hn0epy6nc46wt9hw92pp2kmt/cancel" }, - "discount": null, + "discount": { + "id": "dsc_01hv6scyf7qdnzcdq01t2y8dx4", + "starts_at": "2024-04-12T10:18:47.635628Z", + "ends_at": "2024-05-12T10:18:47.635628Z" + }, "import_meta": null } ], diff --git a/tests/Unit/Entities/EventTest.php b/tests/Unit/Entities/EventTest.php index ef4fb0a..939f3f0 100644 --- a/tests/Unit/Entities/EventTest.php +++ b/tests/Unit/Entities/EventTest.php @@ -12,6 +12,8 @@ use Paddle\SDK\Notifications\Entities\Shared\SavedPaymentMethodOrigin; use Paddle\SDK\Notifications\Events\PaymentMethodDeleted; use Paddle\SDK\Notifications\Events\PaymentMethodSaved; +use Paddle\SDK\Notifications\Events\SubscriptionActivated; +use Paddle\SDK\Notifications\Events\SubscriptionCanceled; use Paddle\SDK\Tests\Utils\ReadsFixtures; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; @@ -222,13 +224,13 @@ public static function eventDataProvider(): iterable [ 'subscription.activated', 'subscription', - \Paddle\SDK\Notifications\Events\SubscriptionActivated::class, + SubscriptionActivated::class, \Paddle\SDK\Notifications\Entities\Subscription::class, ], [ 'subscription.canceled', 'subscription', - \Paddle\SDK\Notifications\Events\SubscriptionCanceled::class, + SubscriptionCanceled::class, \Paddle\SDK\Notifications\Entities\Subscription::class, ], [ @@ -479,4 +481,48 @@ public function it_creates_deleted_payment_method(): void self::assertSame('2024-05-02T02:55:25.198+00:00', $paymentMethod->savedAt->format(DATE_RFC3339_EXTENDED)); self::assertSame('2024-05-03T12:24:18.826+00:00', $paymentMethod->updatedAt->format(DATE_RFC3339_EXTENDED)); } + + /** + * @test + */ + public function it_supports_subscription_discount(): void + { + $event = Event::from([ + 'event_id' => 'evt_01h8bzakzx3hm2fmen703n5q45', + 'event_type' => 'subscription.activated', + 'occurred_at' => '2023-08-21T11:57:47.390028Z', + 'notification_id' => 'ntf_01h8bzam1z32agrxjwhjgqk8w6', + 'data' => self::readJsonFixture('notification/entity/subscription.activated'), + ]); + + self::assertSame('ntf_01h8bzam1z32agrxjwhjgqk8w6', $event->notificationId); + + self::assertInstanceOf(SubscriptionActivated::class, $event); + self::assertInstanceOf(Entity::class, $event->data); + self::assertSame($event->data, $event->subscription); + self::assertEquals('2024-04-12T10:18:47+00:00', $event->subscription->discount->startsAt->format(DATE_RFC3339)); + self::assertEquals('2024-05-12T10:18:47+00:00', $event->subscription->discount->endsAt->format(DATE_RFC3339)); + } + + /** + * @test + */ + public function it_supports_nullable_subscription_discount_starts_at_and_ends_at(): void + { + $event = Event::from([ + 'event_id' => 'evt_01h8bzakzx3hm2fmen703n5q45', + 'event_type' => 'subscription.canceled', + 'occurred_at' => '2023-08-21T11:57:47.390028Z', + 'notification_id' => 'ntf_01h8bzam1z32agrxjwhjgqk8w6', + 'data' => self::readJsonFixture('notification/entity/subscription.canceled'), + ]); + + self::assertSame('ntf_01h8bzam1z32agrxjwhjgqk8w6', $event->notificationId); + + self::assertInstanceOf(SubscriptionCanceled::class, $event); + self::assertInstanceOf(Entity::class, $event->data); + self::assertSame($event->data, $event->subscription); + self::assertNull($event->subscription->discount->startsAt); + self::assertNull($event->subscription->discount->endsAt); + } } diff --git a/tests/Unit/Entities/_fixtures/notification/entity/subscription.activated.json b/tests/Unit/Entities/_fixtures/notification/entity/subscription.activated.json index 9ef48f6..4c7d678 100644 --- a/tests/Unit/Entities/_fixtures/notification/entity/subscription.activated.json +++ b/tests/Unit/Entities/_fixtures/notification/entity/subscription.activated.json @@ -117,7 +117,11 @@ } ], "status": "active", - "discount": null, + "discount": { + "id": "dsc_01hv6scyf7qdnzcdq01t2y8dx4", + "starts_at": "2024-04-12T10:18:47.635628Z", + "ends_at": "2024-05-12T10:18:47.635628Z" + }, "paused_at": null, "address_id": "add_01hv8gq3318ktkfengj2r75gfx", "created_at": "2024-04-12T10:18:48.831Z", diff --git a/tests/Unit/Entities/_fixtures/notification/entity/subscription.canceled.json b/tests/Unit/Entities/_fixtures/notification/entity/subscription.canceled.json index a13b288..f14948d 100644 --- a/tests/Unit/Entities/_fixtures/notification/entity/subscription.canceled.json +++ b/tests/Unit/Entities/_fixtures/notification/entity/subscription.canceled.json @@ -167,7 +167,11 @@ } ], "status": "canceled", - "discount": null, + "discount": { + "id": "dsc_01hv6scyf7qdnzcdq01t2y8dx4", + "starts_at": null, + "ends_at": null + }, "paused_at": null, "address_id": "add_01hv8gq3318ktkfengj2r75gfx", "created_at": "2024-04-12T10:38:00.761Z",