diff --git a/config/revenuecat-webhooks.php b/config/revenuecat-webhooks.php index c4aec56..6d9e076 100644 --- a/config/revenuecat-webhooks.php +++ b/config/revenuecat-webhooks.php @@ -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, diff --git a/src/ProcessRevenueCatWebhookJob.php b/src/ProcessRevenueCatWebhookJob.php index cc87dd5..881039a 100644 --- a/src/ProcessRevenueCatWebhookJob.php +++ b/src/ProcessRevenueCatWebhookJob.php @@ -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; diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index abc155b..aa70fa4 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -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(); } @@ -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); @@ -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); diff --git a/tests/RevenueCatWebhookCallTest.php b/tests/RevenueCatWebhookCallTest.php index d264062..7c101af 100644 --- a/tests/RevenueCatWebhookCallTest.php +++ b/tests/RevenueCatWebhookCallTest.php @@ -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', ], ], @@ -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);