Skip to content

Commit 59b25df

Browse files
committed
Fix Event type detection.
1 parent cee0d95 commit 59b25df

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

src/ProcessRevenueCatWebhookJob.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ class ProcessRevenueCatWebhookJob extends ProcessWebhookJob
99
{
1010
public function handle()
1111
{
12-
if (! isset($this->webhookCall->payload['event_type']) || $this->webhookCall->payload['event_type'] === '') {
12+
$event = $this->webhookCall->payload['event'] ?? null;
13+
14+
if (! $event || ! isset($event['type']) || $event['type'] === '') {
1315
throw WebhookFailed::missingType($this->webhookCall);
1416
}
1517

16-
event("revenuecat-webhooks::{$this->webhookCall->payload['event_type']}", $this->webhookCall);
18+
event("revenuecat-webhooks::{$event['type']}", $this->webhookCall);
1719

18-
$jobClass = $this->determineJobClass($this->webhookCall->payload['event_type']);
20+
$jobClass = $this->determineJobClass($event['type']);
1921

2022
if ($jobClass === '') {
2123
return;

tests/IntegrationTest.php

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PalauaAndSons\RevenueCatWebhooks\Tests;
44

5-
use Illuminate\Auth\Access\AuthorizationException;
65
use Illuminate\Support\Facades\Event;
76
use Illuminate\Support\Facades\Route;
87
use Spatie\WebhookClient\Exceptions\InvalidWebhookSignature;
@@ -19,7 +18,7 @@ public function setUp(): void
1918
Route::revenueCatWebhooks('revenuecat-webhooks');
2019
Route::revenueCatWebhooks('revenuecat-webhooks/{configKey}');
2120

22-
config(['revenuecat-webhooks.jobs' => ['my_type' => DummyJob::class]]);
21+
config(['revenuecat-webhooks.jobs' => ['INITIAL_PURCHASE' => DummyJob::class]]);
2322
cache()->clear();
2423
}
2524

@@ -29,8 +28,11 @@ public function it_can_handle_a_valid_request()
2928
$this->withoutExceptionHandling();
3029

3130
$payload = [
32-
'event_type' => 'my_type',
33-
'key' => 'value',
31+
'api_version' => '1.0',
32+
'event' => [
33+
'type' => 'INITIAL_PURCHASE',
34+
'key' => 'value',
35+
],
3436
];
3537

3638
$this->postJson('revenuecat-webhooks', $payload)
@@ -40,16 +42,17 @@ public function it_can_handle_a_valid_request()
4042

4143
$webhookCall = WebhookCall::first();
4244

43-
$this->assertEquals('my_type', $webhookCall->payload['event_type']);
45+
$this->assertEquals('INITIAL_PURCHASE', $webhookCall->payload['event']['type']);
4446
$this->assertEquals($payload, $webhookCall->payload);
4547
$this->assertNull($webhookCall->exception);
4648

47-
Event::assertDispatched('revenuecat-webhooks::my_type', function ($event, $eventPayload) use ($webhookCall) {
48-
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
49-
$this->assertEquals($webhookCall->id, $eventPayload->id);
49+
Event::assertDispatched('revenuecat-webhooks::INITIAL_PURCHASE',
50+
function ($event, $eventPayload) use ($webhookCall) {
51+
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
52+
$this->assertEquals($webhookCall->id, $eventPayload->id);
5053

51-
return true;
52-
});
54+
return true;
55+
});
5356

5457
$this->assertEquals($webhookCall->id, cache('dummyjob')->id);
5558
}
@@ -66,12 +69,13 @@ public function a_request_with_an_invalid_payload_will_be_logged_but_events_and_
6669

6770
$webhookCall = WebhookCall::first();
6871

69-
$this->assertFalse(isset($webhookCall->payload['event_type']));
72+
$this->assertFalse(isset($webhookCall->payload['event']['type']));
7073
$this->assertEquals(['invalid_payload'], $webhookCall->payload);
7174

72-
$this->assertEquals('Webhook call id `1` did not contain a type. Valid RevenueCat webhook calls should always contain a type.', $webhookCall->exception['message']);
75+
$this->assertEquals('Webhook call id `1` did not contain a type. Valid RevenueCat webhook calls should always contain a type.',
76+
$webhookCall->exception['message']);
7377

74-
Event::assertNotDispatched('revenuecat-webhooks::my_type');
78+
Event::assertNotDispatched('revenuecat-webhooks::INITIAL_PURCHASE');
7579

7680
$this->assertNull(cache('dummyjob'));
7781
}
@@ -84,8 +88,11 @@ public function it_can_handle_a_valid_request_with_authorization_header()
8488
$this->withoutExceptionHandling();
8589

8690
$payload = [
87-
'event_type' => 'my_type',
88-
'key' => 'value',
91+
'api_version' => '1.0',
92+
'event' => [
93+
'type' => 'INITIAL_PURCHASE',
94+
'key' => 'value',
95+
],
8996
];
9097

9198
$this->postJson('revenuecat-webhooks', $payload, ['Authorization' => 'Bearer ABC'])
@@ -95,16 +102,17 @@ public function it_can_handle_a_valid_request_with_authorization_header()
95102

96103
$webhookCall = WebhookCall::first();
97104

98-
$this->assertEquals('my_type', $webhookCall->payload['event_type']);
105+
$this->assertEquals('INITIAL_PURCHASE', $webhookCall->payload['event']['type']);
99106
$this->assertEquals($payload, $webhookCall->payload);
100107
$this->assertNull($webhookCall->exception);
101108

102-
Event::assertDispatched('revenuecat-webhooks::my_type', function ($event, $eventPayload) use ($webhookCall) {
103-
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
104-
$this->assertEquals($webhookCall->id, $eventPayload->id);
109+
Event::assertDispatched('revenuecat-webhooks::INITIAL_PURCHASE',
110+
function ($event, $eventPayload) use ($webhookCall) {
111+
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
112+
$this->assertEquals($webhookCall->id, $eventPayload->id);
105113

106-
return true;
107-
});
114+
return true;
115+
});
108116

109117
$this->assertEquals($webhookCall->id, cache('dummyjob')->id);
110118
}
@@ -118,16 +126,19 @@ public function it_rejects_a_request_with_invalid_authorization_header()
118126
$this->expectException(InvalidWebhookSignature::class);
119127

120128
$payload = [
121-
'event_type' => 'my_type',
122-
'key' => 'value',
129+
'api_version' => '1.0',
130+
'event' => [
131+
'type' => 'INITIAL_PURCHASE',
132+
'key' => 'value',
133+
],
123134
];
124135

125136
$this->postJson('revenuecat-webhooks', $payload, ['Authorization' => 'Invalid Bearer Token'])
126137
->assertStatus(500);
127138

128139
$this->assertCount(0, WebhookCall::get());
129140

130-
Event::assertNotDispatched('revenuecat-webhooks::my_type');
141+
Event::assertNotDispatched('revenuecat-webhooks::INITIAL_PURCHASE');
131142

132143
$this->assertNull(cache('dummyjob'));
133144
}

tests/RevenueCatWebhookCallTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ public function setUp(): void
2020

2121
Event::fake();
2222

23-
config(['revenuecat-webhooks.jobs' => ['my_type' => DummyJob::class]]);
23+
config(['revenuecat-webhooks.jobs' => ['INITIAL_PURCHASE' => DummyJob::class]]);
2424

2525
$this->webhookCall = WebhookCall::create([
2626
'name' => 'revenuecat',
27-
'payload' => ['event_type' => 'my_type', 'name' => 'value'],
27+
'payload' => [
28+
'api_version' => '1.0',
29+
'event' => [
30+
'type' => 'INITIAL_PURCHASE',
31+
'name' => 'value',
32+
],
33+
],
2834
'url' => 'https://example.com/revenuecat-webhooks',
2935
]);
3036

@@ -68,12 +74,13 @@ public function it_will_dispatch_events_even_when_no_corresponding_job_is_config
6874

6975
$webhookCall = $this->webhookCall;
7076

71-
Event::assertDispatched("revenuecat-webhooks::{$webhookCall->payload['event_type']}", function ($event, $eventPayload) use ($webhookCall) {
72-
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
73-
$this->assertEquals($webhookCall->id, $eventPayload->id);
77+
Event::assertDispatched("revenuecat-webhooks::{$webhookCall->payload['event']['type']}",
78+
function ($event, $eventPayload) use ($webhookCall) {
79+
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
80+
$this->assertEquals($webhookCall->id, $eventPayload->id);
7481

75-
return true;
76-
});
82+
return true;
83+
});
7784

7885
$this->assertNull(cache('dummyjob'));
7986
}

0 commit comments

Comments
 (0)