Skip to main content

How to Integrate RevenueCat Webhook in Laravel

How to Integrate RevenueCat Webhook in Laravel

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

Popular posts from this blog

How to Display Flash Messages in EJS using Node.js and Express

Displaying Flash Messages in EJS with Node.js and Express Flash messages are a great way to give users quick feedback — like "Login successful!" or "Please enter all fields!" . In this guide, you’ll learn how to implement them using: express-session connect-flash EJS templating ๐Ÿ“ฆ Step 1: Install Required Packages npm install express express-session connect-flash ejs ⚙️ Step 2: Setup Express App and Middleware const express = require('express'); const session = require('express-session'); const flash = require('connect-flash'); const app = express(); // Set view engine app.set('view engine', 'ejs'); // Middleware app.use(express.urlencoded({ extended: true })); app.use(session({ secret: 'yourSecretKey', resave: false, saveUninitialized: true })); app.use(flash()); // Make flash messages available to all views app.use((req, res, next) => { res.lo...

Laravel vs Node.js – Which One to Choose and Why?

Laravel vs Node.js – Which One to Choose and Why? Choosing the right backend technology is one of the most critical decisions in web development. Both Laravel and Node.js are widely adopted and loved by developers, but each comes with its unique strengths. In this post, I’ll compare both to help you decide which one suits your next project better. What is Laravel? Laravel is a PHP-based, open-source web application framework that follows the Model-View-Controller (MVC) architectural pattern. Known for its elegant syntax, Laravel simplifies development with features like authentication , routing , Eloquent ORM , and Blade templating . It’s ideal for developers looking for a structured and robust development experience. What is Node.js? Node.js is a JavaScript runtime built on Chrome’s V8 engine, allowing JavaScript to be used for server-side development. It uses a non-blocking, event-driven architecture, making it highly efficient and scalable ...

15 Must-Know Git Commands for Every Developer

15 Must-Know Git Commands for Every Developer (With Explanations) Whether you're working solo or with a team, Git is an essential tool for modern developers. These commands help manage your codebase effectively and are frequently asked in tech interviews. Below is a curated list of the 15 most useful Git commands every developer should know. Command Description git init Initializes a new Git repository in your project directory. git clone <url> Clones a remote repository to your local machine. git status Shows the status of changes (staged, unstaged, untracked) in the working directory. git add <file> Adds changes in the specified file(s) to the staging area. git commit -m "message" Records the staged changes with a descriptive message. git pull Fetches...