Skip to content

Commit 6f5191e

Browse files
fix: Subscription discount now supports null starts_at (#109)
1 parent 26ad12c commit 6f5191e

File tree

8 files changed

+79
-11
lines changed

8 files changed

+79
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
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.
88

9+
## [1.7.1] - 2024-12-13
10+
11+
### Fixed
12+
13+
- Subscription discount now supports null `starts_at`
14+
915
## [1.7.0] - 2024-12-11
1016

1117
### Added

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
class Client
6262
{
63-
private const SDK_VERSION = '1.7.0';
63+
private const SDK_VERSION = '1.7.1';
6464

6565
public readonly LoggerInterface $logger;
6666
public readonly Options $options;

src/Entities/Subscription/SubscriptionDiscount.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SubscriptionDiscount
1717
{
1818
private function __construct(
1919
public string $id,
20-
public \DateTimeInterface $startsAt,
20+
public \DateTimeInterface|null $startsAt,
2121
public \DateTimeInterface|null $endsAt,
2222
) {
2323
}
@@ -26,7 +26,7 @@ public static function from(array $data): self
2626
{
2727
return new self(
2828
$data['id'],
29-
DateTime::from($data['starts_at']),
29+
isset($data['starts_at']) ? DateTime::from($data['starts_at']) : null,
3030
isset($data['ends_at']) ? DateTime::from($data['ends_at']) : null,
3131
);
3232
}

src/Notifications/Entities/Subscription/SubscriptionDiscount.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SubscriptionDiscount
1717
{
1818
private function __construct(
1919
public string $id,
20-
public \DateTimeInterface $startsAt,
20+
public \DateTimeInterface|null $startsAt,
2121
public \DateTimeInterface|null $endsAt,
2222
) {
2323
}
@@ -26,7 +26,7 @@ public static function from(array $data): self
2626
{
2727
return new self(
2828
$data['id'],
29-
DateTime::from($data['starts_at']),
29+
isset($data['starts_at']) ? DateTime::from($data['starts_at']) : null,
3030
isset($data['ends_at']) ? DateTime::from($data['ends_at']) : null,
3131
);
3232
}

tests/Functional/Resources/Subscriptions/_fixtures/response/list_default.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,11 @@
323323
"update_payment_method": null,
324324
"cancel": "https://sandbox-buyer-portal.paddle.com/subscriptions/sub_01hp463gxfvndqjjyqn2n7tkth/cancel"
325325
},
326-
"discount": null,
326+
"discount": {
327+
"id": "dsc_01hv6scyf7qdnzcdq01t2y8dx4",
328+
"starts_at": null,
329+
"ends_at": null
330+
},
327331
"import_meta": null
328332
},
329333
{
@@ -467,7 +471,11 @@
467471
"update_payment_method": "https://buyer-portal.paddle.com/subscriptions/sub_01hn0epy6nc46wt9hw92pp2kmt/update-payment-method",
468472
"cancel": "https://buyer-portal.paddle.com/subscriptions/sub_01hn0epy6nc46wt9hw92pp2kmt/cancel"
469473
},
470-
"discount": null,
474+
"discount": {
475+
"id": "dsc_01hv6scyf7qdnzcdq01t2y8dx4",
476+
"starts_at": "2024-04-12T10:18:47.635628Z",
477+
"ends_at": "2024-05-12T10:18:47.635628Z"
478+
},
471479
"import_meta": null
472480
}
473481
],

tests/Unit/Entities/EventTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Paddle\SDK\Notifications\Entities\Shared\SavedPaymentMethodOrigin;
1313
use Paddle\SDK\Notifications\Events\PaymentMethodDeleted;
1414
use Paddle\SDK\Notifications\Events\PaymentMethodSaved;
15+
use Paddle\SDK\Notifications\Events\SubscriptionActivated;
16+
use Paddle\SDK\Notifications\Events\SubscriptionCanceled;
1517
use Paddle\SDK\Tests\Utils\ReadsFixtures;
1618
use PHPUnit\Framework\TestCase;
1719
use Psr\Http\Message\ServerRequestInterface;
@@ -222,13 +224,13 @@ public static function eventDataProvider(): iterable
222224
[
223225
'subscription.activated',
224226
'subscription',
225-
\Paddle\SDK\Notifications\Events\SubscriptionActivated::class,
227+
SubscriptionActivated::class,
226228
\Paddle\SDK\Notifications\Entities\Subscription::class,
227229
],
228230
[
229231
'subscription.canceled',
230232
'subscription',
231-
\Paddle\SDK\Notifications\Events\SubscriptionCanceled::class,
233+
SubscriptionCanceled::class,
232234
\Paddle\SDK\Notifications\Entities\Subscription::class,
233235
],
234236
[
@@ -479,4 +481,48 @@ public function it_creates_deleted_payment_method(): void
479481
self::assertSame('2024-05-02T02:55:25.198+00:00', $paymentMethod->savedAt->format(DATE_RFC3339_EXTENDED));
480482
self::assertSame('2024-05-03T12:24:18.826+00:00', $paymentMethod->updatedAt->format(DATE_RFC3339_EXTENDED));
481483
}
484+
485+
/**
486+
* @test
487+
*/
488+
public function it_supports_subscription_discount(): void
489+
{
490+
$event = Event::from([
491+
'event_id' => 'evt_01h8bzakzx3hm2fmen703n5q45',
492+
'event_type' => 'subscription.activated',
493+
'occurred_at' => '2023-08-21T11:57:47.390028Z',
494+
'notification_id' => 'ntf_01h8bzam1z32agrxjwhjgqk8w6',
495+
'data' => self::readJsonFixture('notification/entity/subscription.activated'),
496+
]);
497+
498+
self::assertSame('ntf_01h8bzam1z32agrxjwhjgqk8w6', $event->notificationId);
499+
500+
self::assertInstanceOf(SubscriptionActivated::class, $event);
501+
self::assertInstanceOf(Entity::class, $event->data);
502+
self::assertSame($event->data, $event->subscription);
503+
self::assertEquals('2024-04-12T10:18:47+00:00', $event->subscription->discount->startsAt->format(DATE_RFC3339));
504+
self::assertEquals('2024-05-12T10:18:47+00:00', $event->subscription->discount->endsAt->format(DATE_RFC3339));
505+
}
506+
507+
/**
508+
* @test
509+
*/
510+
public function it_supports_nullable_subscription_discount_starts_at_and_ends_at(): void
511+
{
512+
$event = Event::from([
513+
'event_id' => 'evt_01h8bzakzx3hm2fmen703n5q45',
514+
'event_type' => 'subscription.canceled',
515+
'occurred_at' => '2023-08-21T11:57:47.390028Z',
516+
'notification_id' => 'ntf_01h8bzam1z32agrxjwhjgqk8w6',
517+
'data' => self::readJsonFixture('notification/entity/subscription.canceled'),
518+
]);
519+
520+
self::assertSame('ntf_01h8bzam1z32agrxjwhjgqk8w6', $event->notificationId);
521+
522+
self::assertInstanceOf(SubscriptionCanceled::class, $event);
523+
self::assertInstanceOf(Entity::class, $event->data);
524+
self::assertSame($event->data, $event->subscription);
525+
self::assertNull($event->subscription->discount->startsAt);
526+
self::assertNull($event->subscription->discount->endsAt);
527+
}
482528
}

tests/Unit/Entities/_fixtures/notification/entity/subscription.activated.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@
117117
}
118118
],
119119
"status": "active",
120-
"discount": null,
120+
"discount": {
121+
"id": "dsc_01hv6scyf7qdnzcdq01t2y8dx4",
122+
"starts_at": "2024-04-12T10:18:47.635628Z",
123+
"ends_at": "2024-05-12T10:18:47.635628Z"
124+
},
121125
"paused_at": null,
122126
"address_id": "add_01hv8gq3318ktkfengj2r75gfx",
123127
"created_at": "2024-04-12T10:18:48.831Z",

tests/Unit/Entities/_fixtures/notification/entity/subscription.canceled.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@
167167
}
168168
],
169169
"status": "canceled",
170-
"discount": null,
170+
"discount": {
171+
"id": "dsc_01hv6scyf7qdnzcdq01t2y8dx4",
172+
"starts_at": null,
173+
"ends_at": null
174+
},
171175
"paused_at": null,
172176
"address_id": "add_01hv8gq3318ktkfengj2r75gfx",
173177
"created_at": "2024-04-12T10:38:00.761Z",

0 commit comments

Comments
 (0)