If you're using RevenueCat to manage in-app subscriptions for your mobile app (iOS, Android, etc.), it’s important to connect it with your backend. Laravel makes it easy to handle incoming webhooks for real-time events like purchases, renewals, cancellations, and more.
๐ What is RevenueCat?
RevenueCat is a powerful subscription management platform for mobile apps. It simplifies in-app purchases across iOS, Android, and Stripe. With webhooks, it notifies your server in real-time when important events happen, such as a new purchase or a cancellation.
๐ Webhook Events from RevenueCat
Common webhook events include:
INITIAL_PURCHASE
RENEWAL
CANCELLATION
BILLING_ISSUE
EXPIRATION
SUBSCRIBER_ALIAS
๐ For a full list of event payloads, visit: RevenueCat Sample Events
✅ Step-by-Step Guide to Integration
1. Create the Route
// routes/api.php
Route::post('/revenuecat/webhook', [App\Http\Controllers\RevenueCatController::class, 'handleWebhook']);
2. Create the Controller
php artisan make:controller RevenueCatController
3. Full Webhook Controller Code
// app/Http/Controllers/RevenueCatController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
class RevenueCatController extends Controller
{
public function handleWebhook(Request $request)
{
$data = $request->all();
Log::info('RevenueCat Webhook Received:', $data);
$event = $data['event']['type'] ?? null;
$userId = $data['event']['app_user_id'] ?? null;
$productId = $data['event']['product_id'] ?? null;
switch ($event) {
case 'INITIAL_PURCHASE':
// Create new subscription
DB::table('subscriptions')->insert([
'user_id' => $userId,
'status' => 'active',
'product_id' => $productId,
'purchased_at' => now(),
]);
DB::table('users')->where('id', $userId)->update([
'is_premium' => 1
]);
break;
case 'RENEWAL':
DB::table('subscriptions')->where('user_id', $userId)->update([
'renewed_at' => now()
]);
DB::table('users')->where('id', $userId)->update([
'is_premium' => 1
]);
break;
case 'CANCELLATION':
DB::table('subscriptions')->where('user_id', $userId)->update([
'status' => 'canceled'
]);
DB::table('users')->where('id', $userId)->update([
'is_premium' => 0
]);
break;
default:
Log::info('Unhandled RevenueCat event:', [$event]);
break;
}
return response()->json(['status' => 'success'], 200);
}
}
4. Set Webhook URL in RevenueCat
Go to your RevenueCat dashboard → Project settings → Webhooks → Add the URL:
https://yourdomain.com/api/revenuecat/webhook
5. Secure the Webhook (Optional)
You can secure your endpoint using secret keys, IP allow-listing, or validating RevenueCat's request signature to make sure it's authentic.
๐งช Testing Webhooks Locally
You can use ngrok to expose your local Laravel server and test webhooks from RevenueCat.
ngrok http 8000
✅ Conclusion
Integrating RevenueCat webhooks with Laravel lets you handle subscription events like purchases, renewals, and cancellations automatically and in real time. This integration ensures your users' premium access stays up-to-date and improves the overall app experience.
๐ฏ Pro tip: Always log and test each event properly before moving to production.
๐ฅ Want a downloadable Laravel + RevenueCat starter project? Let me know in the comments!
Comments
Post a Comment