Skip to content

schorpy/Freemius-Laravel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Freemius Package

This package integrates Freemius subscription management with Laravel, inspired by Laravel Cashier. It allows you to manage subscriptions, plans, and payments using Freemius, along with providing easy-to-use methods for interacting with the Freemius API.

Features

  • Subscribe users to Freemius plans
  • Retrieve user subscription details from Freemius
  • Swap plans, update payment methods, and cancel subscriptions
  • Download invoices directly
  • Webhook support for real-time subscription events

Environment Settings

Add your Freemius credentials to your .env file:

FREEMIUS_API_TOKEN=1a5dd
FREEMIUS_PRODUCT_SECRET_KEY=sk_
FREEMIUS_PRODUCT_ID=12345
FREEMIUS_STORE_ID=1234

Billable Model

To make sure we can actually create checkouts for our customers, we'll need to configure a model to be our "billable" model. This is typical the User model of your app. To do this, import and use the Billable trait on your model:

use Freemius\Laravel\Billable;
 
class User extends Authenticatable
{
    use Billable;
}

Webhooks & CSRF Protection

Incoming webhooks should not be affected by CSRF protection. To prevent this, add your webhook path to the except list of your App\Http\Middleware\VerifyCsrfToken middleware:

protected $except = [
    'freemius/*',
];

Or if you're using Laravel v11 and up, you should exclude freemius/* in your application's bootstrap/app.php file:

->withMiddleware(function (Middleware $middleware) {
    $middleware->validateCsrfTokens(except: [
        'freemius/*',
    ]);
})

Webhook URL:

http://127.0.0.1:8000/freemius/webhook

Usage Examples

Generate Checkout Link

$user = $request->user();
$checkout = $user->checkout($planId)
    ->withEmail('john@example.com')
    ->withTitle('my saas product')
    ->showReviews(true)
    ->showRefundBadge(true)
    ->withCancelUrl('https://site.com/checkout/')
    ->withCancelIcon('https://example.com/logo.png');

return $checkout->redirect();

Check Subscription Status

if ($user->subscribed()) {
    // User has an active subscription
}

if ($user->subscription()->valid()) {
    // Subscription is valid
}

Download Invoice

use Freemius\Laravel\Freemius;

$url = Freemius::downloadInvoice($paymentId);

Swap Plan

$subscription = $request->user()->subscription();
$subscription->cancelUrl = 'https://site.com/checkout/';
$subscription->cancelIcon = 'https://example.com/logo.png';

$url = $subscription->swap($planId);
return redirect()->away($url);

Update Payment Method

$subscription = $request->user()->subscription();
$url = $subscription->updatePaymentMethodUrl();

return redirect()->away($url);

Cancel Subscription

$subscription = $request->user()->subscription();

if (! $subscription) {
    return response()->json(['error' => 'No active subscription found.'], 404);
}

// Cancel subscription
$subscription->cancel($subscription->subscription_id);

Contributing

We welcome contributions! Steps to contribute:

  • Fork the repository and clone to your local machine

  • Create a feature branch for your changes

  • Implement your changes or add new features

  • Test thoroughly

  • Submit a pull request with a detailed description

Notes

  • This package is still in development. Some features may be incomplete.

  • Contributions, bug reports, and feature requests are highly appreciated.

About

Laravel package for Freemius payment SaaS.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published