Skip to content

Commit 37a487d

Browse files
authored
Merge pull request #153 from imdhemy/feat/get-notification-payload
[1.x] Get raw notification data
2 parents 5ece24b + f63d7ff commit 37a487d

File tree

10 files changed

+343
-184
lines changed

10 files changed

+343
-184
lines changed

composer.lock

Lines changed: 149 additions & 142 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Contracts/ServerNotificationContract.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,45 @@
66

77
/**
88
* Interface ServerNotificationContract
9+
*
910
* @package Imdhemy\Purchases\Events\Contracts
1011
*/
1112
interface ServerNotificationContract
1213
{
1314
/**
15+
* Gets the notification type
16+
*
1417
* @return string
1518
*/
1619
public function getType(): string;
1720

1821
/**
22+
* Gets the subscription associated with the notification
23+
*
1924
* @param Client|null $client
25+
*
2026
* @return SubscriptionContract
2127
*/
2228
public function getSubscription(?Client $client = null): SubscriptionContract;
2329

2430
/**
31+
* Returns true if the notification is a test notification
32+
*
2533
* @return bool
2634
*/
2735
public function isTest(): bool;
2836

2937
/**
38+
* Gets the application bundle
39+
*
3040
* @return string
3141
*/
3242
public function getBundle(): string;
43+
44+
/**
45+
* Gets the notification payload
46+
*
47+
* @return array
48+
*/
49+
public function getPayload(): array;
3350
}

src/Contracts/SubscriptionContract.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88

99
/**
1010
* Interface SubscriptionContract
11+
*
1112
* @package Imdhemy\Purchases\Events\Contracts
1213
*/
1314
interface SubscriptionContract
1415
{
16+
// List of providers
17+
public const PROVIDER_APP_STORE = 'app_store';
18+
public const PROVIDER_GOOGLE_PLAY = 'google_play';
19+
1520
/**
1621
* @return Time
1722
*/

src/ServerNotifications/AppStoreServerNotification.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public function getBundle(): string
9090
return (string)$this->notification->getBid();
9191
}
9292

93+
/**
94+
* Gets the notification payload
95+
*
96+
* @return array
97+
*/
98+
public function getPayload(): array
99+
{
100+
return $this->notification->toArray();
101+
}
102+
93103
/**
94104
* @return string|null
95105
*/

src/ServerNotifications/AppStoreV2ServerNotification.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ private function __construct(V2DecodedPayload $payload)
2525
$this->payload = $payload;
2626
}
2727

28+
/**
29+
* Static constructor
30+
*
31+
* @param V2DecodedPayload $decodedPayload
32+
*
33+
* @return static
34+
*/
2835
public static function fromDecodedPayload(V2DecodedPayload $decodedPayload): self
2936
{
3037
return new self($decodedPayload);
@@ -63,4 +70,14 @@ public function getBundle(): string
6370
{
6471
return $this->payload->getAppMetadata()->bundleId();
6572
}
73+
74+
/**
75+
* Gets the notification payload
76+
*
77+
* @return array
78+
*/
79+
public function getPayload(): array
80+
{
81+
return $this->payload->toArray();
82+
}
6683
}

src/ServerNotifications/GoogleServerNotification.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/**
1313
* Class GoogleServerNotification
14+
*
1415
* @package Imdhemy\Purchases\ServerNotifications
1516
*/
1617
class GoogleServerNotification implements ServerNotificationContract
@@ -20,10 +21,11 @@ class GoogleServerNotification implements ServerNotificationContract
2021
/**
2122
* @var DeveloperNotification
2223
*/
23-
private $notification;
24+
private DeveloperNotification $notification;
2425

2526
/**
2627
* GoogleServerNotification constructor.
28+
*
2729
* @param DeveloperNotification $notification
2830
*/
2931
public function __construct(DeveloperNotification $notification)
@@ -45,6 +47,7 @@ public function getType(): string
4547

4648
/**
4749
* @param Client|null $client
50+
*
4851
* @return SubscriptionContract
4952
* @throws GuzzleException
5053
*/
@@ -68,4 +71,14 @@ public function getBundle(): string
6871
{
6972
return $this->notification->getPackageName();
7073
}
74+
75+
/**
76+
* Gets the notification payload
77+
*
78+
* @return array
79+
*/
80+
public function getPayload(): array
81+
{
82+
return $this->notification->toArray();
83+
}
7184
}

src/Subscriptions/AppleSubscription.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getItemId(): string
5858
*/
5959
public function getProvider(): string
6060
{
61-
return 'app_store';
61+
return SubscriptionContract::PROVIDER_APP_STORE;
6262
}
6363

6464
/**

tests/ServerNotifications/AppStoreServerNotificationTest.php renamed to tests/Unit/ServerNotifications/AppStoreServerNotificationTest.php

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
11
<?php
22

3-
namespace Tests\ServerNotifications;
3+
namespace Tests\Unit\ServerNotifications;
44

55
use Imdhemy\AppStore\ServerNotifications\ServerNotification;
6-
use Imdhemy\Purchases\Contracts\ServerNotificationContract;
76
use Imdhemy\Purchases\Contracts\SubscriptionContract;
87
use Imdhemy\Purchases\ServerNotifications\AppStoreServerNotification;
98
use Imdhemy\Purchases\ValueObjects\Time;
9+
use JsonException;
1010
use Tests\TestCase;
1111

1212
class AppStoreServerNotificationTest extends TestCase
1313
{
1414
/**
1515
* @var AppStoreServerNotification
1616
*/
17-
private $appStoreServerNotification;
17+
private AppStoreServerNotification $appStoreServerNotification;
18+
19+
/**
20+
* @var array
21+
*/
22+
private array $serverNotificationBody;
1823

1924
/**
2025
* @inheritDoc
26+
* @throws JsonException
2127
*/
2228
protected function setUp(): void
2329
{
2430
parent::setUp();
2531
$path = $this->testAssetPath('appstore-server-notification.json');
26-
$serverNotificationBody = json_decode(file_get_contents($path), true);
32+
$this->serverNotificationBody = json_decode(file_get_contents($path), true, 512, JSON_THROW_ON_ERROR);
2733

28-
$serverNotification = ServerNotification::fromArray($serverNotificationBody);
34+
$serverNotification = ServerNotification::fromArray($this->serverNotificationBody);
2935
$this->appStoreServerNotification = new AppStoreServerNotification($serverNotification);
3036
}
3137

3238
/**
3339
* @test
3440
*/
35-
public function test_constructor()
36-
{
37-
$this->assertInstanceOf(ServerNotificationContract::class, $this->appStoreServerNotification);
38-
}
39-
40-
/**
41-
* @test
42-
*/
43-
public function test_get_notification_type()
41+
public function get_notification_type(): void
4442
{
4543
$this->assertEquals(
4644
ServerNotification::DID_CHANGE_RENEWAL_STATUS,
@@ -51,15 +49,17 @@ public function test_get_notification_type()
5149
/**
5250
* @test
5351
*/
54-
public function test_get_subscription()
52+
public function get_subscription(): void
5553
{
56-
$this->assertInstanceOf(SubscriptionContract::class, $this->appStoreServerNotification->getSubscription());
54+
$subscription = $this->appStoreServerNotification->getSubscription();
55+
56+
$this->assertEquals(SubscriptionContract::PROVIDER_APP_STORE, $subscription->getProvider());
5757
}
5858

5959
/**
6060
* @test
6161
*/
62-
public function test_get_change_renewal_status_data()
62+
public function get_change_renewal_status_data(): void
6363
{
6464
$isAutoRenewal = $this->appStoreServerNotification->isAutoRenewal();
6565
$changeDate = $this->appStoreServerNotification->getAutoRenewStatusChangeDate();
@@ -71,9 +71,23 @@ public function test_get_change_renewal_status_data()
7171
/**
7272
* @test
7373
*/
74-
public function test_get_bundle()
74+
public function get_bundle(): void
7575
{
76-
$this->assertNotNull($this->appStoreServerNotification->getBundle());
76+
$this->assertEquals(
77+
'com.twigano.fashion',
78+
$this->appStoreServerNotification->getBundle()
79+
);
80+
}
81+
82+
/**
83+
* @test
84+
*/
85+
public function get_payload(): void
86+
{
87+
$this->assertEquals(
88+
$this->serverNotificationBody,
89+
$this->appStoreServerNotification->getPayload()
90+
);
7791
}
7892

7993
/**
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Tests\Unit\ServerNotifications;
4+
5+
use Imdhemy\AppStore\ServerNotifications\V2DecodedPayload;
6+
use Imdhemy\Purchases\ServerNotifications\AppStoreV2ServerNotification;
7+
use Tests\TestCase;
8+
9+
class AppStoreV2ServerNotificationTest extends TestCase
10+
{
11+
private V2DecodedPayload $payload;
12+
13+
private AppStoreV2ServerNotification $sut;
14+
15+
/**
16+
* @inheritDoc
17+
*/
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$claims = [
23+
'notificationType' => V2DecodedPayload::TYPE_TEST,
24+
'data' => [
25+
'bundleId' => 'com.example.app',
26+
],
27+
];
28+
29+
$this->payload = V2DecodedPayload::fromArray($claims);
30+
31+
$this->sut = AppStoreV2ServerNotification::fromDecodedPayload($this->payload);
32+
}
33+
34+
/**
35+
* @test
36+
*/
37+
public function get_type(): void
38+
{
39+
$this->assertEquals(V2DecodedPayload::TYPE_TEST, $this->sut->getType());
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function get_subscription(): void
46+
{
47+
$this->assertSame(
48+
$this->payload,
49+
$this->sut->getSubscription()->getProviderRepresentation()
50+
);
51+
}
52+
53+
/**
54+
* @test
55+
*/
56+
public function is_test(): void
57+
{
58+
$this->assertTrue($this->sut->isTest());
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function get_bundle(): void
65+
{
66+
$this->assertEquals('com.example.app', $this->sut->getBundle());
67+
}
68+
69+
/**
70+
* @test
71+
*/
72+
public function get_payload(): void
73+
{
74+
$this->assertSame($this->payload->toArray(), $this->sut->getPayload());
75+
}
76+
}

0 commit comments

Comments
 (0)