Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Subscription discount now supports null starts_at #109

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Entities/Subscription/SubscriptionDiscount.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SubscriptionDiscount
{
private function __construct(
public string $id,
public \DateTimeInterface $startsAt,
public \DateTimeInterface|null $startsAt,
public \DateTimeInterface|null $endsAt,
) {
}
Expand All @@ -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,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SubscriptionDiscount
{
private function __construct(
public string $id,
public \DateTimeInterface $startsAt,
public \DateTimeInterface|null $startsAt,
public \DateTimeInterface|null $endsAt,
) {
}
Expand All @@ -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,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
{
Expand Down Expand Up @@ -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
}
],
Expand Down
50 changes: 48 additions & 2 deletions tests/Unit/Entities/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
],
[
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading