Skip to content

Commit

Permalink
Normalise event names to lowercase.
Browse files Browse the repository at this point in the history
  • Loading branch information
ipalaus committed May 19, 2024
1 parent 59b25df commit 7d1d67d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions config/revenuecat-webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*
* You can find a list of RevenueCat webhook events here:
* https://www.revenuecat.com/docs/integrations/webhooks/event-types-and-fields
*
* Important: use lowercase for your event names.
*/
'jobs' => [
// 'initial_purchase' => \App\Jobs\RevenueCatWebhooks\InitialPurchase::class,
Expand Down
7 changes: 5 additions & 2 deletions src/ProcessRevenueCatWebhookJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public function handle()
throw WebhookFailed::missingType($this->webhookCall);
}

event("revenuecat-webhooks::{$event['type']}", $this->webhookCall);
// Normalize the event type to lowercase, RevenueCat sends them as uppercase
$eventType = strtolower($event['type']);

$jobClass = $this->determineJobClass($event['type']);
event("revenuecat-webhooks::{$eventType}", $this->webhookCall);

$jobClass = $this->determineJobClass($eventType);

if ($jobClass === '') {
return;
Expand Down
6 changes: 3 additions & 3 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function setUp(): void
Route::revenueCatWebhooks('revenuecat-webhooks');
Route::revenueCatWebhooks('revenuecat-webhooks/{configKey}');

config(['revenuecat-webhooks.jobs' => ['INITIAL_PURCHASE' => DummyJob::class]]);
config(['revenuecat-webhooks.jobs' => ['initial_purchase' => DummyJob::class]]);
cache()->clear();
}

Expand Down Expand Up @@ -46,7 +46,7 @@ public function it_can_handle_a_valid_request()
$this->assertEquals($payload, $webhookCall->payload);
$this->assertNull($webhookCall->exception);

Event::assertDispatched('revenuecat-webhooks::INITIAL_PURCHASE',
Event::assertDispatched('revenuecat-webhooks::initial_purchase',
function ($event, $eventPayload) use ($webhookCall) {
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
$this->assertEquals($webhookCall->id, $eventPayload->id);
Expand Down Expand Up @@ -106,7 +106,7 @@ public function it_can_handle_a_valid_request_with_authorization_header()
$this->assertEquals($payload, $webhookCall->payload);
$this->assertNull($webhookCall->exception);

Event::assertDispatched('revenuecat-webhooks::INITIAL_PURCHASE',
Event::assertDispatched('revenuecat-webhooks::initial_purchase',
function ($event, $eventPayload) use ($webhookCall) {
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
$this->assertEquals($webhookCall->id, $eventPayload->id);
Expand Down
6 changes: 3 additions & 3 deletions tests/RevenueCatWebhookCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public function setUp(): void

Event::fake();

config(['revenuecat-webhooks.jobs' => ['INITIAL_PURCHASE' => DummyJob::class]]);
config(['revenuecat-webhooks.jobs' => ['initial_purchase' => DummyJob::class]]);

$this->webhookCall = WebhookCall::create([
'name' => 'revenuecat',
'payload' => [
'api_version' => '1.0',
'event' => [
'type' => 'INITIAL_PURCHASE',
'type' => 'INITIAL_PURCHASE', // Event type in uppercase as sent by RevenueCat
'name' => 'value',
],
],
Expand Down Expand Up @@ -74,7 +74,7 @@ public function it_will_dispatch_events_even_when_no_corresponding_job_is_config

$webhookCall = $this->webhookCall;

Event::assertDispatched("revenuecat-webhooks::{$webhookCall->payload['event']['type']}",
Event::assertDispatched("revenuecat-webhooks::initial_purchase",
function ($event, $eventPayload) use ($webhookCall) {
$this->assertInstanceOf(WebhookCall::class, $eventPayload);
$this->assertEquals($webhookCall->id, $eventPayload->id);
Expand Down

0 comments on commit 7d1d67d

Please sign in to comment.